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

问题描述 投票:0回答:1

我有以下代码,可将扩展名为 .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%"

 )
 )  
batch-file vbscript
1个回答
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 /?
© www.soinside.com 2019 - 2024. All rights reserved.