如何根据文件名模式将文件移动到不同的文件夹?

时间:2016-05-25 11:47:08

标签: batch-file vbscript

我有以下代码将.tiff或.tif扩展名的文件移动到目标文件夹。

move C:\Pics\*.tif D:\folder\images
move C:\Pics\*.tiff D:\folder\images

但是,我想只将文件名只包含数字的文件移动到文件夹
D:\folder\images

如果文件中包含任何非数字字符,则应将其移至文件夹
D:\folder\Fail

例如,如果文件名为00000012345.tiff,则应将其移至文件夹D:\folder\images。但如果它在名称中包含任何非数字字符,例如000c012ab.tif,则应将其移至D:\folder\Fail

我希望将其编码为批处理脚本。但如果使用批处理文件无法做到这一点,那么VB脚本也可以。

这是原始问题的延续。如何设置批量移动文件的限制,让我们在一批中说50。我尝试过类似的东西,但它没有用。我只需要批量处理.tif / tiff文件。

for %%I in ("C:\Documents\Pictures\*.tif*") do (
Rem Max Limit which can be moved in batch file
set MaxLimit=2
    set "HasOnlyDigits=1"
    for /F "tokens=1 delims=0123456789" %%T in ("%%~nI")  do set "HasOnlyDigits=%%T"
    if "!HasOnlyDigits!" == "1"  (if %%I==%MaxLimit% exit /b 0
(
        move /Y "%%I" "%FolderGood%"

 )
 )  

1 个答案:

答案 0 :(得分:1)

这个评论的批处理代码应该完成这项工作:

@echo off
setlocal EnableExtensions EnableDelayedExpansion

rem Define the two target folders.
set "FolderGood=D:\folder\images"
set "FolderFail=D:\folder\Fail"

rem Create the target folder for good image files if not already existing.
if not exist "%FolderGood%\*" md "%FolderGood%"
goto LoopBegin

rem Process all *.tif and *.tiff files in folder C:\Pics. The first FOR loop
rem holds in loop variable I the name of a TIFF file with full path and file
rem extension. With %%~nI just the file name of the TIFF file without path
rem and without file extension is used in a second loop which interprets
rem because of parameter /F the double quoted file name as string and not
rem as file name pattern like the first FOR loop.

rem The second FOR loop tries to get the first substring from file
rem name string consisting only of non digit characters. This is only
rem successful if the file name contains any non digit character at all
rem which is assigned to environment variable HasOnlyDigits overwriting
rem its value 1 (a digit) assigned to the variable just before.

rem Next the value of the environment variable HasOnlyDigits is compared
rem with the initial value 1 using delayed expansion as necessary here,
rem and moves the TIF file containing only digits in file name to the
rem target folder for good files if this string comparison is true.
rem Otherwise the file name contained at least 1 non digit character and
rem therefore HasOnlyDigits does not have anymore the string value 1 and
rem so the file is moved to target folder for failed files. The folder
rem is created if not already existing. But it is expected that there
rem are no TIFF files with a non digit character in file name. Therefore
rem the folder for non good TIFF files is created only when really needed.

rem The maximum number of TIF files moved by one execution of
rem this batch file is determined by the number specified below.

:LoopBegin
set "FileCount=50"

for %%I in ("C:\Pics\*.tif*") do (
    if !FileCount! EQU 0 (
        echo Exiting after having moved already %FileCount% TIF files.
        goto LoopEnd
    )
    set "HasOnlyDigits=1"
    for /F "tokens=1 delims=0123456789" %%T in ("%%~nI") do set "HasOnlyDigits=%%T"
    if "!HasOnlyDigits!" == "1" (
        move /Y "%%I" "%FolderGood%"
    ) else (
        if not exist "%FolderFail%\*" md "%FolderFail%"
        move /Y "%%I" "%FolderFail%"
    )
    set /A FileCount-=1
)

rem Discard all environment variables set in this batch file and restore
rem initial environment variables on calling this batch file as well as
rem restoring initial states for delayed expansion and command extensions
rem and restore also initial current directory whereby the current
rem directory was not changed by the batch code above.

:LoopEnd
endlocal

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

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • md /?
  • move /?
  • rem /?
  • set /?
  • setlocal /?