Page 1 of 1
Batch file to Rename files to their file path
Posted: 09 May 2013 20:50
by Ruu
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
Re: Batch file to Rename files to their file path
Posted: 10 May 2013 04:11
by foxidrive
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%
Re: Batch file to Rename files to their file path
Posted: 10 May 2013 10:01
by !k
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
Re: Batch file to Rename files to their file path
Posted: 10 May 2013 13:47
by Endoro
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
Re: Batch file to Rename files to their file path
Posted: 16 Mar 2014 09:08
by Samir
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.
Re: Batch file to Rename files to their file path
Posted: 16 Mar 2014 18:13
by Squashman
xxcopy does a good job of flattening folder structures.
Re: Batch file to Rename files to their file path
Posted: 27 Mar 2014 08:12
by Samir
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.