Batch Move files

Scenario

I needed to move files in batches that would put no more than 600 files in a folder at any time. I wrote the batch file to count the files in the target directory,

Prerequisites

Tested on Windows 2008 R2.

Command line should be compatible with Windows 2000 or newer.

Code

@echo off
set sourcefolder=I:\Isource
set sourcefilemask=*.ext
set targetfolder=I:\target
set logfile=i:\importlog.txt
set limit=300
set batchsize=300
For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do ( 
    Set Month=%%A
    Set Day=%%B
    Set Year=%%C
)
For /F "tokens=1,2 delims=: " %%A in ('Time /t') do ( 
    Set Hour=%%A
    Set Min=%%B
)


I:
cd %targetfolder%
set count=0
set index=0

echo %Year%-%Month%-%Day% %Hour%:%Min% Starting batch move tests. %0
echo %Year%-%Month%-%Day% %Hour%:%Min% %0 ran. >> %logfile%


REM Check if source files exist
if not exist %sourcefolder%\%sourcefilemask% goto :end

REM Count files in target then run move
for /f %%f in ('dir %targetfolder%  /b ') do call :count %%~f
if %count% gtr %limit% goto :end
echo Finishing with %count%
call :movebatch
echo %Year%-%Month%-%Day% %Hour%:%Min% :: Target Count %count% :: Move Index %index%  >> %logfile%
goto :end



REM Check the total files in the target folder.  
:count
rem If there are more than %limit% files, stop the script.
if %count% gtr %limit% goto :end
echo Counting files : %count%
set /a count+=1
goto :end


:movebatch
for /f %%f in ('dir %sourcefolder%\%sourcefilemask% /b /o-d ') do call :proc %%~f
echo Moved %index% files.  >> %logfile%
goto :end


:proc
if %index% gtr %batchsize% goto :end
  set /a index+=1
  move %sourcefolder%\%1 %targetfolder%
  ren %targetfolder%\%1 %1.import
  echo %index% - File [%1] moved and prepared for import.
goto :end

:end



There is a good command reference here: http://ss64.com/