简体   繁体   中英

How to move files to different folders based on pattern of file name?

I have the following code which moves the files with.tiff or.tif extension to the destination folder.

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

However, I want to move only the files which have a file name consisting only of digits to folder
D:\folder\images

If the file contains any non digit character in the file name, then it should be moved to folder
D:\folder\Fail

For example if the file name is 00000012345.tiff , then it should be moved to the folder D:\folder\images . But if it contains any non digit character in the name such as 000c012ab.tif , then it should be moved to the D:\folder\Fail .

I want coded this preferably as batch script. But if this can't be done with a batch file, then a VB script would be also okay.

This is continuation to the original question. How can I set the limit to move the files in batches, let us say 50 in one batch. I tried something like this but it is not working. I need batches only for.tif/tiff files.

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

 )
 )  

This commented batch code should do the job:

@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

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM