Batch Script to Find, Rename and Copy Files

Microsoft Windows vista home premium - 3...
October 1, 2010 at 10:23:47
Specs: Windows 7
I need to write a batch or command to accomplish:
Scenario: There are about 100 odd folders in a parent directory which conatain a file "report.xml". I need to find these files and copy all the 40-50 files found to some seperate directory. Since all the files have the same name "report.xml", I need to rename the files with a name identifying the location they were found i.e. their parent folder. So if the file is found in a sub folder SB, it will be called as SB.xml.

C:\Reports\report\SERVER1\folder\report.xml -----> SERVER1.xml
C:\Reports\report\SERVER2\folder\report.xml -----> SERVER2.xml
C:\Reports\report\SERVER3\\folder\report.xml -----> SERVER3.xml

I know a for loop will do the trick, but how to tokenize the full file path and accomplish it?
Appreciate your time..


See More: Batch Script to Find, Rename and Copy Files

Report •

#1
October 1, 2010 at 12:57:36
Thanks to Razor2.3 for showing how to get the parent folder.

==================================================
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b/s/a-d c:\files\*.doc') do (
set P=%%~Pa
set P=!P:~0,-1!
call :sub1 !P!
echo.copy "%%a" "d:\dest\!F!%%~Xa"
)

goto :eof

:sub1
set F=%~N1
goto :eof


=====================================
Helping others achieve escape felicity

M2


Report •

#2
October 1, 2010 at 13:16:17
You don't have to thank me; just knowing it's useful is enough. Also, I've refined that trick a bit, because I apparently hate sub-functions.

@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b/s/a-d c:\files\*.doc') do (
  for %%p in ("%%~Pa.") do echo.copy "%%a" "d:\dest\%%~Np%%~Xa"
)

How To Ask Questions The Smart Way


Report •

#3
October 1, 2010 at 13:38:37
Hi R2,

Yeah, subs are not optimal.

A couple days ago I was working one with nested FORs and got so lost I forgot what I started out to do.

LOL


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
October 1, 2010 at 17:44:25
Thanks a ton to R2 and M2...you guys are genius...the script is working fine.
Can I get more higher parent levels rather than the immediate parent of a file which this script does?

C:\Reports\report\SERVER3\\folder\report.xml -----> SERVER3.xml

So in this case I dont want the new file to be folder.xml, rather SERVER3.xml which is a level above the imediate parent of the file...I actually want to go to the 3rd parent of the file..:)


Report •

#5
October 1, 2010 at 18:09:23
So in this path which folder do you want?

C:\Reports\report\SERVER3\folder


=====================================
Helping others achieve escape felicity

M2


Report •

#6
October 1, 2010 at 18:12:18
I will want to have "report" in the following path

C:\Reports\report\SERVER3\folder


Report •

#7
October 1, 2010 at 19:53:42
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b/s/a-d e:\bak\*.doc') do (
for /f "tokens=2 delims=\" %%f in ("%%~Pa") do (
echo copy "%%a" "d:\dest\%%f%%~Xa"
)
)


=====================================
Helping others achieve escape felicity

M2


Report •

#8
October 2, 2010 at 07:39:37
If you want to "go up" one more level, just replace "%%~Pa." with "%%~Pa.."

And if you want to just rename, just replace copy "%%a" "d:\dest\%%~Np%%~Xa" with ren "%%a" "%%~Np%%~Xa"

How To Ask Questions The Smart Way


Report •

#9
October 2, 2010 at 12:09:11
Thanks M2 and R2.. all what i could have ever asked for...Appreciate your help guys....Solved!

Report •

Ask Question