曾尝试创建其他批处理脚本来为我处理文件。我可以在.rar文件中发送1个或几个.pdf测试文件。
所以我想要瞄准的是:
If the first variable 1 is named 'test' then
Is there a .rar file in the folder from variable 2 then
Extract to a folder and then delete .rar file
else
check that there is a .pdf file and then copy to folder
Else
Tell the user that neither a file or a archive has been found
我设法将其拼凑在一起,但是我需要帮助以尝试进一步扩展它以包括所有选项:
@echo off
set "cat=%1"
IF "%cat%"=="test" ( for /f %%G in ('dir *.rar /b') do set filename=%%~G)
echo %filename%
这只给我一半文件名,因为它们在文件名中有空格,还需要更改第三行的目录以查找发送的变量2。
要添加到该文件中,我只是被告知.txt文件是相同的,倍数以.rar文件形式发送给我
您可以使用此:
@echo off
set "cat=%~1"
IF "%cat%"=="test" (
for %%G in (*.rar) do set filename=%%G
)
echo %filename%
这里通配符用于获取目录中的所有rar文件。
我建议打开command prompt,运行call /?
并阅读输出帮助。帮助说明了如何从批处理文件中引用批处理文件的参数(也称为选项或参数,而不是变量)。
建议检查是否使用至少一个参数调用了批处理文件,如果必须使用至少一个参数调用了该文件,并且在没有任何参数的情况下启动了该批处理文件,或者输出了一个帮助,请输出帮助以正确使用批处理文件以/?
开头,这是Windows上用于获取有关命令或程序帮助的默认设置。
Rar.txt
。双击打开该文件即可读取该文本文件,其中Rar.exe
可以提取目录中找到的一个或多个* .rar存档文件。因此,实际上没有必要使用命令FOR。但是建议此任务使用命令[[FOR,因为在成功提取RAR归档文件后应删除RAR文件。让我们看看
FOR
for /f %%G in ('dir *.rar /b') do
及其在执行时的作用。FOR
与选项/F
一起使用来处理文本文件内容或单个字符串,或者命令行的输出在这种情况下会导致在后台使用%ComSpec% /c
和命令行之间的命令行启动命令处理。附加两个'
。因此,由Windows命令进程cmd.exe
执行的处理如下:默认情况下,Windows已安装到for /F
中,并且使用C:\Windows
处理批处理文件:C:\Windows\System32\cmd.exe /c dir *.rar /b
DIR由后台的单独命令处理执行
在当前目录中搜索
*.rar
/A-H
上的默认默认为/A
)&()[]{}^=;!'+,`~
。STDERR。FOR
分别处理批处理文件的命令进程将输出处理,将后台命令进程的句柄STDERR重定向到其自己的STDERR句柄,这导致将其显示在控制台窗口中案件。但是,用于处理启动的后台命令过程的STDOUT的输出分别由FOR捕获处理批处理文件的命令过程,并在启动的后台命令过程自身终止后逐行处理。与选项/F
一起使用的FOR
DIR在使用选项/B
执行时不会输出空行。for /F
默认情况下使用正常空格和水平制表符作为字符串定界符将非空行拆分为子字符串,并且默认情况下仅将第一个空格/制表符分隔的字符串分配给指定的循环变量,此处为循环变量G
。如果将行拆分后的第一个子字符串以分号开头,则默认情况下,for /F
也将忽略已处理的行,因为eol=;
是行尾选项的默认选项。
for /f %%G in ('dir *.rar /b') do
在处理DIR
输出的目录条目列表时会引起一些问题。对于包含空格的文件/文件夹名称,仅将文件/文件夹名称的第一个由空格/制表符分隔的部分分配给循环变量G
,而不是完整名称。例如,将My Archive.rar
这样的名称仅将My
分配给循环变量G
。
G
,这再次表示G
不持有完整名称。例如,类似 TwoLeadingSpaces.rar
的名称导致仅将G
分配给循环变量TwoLeadingSpaces.rar
,而没有两个前导空格,并且在引用循环变量G
的值时找不到文件(或文件夹)。;Test.rar
(以分号开头的名称)或 ;TestWithALeadingSpace.rar
(以前导空格和分号开头的名称)之类的名称,以进行进一步处理。第2点和第3点通常没有问题,因为开头的文件/文件夹名称前带有空格或分号确实很少。但是带有空格的文件/文件夹名称经常出现。/F
的FOR
for %%G in (*.rar) do
FOR
*.rar
相匹配的非隐藏文件(不是目录),并为找到的文件名分配不带循环变量 FORG
和下一个路径的文件在do
之后运行命令。没有启动其他命令过程,也没有完成子字符串拆分。但是,对于每个找到的文件名执行的命令删除,移动或重命名与通配符模式*.rar
匹配的文件时,这种非常简单的解决方案就会出现问题。与通配符模式匹配的目录条目的列表在FOR循环的主体的每次迭代中更改,而命令
因此,成功提取后需要删除RAR存档文件的更好解决方案是:for /F "eol=| delims=" %%I in ('dir *.rar /A-D /B 2^>nul') do
DIR选项的输出就是与当前目录中的通配符模式匹配的文件名,包括隐藏的RAR存档文件。[/A-D
导致忽略带有属性目录的目录条目。因此,DIR
2^>nul
作为2>nul
传递给后台命令进程,这导致将在没有找到的*.rar
文件上的DIR所输出的错误消息重定向到设备NUL
来抑制它。阅读有关Using command redirection operators的Microsoft文章,以获取2>nul
的解释。重定向运算符>
必须在FOR命令行上使用脱字符号^
进行转义,才能在执行命令FOR
并执行嵌入的[ C0]在后台启动的单独命令过程中的命令行。dir
选项for /F
将行尾字符从eol=|
更改为;
。根据Microsoft关于|
的文档,任何文件名的文件名中都不能包含竖线。因此,由于文件结尾选项,FOR不再忽略任何文件名。Naming Files, Paths, and Namespaces选项for /F
将将行拆分为子字符串的定界符列表更改为空的定界符列表,这将完全禁用行拆分行为。因此,一个文件名在文件名中的任何位置都有一个或多个空格,将完全分配给指定的循环变量delims=
。
关于批处理文件参数的处理方式,任务描述不是很清楚,特别是如果第一个参数不区分大小写I
时。
test
或根本没有参数或以test
作为第一个参数的情况下调用此任务而工作。/?
为了了解所使用的命令及其工作方式,请打开@echo off
setlocal EnableExtensions DisableDelayedExpansion
if "%~1" == "" goto OutputHelp
if "%~1" == "/?" goto OutputHelp
if /I not "%~1" == "test" goto MoreCode
set "SourceFolder=%~2"
if defined SourceFolder goto CheckFolder
echo/
echo Error: Folder with RAR or PDF file(s) not specified on command line.
goto OutputHelp
:CheckFolder
rem Replace all forward slashes by backslashes in folder name.
set "SourceFolder=%SourceFolder:/=\%"
rem Append a backslash to folder path if it does not end with a backslash.
if not "%SourceFolder:~-1%" == "\" set "SourceFolder=%SourceFolder%\"
rem Check the existence of the source folder.
if exist "%SourceFolder%" goto ProcessFolder
echo/
echo Error: Folder "%SourceFolder%" does not exist.
goto OutputHelp
:ProcessFolder
rem Get full qualidfied folder name, i.e. the folder name
rem with its absolute path and ending with a backslash.
for %%I in ("%SourceFolder%") do set "SourceFolder=%%~fI"
rem Define the destination folder for the PDF files extracted from the
rem RAR archive file(s) in source folder or copied from source folder.
set "DestinationFolder=C:\Temp\Test\"
rem Search for all *.rar files in folder passed with second argument and
rem extract all *.pdf files in each RAR archive file to the configured
rem destination folder. Rar.exe creates the destination folder automatically
rem if it is not already existing. The batch file is halted after processing
rem a RAR file on which Rar.exe exited with a value greater 0. Read the exit
rem codes documentation of Rar.exe at bottom of text file Rar.txt for more
rem information about the RAR exit codes. See Rar.txt also for the meaning
rem of the few RAR switches used here.
set "RarFileCount=0"
for /F "eol=| delims=" %%I in ('dir "%SourceFolder%*.rar" /A-D /B 2^>nul') do (
set /A RarFileCount+=1
"%ProgramFiles%\WinRAR\Rar.exe" e -cfg- -idcdp -or -- "%SourceFolder%%%I" *.pdf "%DestinationFolder%"
if not errorlevel 1 (del /A /F "%SourceFolder%%%I") else echo/& pause
)
if %RarFileCount% == 0 goto CheckFiles
if %RarFileCount% == 1 (set "PluralS=") else set "PluralS=s"
echo/
echo Info: Processed %RarFileCount% *.rar file%PluralS% in folder "%SourceFolder%".
goto EndBatch
:CheckFiles
echo Info: There are no *.rar files in folder "%SourceFolder%".
if exist "%SourceFolder%*.pdf" goto CopyFiles
echo Info: There are no *.pdf files in folder "%SourceFolder%".
goto EndBatch
:CopyFiles
rem Copy all PDF files in source folder to destination folder. xcopy.exe
rem creates destination folder automatically if it is not already existing.
echo/
%SystemRoot%\System32\xcopy.exe "%SourceFolder%*.pdf" "%DestinationFolder%" /C /I /Y
goto EndBatch
:OutputHelp
echo/
echo Usage: %~n0 [TEST] [Folder with RAR or PDF file(s)]
echo/
echo If the first argument is case-insensitive TEST, the second argument
echo specifies the folder containing the RAR files to extract or the PDF
echo files to copy to destination folder. The folder must be specified
echo with first argument being TEST.
echo/
pause
goto EndBatch
:MoreCode
rem Add here the code to execute on first argument is not case-insensitive TEST.
:EndBatch
endlocal
窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。
call /?
del /?
dir /?
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
rem /?
set /?
setlocal /?
xcopy /?