批处理文件,用于输入多种文件类型,并根据文件类型对其进行隔离

时间:2016-01-07 00:26:04

标签: batch-file

@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%" 

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

1 个答案:

答案 0 :(得分:0)

以下是您按要求扩展的批处理代码并注释:

@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

要解决的最困难任务的示例:从过滤器规范中获取文件类型:

源文件夹是:C:\Temp\Source
目标文件夹是:C:\Temp\Target
选择过滤器为:* *.* *.txt *.htm* *.php? File* x.y.* File_??.* Image*.???

执行的 XCOPY 命令是:

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

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
  • xcopy /?