Adding folder name to filenames

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mcranmer
Posts: 15
Joined: 20 Feb 2012 10:45

Adding folder name to filenames

#1 Post by mcranmer » 27 Mar 2012 05:41

I have a large number of folders each containing a number of text files. The folders have generic names like 'UD' or 'KX'.

I want to add the name of each folder to the start of all filenames of files within the folder.

so files:

filename1
filename2
filename3

in folder KX

should be renamed:

KX_filename1
KX_filename2
KX_filename3


thanks,

mark

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Adding folder name to filenames

#2 Post by foxidrive » 27 Mar 2012 06:41

Run this, it will pause after each folder and show you the commands it would use for that folder.

If it looks right then remove the 2nd echo and the pause and rerun it.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir /ad /b /s') do (
echo ==="%%a"===
pushd "%%a"
for /f "delims=" %%a in ('dir /a-d /b') do echo ren "%%b" "%%~nxa_%%b"
popd
pause
)

Post Reply