Batch file 批处理文件以输入多个文件类型,并根据文件类型将其隔离

Batch file 批处理文件以输入多个文件类型,并根据文件类型将其隔离,batch-file,Batch File,我已经写了上面的批处理文件,我需要它应该采取多个过滤器,如HTM、XML等,并将它们粘贴到目标文件夹(创建与过滤器名称相同的文件夹)中,并根据过滤器将它们隔离。这是您的批处理代码,根据要求进行扩展并注释: @echo off set /p var=Enter source folder: set /p var1=Enter destination folder: set /p var2=Select Filer: xcopy /f /j /s /z /c /y "%var%\*.%var2

我已经写了上面的批处理文件,我需要它应该采取多个过滤器,如HTM、XML等,并将它们粘贴到目标文件夹(创建与过滤器名称相同的文件夹)中,并根据过滤器将它们隔离。

这是您的批处理代码,根据要求进行扩展并注释:

@echo off
set /p var=Enter source folder:

set /p var1=Enter destination folder:
set /p var2=Select Filer:

xcopy /f /j /s /z /c /y  "%var%\*.%var2%" "%var1%\%var2%" 
最难解决的任务示例:从筛选器规范获取文件类型:

源文件夹是:
C:\Temp\Source

目标文件夹是:
C:\Temp\Target

选择的过滤器是:
***.txt*.htm**.php?文件*x.y.*文件*Image*?

执行的XCOPY命令包括:

@echo off
setlocal EnableExtensions EnableDelayedExpansion

rem For source and target folder predefine the variables with
rem a double quote as value to avoid a syntax error in case of
rem user just hits key RETURN or ENTER without entering anything.

:EnterSource
set "SourceFolder=""
set /P "SourceFolder=Enter source folder: "
rem Remove all double quotes from enter string.
set "SourceFolder=!SourceFolder:"=!"
rem Check if the remaining string is an empty string.
if "%SourceFolder%" == "" goto EnterSource
rem Replace all slashes by backslashes
set "SourceFolder=!SourceFolder:/=\!"
rem Remove a trailing backslash.
if "%SourceFolder:~-1%" == "\" set "SourceFolder=!SourceFolder:~0,-1!"
rem Check if source folder exists.
if not exist "%SourceFolder%\*" goto EnterSource
echo.

:EnterTarget
set "TargetFolder=""
set /P "TargetFolder=Enter target folder: "
set "TargetFolder=!TargetFolder:"=!"
if "%TargetFolder%" == "" goto EnterTarget
set "TargetFolder=!TargetFolder:/=\!"
if "%TargetFolder:~-1%" == "\" set "TargetFolder=!TargetFolder:~0,-1!"

echo.
echo Multiple select filter can be entered separated by spaces.
echo.
echo For example: *.txt *.htm *.log
echo.
echo The default is: *
echo.

:EnterFilter
set "SelectFilter=*"
set /P "SelectFilter=Select filter: "
set "SelectFilter=!SelectFilter:"=!"
if "%SelectFilter%" == "" goto EnterFilter

rem Run xcopy for each filter specified. The command FOR is used to separate
rem the string on spaces. Only the first token (= first filer) is always
rem used for a single XCOPY execution and the remaining token (= filters)
rem are assigned again to variable SelectFilter. After each execution of
rem XCOPY it is checked if there are more folters to process and therefore
rem if FOR and XCOPY must be executed once more.

:CopyLoop
for /F "tokens=1*" %%I in ("%SelectFilter%") do (
    call :GetFileType "%%~xI"
    xcopy "%SourceFolder%\%%I" "%TargetFolder%\!FileType!" /C /F /H /I /K /R /S /Y
    set "SelectFilter=%%J"
)
if not "%SelectFilter%" == "" goto CopyLoop
endlocal
goto :EOF

rem This function TRIES to get from file extension string according to
rem filter specification just the file type using string substitutions.
rem The command goto :EOF results in exiting the function an returning
rem to CopyLoop above with continuing the execution on command XCOPY.

:GetFileType
set "FileType=%~1"

rem The passed file type string could be an empty string if the filter
rem specification is for example "File*" without a file extension.

if "%FileType%" == "" goto :EOF

rem Remove the period at beginning of file type string. It is
rem guaranteed that there is only one period in the entire file
rem type string which is at beginning if there is one at all.

set "FileType=%FileType:.=%"

rem The file type string could be now an empty string if the filter
rem specification is for example just "*" to match all files.

if "%FileType%" == "" goto :EOF

rem Remove an asterisk at end of file type string if there is one.

if "%FileType:~-1%" == "*" set "FileType=%FileType:~0,-1%"

rem The file type string could be now an empty string
rem if the filter specification is for example "*.*".

if "%FileType%" == "" goto :EOF

rem Remove everything after first wildcard character
rem for example if filter specification is "*.php?".

for /F "tokens=1 delims=*?" %%T in ("%FileType%") do set "FileType=%%T"
if "%FileType%" == "" goto :EOF

rem Remove all question marks if the file type string
rem consists now only of 1 or more question marks.

set "FileType=%FileType:?=%"

rem The file type string could be now an empty string if
rem the filter specification is for example "Image*.???".

if "%FileType%" == "" goto :EOF

rem Append a backslash to not empty file type string for XCOPY.

set "FileType=%FileType%\"
goto :EOF
要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • 呼叫/?
  • echo/?
  • endlocal/?
  • 获取/?
  • goto/?
  • 如果/?
  • rem/?
  • 设置/?
  • setlocal/?
  • xcopy/?

您是否听说过的
?在命令提示窗口中键入
for/?
并阅读帮助文本…这真的非常有用,非常感谢,只是有一件事它不会创建与筛选器名称相同的单独文件夹并在其中隔离它们。@Sandy Batch code已重新编写,用于根据筛选器规范确定文件类型并将其附加到目标文件夹路径。一个有问题的输入和输出目录列表的例子可以很好地理解这个需求。描述批处理代码作为过滤器规范的期望值也是很有用的。这段代码甚至是为不寻常的过滤器规范编写的,但我很确定它并不适用于批处理用户可以输入的所有过滤器。
xcopy "C:\Temp\Source\*"          "C:\Temp\Target\"     /C /F /H /I /K /R /S /Y
xcopy "C:\Temp\Source\*.*"        "C:\Temp\Target\"     /C /F /H /I /K /R /S /Y
xcopy "C:\Temp\Source\*.txt"      "C:\Temp\Target\txt"  /C /F /H /I /K /R /S /Y
xcopy "C:\Temp\Source\*.htm*"     "C:\Temp\Target\htm\" /C /F /H /I /K /R /S /Y
xcopy "C:\Temp\Source\*.php?"     "C:\Temp\Target\php\" /C /F /H /I /K /R /S /Y
xcopy "C:\Temp\Source\File*"      "C:\Temp\Target\"     /C /F /H /I /K /R /S /Y
xcopy "C:\Temp\Source\x.y.*"      "C:\Temp\Target\"     /C /F /H /I /K /R /S /Y
xcopy "C:\Temp\Source\File_??.*"  "C:\Temp\Target\"     /C /F /H /I /K /R /S /Y
xcopy "C:\Temp\Source\Image*.???" "C:\Temp\Target\"     /C /F /H /I /K /R /S /Y