Batch file to Rename files to their file path

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ruu
Posts: 1
Joined: 09 May 2013 20:36

Batch file to Rename files to their file path

#1 Post by Ruu » 09 May 2013 20:50

Hi,

I am new to Batch script.
What I am trying to do is rename files.

For example, a file’s full path is

C:\testb\first.txt

I want to change this file name “first.txt” to “C_testb_first.txt” OR “testb_first.txt”
And do this rename for all files in this folder “testb” and all files in subdirectory also.

Can anyone help me please?

Regards
Ruu

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

Re: Batch file to Rename files to their file path

#2 Post by foxidrive » 10 May 2013 04:11

Remove the echo if the ren command does what you want.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir /a-d /b /s') do call :next "%%a"
pause
goto :EOF
:next
set "name=%~1"
set "name=%name::=%"
set "name=%name:\=_%"
echo ren "%~1" "%name%

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Batch file to Rename files to their file path

#3 Post by !k » 10 May 2013 10:01

Ruu wrote:OR “testb_first.txt”

Code: Select all

@echo off
setlocal enableextensions

for /f "delims=" %%a in ('dir /a-d /b /s') do call :next "%%a"
pause
goto :EOF

:next
set "name=%~1"
set "name=%name:\=_%"
echo ren "%~1" "%name:~3%
goto :EOF

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Batch file to Rename files to their file path

#4 Post by Endoro » 10 May 2013 13:47

please note:
  • the call:function "%file name%" code doesn't work with files with % in the name
  • if delayed expansion is enabled the code also doesn't work with files with ! or ^ in the name

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Batch file to Rename files to their file path

#5 Post by Samir » 16 Mar 2014 09:08

foxidrive wrote:Remove the echo if the ren command does what you want.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir /a-d /b /s') do call :next "%%a"
pause
goto :EOF
:next
set "name=%~1"
set "name=%name::=%"
set "name=%name:\=_%"
echo ren "%~1" "%name%
Love this! Saved us A lot of grief over renaming a couple of hundred files. Thank you!

We modified this batch by changing ren to move since ren left the file in the same directory and we wanted to flatten the directory structure since the resulting filenames included the directory structure in the filenames.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch file to Rename files to their file path

#6 Post by Squashman » 16 Mar 2014 18:13

xxcopy does a good job of flattening folder structures.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Batch file to Rename files to their file path

#7 Post by Samir » 27 Mar 2014 08:12

Squashman wrote:xxcopy does a good job of flattening folder structures.
That it does, but since we needed to rename the file with the path included, this batch worked great.

Post Reply