Festerau
Festerau

Reputation: 3

Command line counter not incrementing

Hi I have a batch script to move x amount of files from one folder to another. The counter that count the files moved is not incrementing. The script is as follows

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

echo on
set DataMax=50
set Counter=1
set SrcMax=50
set DataLoc=Destination Folder
Set HoldLoc=Source Folder
set count=0
FOR /F %%a in ('DIR /B %DataLoc%\*.pst') do set /A count=count+1
if %count% GEQ %DataMax% (Goto Exit) else (GOTO FMove)
:FMove
Echo Gather Top 50 files

FOR /F "TOKENS=*" %%a IN ('dir /A-D /O-D /B %HoldLoc%\*.pst') DO (
        if %Counter% LEQ %SrcMax% (
        MOVE /y %HoldLoc%\%%a %DataLoc%\
        SET /A Counter += 1
       )
    )
goto Exit
:Exit
exit

The Set /A Counter += 1 does not seem to work. Thanks in Advance for any assistance.

Upvotes: 0

Views: 125

Answers (2)

dbenham
dbenham

Reputation: 130809

npockmaka has shown how to get your code to work under normal circumstances by using delayed expansion. However, it will fail if any files names contain the ! character (unlikely, but it could happen)

It is possible to make the code work without delayed expansion by intentionally dividing by zero when the maximum count is exceeded. The error message is hidden by redirecting to nul, and the || operator detects the error and conditionally executes the EXIT command.

I also streamlined the first loop to use FIND to quickly get the count, instead of iterating each file.

@echo off
setlocal
set /a count=0, SrcMax=DataMax=50
set "DataLoc=Destination Folder"
set "HoldLoc=Source Folder"
for /f %%N in (
  'dir /b "%DataLoc%\*.pst"^|find /c /v ""'
) do if %%N geq %DataMax% exit /b
echo Gather Top 50 files
for /f "eol=: delims=" %%A in (
  'dir /a-d /o-d /b "%HoldLoc%\*.pst"'
) do (
  set /a "1/(SrcMax-count), count+=1" 2>nul || exit /b
  move /y "%HoldLoc%\%%B" "%DataLoc%\"
)

Another option is to number each file via FINDSTR /N, and let FOR /F parse out the number and file name.

@echo off
setlocal
set /a SrcMax=DataMax=50
set "DataLoc=Destination Folder"
set "HoldLoc=Source Folder"
for /f %%N in (
  'dir /b "%DataLoc%\*.pst"^|find /c /v ""'
) do if %%N geq %DataMax% exit /b
echo Gather Top 50 files
for /f "tokens=1* delims=:" %%A in (
  'dir /a-d /o-d /b "%HoldLoc%\*.pst"^|findstr /n "^"'
) do (
  if %%A gtr %SrcMax% exit /b
  move /y "%HoldLoc%\%%B" "%DataLoc%"
)

There is one thing that concerns me in your logic.

If you already have 50 files in your destination, then you exit without doing anything. If you do not yet have 50 files, then you move up to 50 files from the source to the destination. If there are 49 files in the destination at the start, then there is the potential to end up with 99 files in the destination, assuming none of the moved file names match the existing files in the destination.

Upvotes: 1

npocmaka
npocmaka

Reputation: 57242

As you already have enabled delayed expansion try like:

FOR /F "TOKENS=*" %%a IN ('dir /A-D /O-D /B %HoldLoc%\*.pst') DO (
        if !Counter! LEQ %SrcMax% (
         MOVE /y %HoldLoc%\%%a %DataLoc%\
         SET /A Counter=Counter+1
       )
    )

Upvotes: 3

Related Questions