Mareena
Mareena

Reputation: 81

Move Files using loop in Windows Batch Programming

I am using below code to transfer files, it is showing on each movement of file that 1 file(s) moved, 1 file(s) moved and so on...but it is not showing at the end that total number of files moved ? it was working for my first code even echo %%i was placed in the same location as placed below...plz help...?

setlocal enabledelayedexpansion
if exist C:\Hi\*.pdf (goto COPYFILES) else (goto NOFILES)

:COPYFILES
for /f %%i in ('DIR /b C:\Hi\*_*.*') do (
    echo %%i
    set fn=%%i
    set fn=!fn:~11,8!
    move C:\Hi\%%i E:\!fn!\
)
echo complete

:NOFILES
echo There are no files to move

Upvotes: 3

Views: 8725

Answers (1)

Alex K.
Alex K.

Reputation: 175936

The variable %%i will only ever contain part of the file name, so you try to

move C:\Hi\30072011.pdf 

instead of

move c:\hi\1000225013_30072011.pdf

Alternative:

setlocal enabledelayedexpansion
if exist C:\Hi\*.pdf (goto COPYFILES) else (goto NOFILES)

:COPYFILES
for /f %%i in ('DIR /b C:\Hi\*_*.*') do (
    echo %%i
    set fn=%%i
    set fn=!fn:~11,8!
    move C:\Hi\%%i E:\!fn!\
)
echo complete
goto:eof

:NOFILES
echo There are no files to move

Upvotes: 1

Related Questions