使用批处理文件按扩展名移动单个文件
Move a single file by extension with a batch file
这是我第一次 post 访问此站点,但由于我已经在 Whosebug 上找到了很多帮助,所以我现在自己问这个问题。我没有找到这个问题的答案。我的英语不是很好,而且我的问题很复杂,我希望你们都能理解我。如果没有或者有任何问题需要帮助我,请随时提问。
所以我想制作一个批处理文件,通过扩展名(在我的例子中,它是 .pdf)移动文件,但不是所有文件。我知道有这个命令:
move "Y:\Scan\*.pdf" "Y:\Scan\Archive\"
但这会将所有 pdf 文件移动到存档文件夹中,我不希望这样做。
为了更好地了解我的计划,我想需要一个简短的描述。
假设您有一台家庭服务器并且您的计算机已连接到它,还有一台打印机和一台扫描仪。我在家庭办公室的文件夹中有很多文档,我想将它们全部放在我的服务器上。这是通过扫描这些文件夹中的所有纸张来完成的。每次我扫描一些东西时,它都会以 .pdf 格式保存在我本地计算机上的一个文件夹中,按时间排序,看起来像这样:2017-06-15-10-30.pdf
我当前的脚本要求用户输入 3 次。一次是村庄名称,一次是街道名称,一次是房屋编号,因为档案文件夹的名称应该与它们所指的相应房屋相同。所以我的批处理中已经有了这 3 个输入,并且整个文件夹名称都保存在一个新变量中。
我现在的计划是询问用户有多少 pdf 对应于上次创建的文件夹,无论用户输入什么,这个数量的文件都将存储在该文件夹中。为了更好地理解这一点,这是我到目前为止所做的(我的批处理代码)。
@echo off
cls
title Archive-Script (by Relentless)
color 0b
:reset
cls
REM This is the village's name
SET ON=""
REM This is the street's name
SET SN=""
REM This is the number of the house
SET HN=""
REM This is a variable to store whether the user wants to reset the variables or not (used later)
SET RESET=""
:start
cls
IF %RESET% == N echo To overwrite the village's name, please enter the name again and hit enter. If you don't want to overwrite the name, just hit enter!
IF NOT %RESET% == N echo Please enter the village's name (confirm by pressing enter):
IF %RESET% == N echo Current name: %ON%
set /p "ON="
echo The village's name was set to %ON%!
echo.
IF %RESET% == N echo To overwrite the street's name, please enter the name again and hit enter. If you don't want to overwrite the name, just hit enter!
IF NOT %RESET% == N echo Please enter the street's name (confirm by pressing enter):
IF %RESET% == N echo Current name: %SN%
set /p "SN="
echo The street's name was set to %SN%!
echo.
IF %RESET% == N echo To overwrite the house's number, please enter the number again and hit enter. If you don't want to overwrite the number, just hit enter!
IF NOT %RESET% == N echo Please enter the house's number (confirm by pressing enter):
IF %RESET% == N echo Current number: %HN%
set /p "HN="
echo The house's number was set to %HN%!
echo.
echo.
echo.
echo.
echo.
SET FOLDERNAME="%ON%, %SN% %HN%"
echo The folder %FOLDERNAME% will now be created!
mkdir ""%FOLDERNAME%""
timeout /t 2 /nobreak
:pdfmove
cls
echo How many pdf-files are corresponding to the latest created folder?
set /p "PDF="
for /l %%x in (1,1,%PDF%) do (
echo %%x
move Y:\Scan\*.pdf Y:\Scan\%FOLDERNAME%\
)
:resetoption
cls
echo %ON%
echo %SN%
echo %HN%
echo.
echo Should the variables be resetted again? (J/N)
set /p "RESET="
IF %RESET% == J goto reset
IF %RESET% == N goto start
如您所见,我已经尝试实施移动选项,但这将移动所有 pdf 文件,而不是每个 for 循环移动一个。任何人都可以帮我吗?我想代码很容易理解。
无情的问候
for /l %%x in (1,1,%PDF%) do (
set "moved="
for /f "delims=" %%a in ('dir /b /a-d "y:\scan\*.pdf" '
) do if not defined moved (
echo %%x : %%a
set "moved=Y"
move "Y:\Scan\%%a" Y:\Scan\%FOLDERNAME%\
)
)
我相信您想做的是移动用户输入的 pdf 数量。
以上更改将针对每个 %%x
,首先将标志 moved
设置为 nothing,然后执行目录扫描,在 /b
基本形式(只有文件名)/a-d
没有目录名。目录中存在的每个文件名将依次分配给 %%a
。 delims=
选项允许处理包含空格的文件名。
当找到第一个文件时,其名称是 echo
与序列号和标志是 set
到 Y
(任何非空字符串都可以)。然后移动文件。
当 for...%%a...
处理下一个文件名时,moved
现在设置为一个值,因此不再执行文件移动。
然后对 %%x
的每个值重复整个过程,因此将移动在源目录中找到的第一个 .pdf
文件名的 %PDF%
次移动,因为每次移动一个时间和 dir
命令将找不到上次移动的文件,因为好吧 - 它已被移动,因此不再位于源目录中。
好的,这是生成可变数量空格的修复方法
SET "tspaces=T"
:addspace
SET "tspaces=%tspaces% "
CALL SET "testspace=%%tspaces:~%pdf%%%"
IF "%testspace%" neq " " GOTO addspace
FOR /L %%x IN (1,1,%pdf%) DO (
set "moved="
for /f "delims=" %%a in ('dir /b /a-d "y:\scan\*.pdf" '
) do if not defined moved (
echo %%x : %%a
set "moved=Y"
call move "Y:\Scan\%%a" "Y:\Scan\%FOLDERNAME%\olddocumen%%tspaces:~0,%%x%%.pdf"
)
)
第一部分设置tspaces
到t
+足够的空格数
请注意,在第二部分中,move
现在是 call
ed,它解析变量子字符串。由于 %%x
从 1 到 %pdf%
变化,因此 tspaces
的选择是 %%x
个字符 - 当 olddocument
的最后一个字符是必需的;此后跟随着一些空格,所以 t
是 tspaces
.
的初始值
这是我第一次 post 访问此站点,但由于我已经在 Whosebug 上找到了很多帮助,所以我现在自己问这个问题。我没有找到这个问题的答案。我的英语不是很好,而且我的问题很复杂,我希望你们都能理解我。如果没有或者有任何问题需要帮助我,请随时提问。
所以我想制作一个批处理文件,通过扩展名(在我的例子中,它是 .pdf)移动文件,但不是所有文件。我知道有这个命令:
move "Y:\Scan\*.pdf" "Y:\Scan\Archive\"
但这会将所有 pdf 文件移动到存档文件夹中,我不希望这样做。 为了更好地了解我的计划,我想需要一个简短的描述。 假设您有一台家庭服务器并且您的计算机已连接到它,还有一台打印机和一台扫描仪。我在家庭办公室的文件夹中有很多文档,我想将它们全部放在我的服务器上。这是通过扫描这些文件夹中的所有纸张来完成的。每次我扫描一些东西时,它都会以 .pdf 格式保存在我本地计算机上的一个文件夹中,按时间排序,看起来像这样:2017-06-15-10-30.pdf
我当前的脚本要求用户输入 3 次。一次是村庄名称,一次是街道名称,一次是房屋编号,因为档案文件夹的名称应该与它们所指的相应房屋相同。所以我的批处理中已经有了这 3 个输入,并且整个文件夹名称都保存在一个新变量中。
我现在的计划是询问用户有多少 pdf 对应于上次创建的文件夹,无论用户输入什么,这个数量的文件都将存储在该文件夹中。为了更好地理解这一点,这是我到目前为止所做的(我的批处理代码)。
@echo off
cls
title Archive-Script (by Relentless)
color 0b
:reset
cls
REM This is the village's name
SET ON=""
REM This is the street's name
SET SN=""
REM This is the number of the house
SET HN=""
REM This is a variable to store whether the user wants to reset the variables or not (used later)
SET RESET=""
:start
cls
IF %RESET% == N echo To overwrite the village's name, please enter the name again and hit enter. If you don't want to overwrite the name, just hit enter!
IF NOT %RESET% == N echo Please enter the village's name (confirm by pressing enter):
IF %RESET% == N echo Current name: %ON%
set /p "ON="
echo The village's name was set to %ON%!
echo.
IF %RESET% == N echo To overwrite the street's name, please enter the name again and hit enter. If you don't want to overwrite the name, just hit enter!
IF NOT %RESET% == N echo Please enter the street's name (confirm by pressing enter):
IF %RESET% == N echo Current name: %SN%
set /p "SN="
echo The street's name was set to %SN%!
echo.
IF %RESET% == N echo To overwrite the house's number, please enter the number again and hit enter. If you don't want to overwrite the number, just hit enter!
IF NOT %RESET% == N echo Please enter the house's number (confirm by pressing enter):
IF %RESET% == N echo Current number: %HN%
set /p "HN="
echo The house's number was set to %HN%!
echo.
echo.
echo.
echo.
echo.
SET FOLDERNAME="%ON%, %SN% %HN%"
echo The folder %FOLDERNAME% will now be created!
mkdir ""%FOLDERNAME%""
timeout /t 2 /nobreak
:pdfmove
cls
echo How many pdf-files are corresponding to the latest created folder?
set /p "PDF="
for /l %%x in (1,1,%PDF%) do (
echo %%x
move Y:\Scan\*.pdf Y:\Scan\%FOLDERNAME%\
)
:resetoption
cls
echo %ON%
echo %SN%
echo %HN%
echo.
echo Should the variables be resetted again? (J/N)
set /p "RESET="
IF %RESET% == J goto reset
IF %RESET% == N goto start
如您所见,我已经尝试实施移动选项,但这将移动所有 pdf 文件,而不是每个 for 循环移动一个。任何人都可以帮我吗?我想代码很容易理解。
无情的问候
for /l %%x in (1,1,%PDF%) do (
set "moved="
for /f "delims=" %%a in ('dir /b /a-d "y:\scan\*.pdf" '
) do if not defined moved (
echo %%x : %%a
set "moved=Y"
move "Y:\Scan\%%a" Y:\Scan\%FOLDERNAME%\
)
)
我相信您想做的是移动用户输入的 pdf 数量。
以上更改将针对每个 %%x
,首先将标志 moved
设置为 nothing,然后执行目录扫描,在 /b
基本形式(只有文件名)/a-d
没有目录名。目录中存在的每个文件名将依次分配给 %%a
。 delims=
选项允许处理包含空格的文件名。
当找到第一个文件时,其名称是 echo
与序列号和标志是 set
到 Y
(任何非空字符串都可以)。然后移动文件。
当 for...%%a...
处理下一个文件名时,moved
现在设置为一个值,因此不再执行文件移动。
然后对 %%x
的每个值重复整个过程,因此将移动在源目录中找到的第一个 .pdf
文件名的 %PDF%
次移动,因为每次移动一个时间和 dir
命令将找不到上次移动的文件,因为好吧 - 它已被移动,因此不再位于源目录中。
好的,这是生成可变数量空格的修复方法
SET "tspaces=T"
:addspace
SET "tspaces=%tspaces% "
CALL SET "testspace=%%tspaces:~%pdf%%%"
IF "%testspace%" neq " " GOTO addspace
FOR /L %%x IN (1,1,%pdf%) DO (
set "moved="
for /f "delims=" %%a in ('dir /b /a-d "y:\scan\*.pdf" '
) do if not defined moved (
echo %%x : %%a
set "moved=Y"
call move "Y:\Scan\%%a" "Y:\Scan\%FOLDERNAME%\olddocumen%%tspaces:~0,%%x%%.pdf"
)
)
第一部分设置tspaces
到t
+足够的空格数
请注意,在第二部分中,move
现在是 call
ed,它解析变量子字符串。由于 %%x
从 1 到 %pdf%
变化,因此 tspaces
的选择是 %%x
个字符 - 当 olddocument
的最后一个字符是必需的;此后跟随着一些空格,所以 t
是 tspaces
.