[Solved] Batch file for moving file to not yet exist folder [closed]


  • Get the total count of files to move,
  • iterate files and count,
  • calculate the rest and check if uneven.

:: Q:\Test\2018\12\14\SO_53784108.cmd
@Echo off & SetLocal EnableDelayedExpansion
set "Base=A:\PHOTOS"
PushD "%Base%" || (Echo can't find Base:%Base% &Pause&Goto :Eof)

:: get Total files
For /f %%A in ('dir /B *.png ^|find /c ".png"') Do set "Total=%%%A"

Set Cnt=0
For /f "delims=" %%A in ('dir /B *.png') Do (
    Set /A "Cnt+=1,Rest=Total-Cnt+(Cnt%%2),DstNum=1000+(Cnt+1)/2"
    Set "Dest=%Base%\PHOTOS_SUB!DstNum:~-3!"
    MD "!Dest!" 2>Nul
    If !Rest! lss 2 (
        Move "%Base%\*.png" "!Dest!\" >Nul
        Goto :End
    ) else (
        Move "%%A" "!Dest!\" >Nul
    )
)
:End
PopD
Goto :Eof

Sample Tree after running the batch:

> Tree \ /F
└───PHOTOS
    ├───PHOTOS_SUB001
    │       image_001.png
    │       image_002.png
    │
    ├───PHOTOS_SUB002
    │       image_003.png
    │       image_004.png
    │
    └───PHOTOS_SUB003
            image_005.png
            image_006.png
            image_007.png

solved Batch file for moving file to not yet exist folder [closed]