Batch File loop through Files

April 14, 2011 at 08:33:28
Specs: Windows XP
Hi all,
I have a batch file to backup some mdb files, by copying, renaming, and copying again...but it was written for 1 file, and I need to do mutliple files now that might exist in a folder.
How can i write the loop?

Thanks in advance for help *code below*

@echo off
cd c:\
cd "%SystemDrive%\RTS\"
mkdir Backup
xcopy *.* "%SystemDrive%\RTS\Backup"
cd "%SystemDrive%\RTS\Backup"
for /F %%a in ('dir /b *.mdb') do set FileName=%%~na
for %%i in (*.mdb) do ren %%i %FILENAME%_%COMPUTERNAME%.mdb
xcopy *.* "\\codfile\2011 RTS" /e /c /i /h /r /k /y
cd "%SystemDrive%\RTS\"
rmdir Backup /s/q

See More: Batch File loop through Files

Report •

April 14, 2011 at 13:09:40
put parans around the for loop and use the /S switch in your DIR it...

for /F %%a in ('dir /b /s *.mdb') do (
  set FileName=%%~na
  xcopy %FILENAME%_%COMPUTERNAME%.mdb "\\codfile\2011 RTS" /e /c /i /h /r /k /y

Report •

April 15, 2011 at 06:30:08
I tried it, but it only does the loop twice, and it doesn't append ComputerName, it replaces the entire filename with ComputerName.
I had some issues like this when i tried the loop...

is there an easier way to rename these files by appending the computername, then copying the files....or is the loop easiest way?

thx for response btw...

Report •

April 15, 2011 at 07:55:02
If it only looped twice then there only two MDB files in the directory and sub-directories under the directory you ran this batch file maybe you should modify your dire to run from the root like...

for /F %%a in ('dir c:\ /b /s *.mdb') do (

as for the renameing you could just do...

ren *.mdb *_%COMPUTERNAME%.mdb

Not sure what you are doing I only answered the question about a loop not appending file names.

Good Luck.

Report •
Related Solutions

Ask Question