P. Grunder P. Grunder - 3 months ago 14
PowerShell Question

Changing Multiple Filenames in multiple Folders with Batch

I have several Folder which contain from 5 to 20 Files with all different names. They get replaced every week, so the Name of each File also changes. But i Need them to have specific Names so i can upload them by using my SQL loader.

Is there a way to create a Batch file, which goes into every Folder that i specify, select all Files and changes all the names? Perfect Solution would just be a upcounting number like: file1.xml, file2.xml etc.

Since im a total newbie to Batch i searched around a bit and found following code, but it only changes the files in 1 specific Folder.

Dir *.xml | ForEach-Object -begin { $count=1 }
-process { rename-item $_ -NewName "$count.xml"; $count++ }


Update 1
I found the working code which allows me to rename the files in a Folder as i want them to be. I would just Need a code, that allows me to do this to several other Folders at the same time or automatically one after another.

@echo off & setlocal EnableDelayedExpansion

set a=1
for /f "delims=" %%i in ('dir /b *.xml') do (
ren "%%i" "!a!.xml"
set /a a+=1
)

Answer
@echo off & setlocal EnableDelayedExpansion 

set a=1

rem make old name to prevent same name collision
for /D %%i in (*) do (
    cd %%i
    for /f "delims=" %%j in ('dir /b *.xml') do (
        ren "%%j" "%%j-old.xml" 
    ) 
    cd ..
)

rem rename process
for /D %%i in (*) do (
    cd %%i
    for /f "delims=" %%j in ('dir /b *.xml') do (
        ren "%%j" "!a!.xml" 
        set /a a+=1
    ) 
    cd ..
)

echo Done
pause
Start .

hope it helps.

Comments