Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 855
Mareena Avatar asked Sep 24 '26 13:09

Mareena


1 Answers

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
like image 88
Alex K. Avatar answered Sep 26 '26 04:09

Alex K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!