Reputation: 3
I am trying to write a batch file to rename many files with a #
sign in the filename to be replaced with No.
.
i.e. *#*.*
to *No.*.*
Examples are GT #3.doc
to GT No. 3.doc
all files have differing file extensions. i.e. .txt
, .doc
, .docx
, .xls
, .dat
, .mdb
, etc...
Also, these files are not in one folder but are in many folders and sub-folders.
OS is Win 7. I look forward to the help as I am a novice with batch files and there are hundreds of files to be renamed for moving to a cloud solution that does not accept symbols. Thank you.
Upvotes: 0
Views: 2251
Reputation: 155
Thanks aschipfl :)
You mean like this?
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "ROOT=D:\path\to\root\folder\"
set "PATTERN=version.exe"
set "SEARCH=version"
set "REPLAC=v"
for /R "%ROOT%" %%I in ("%PATTERN%") do (
set "FILENAME=%%~nI"
setlocal EnableDelayedExpansion
set "FILENAME=!FILENAME:%SEARCH%=%REPLAC%!"
ren "%%~fI" "!FILENAME!%~xI"
endlocal
)
endlocal
exit /B
And to rename also all folders and all nested subfolders containing the word "version" to change to "v"?
Upvotes: 0
Reputation: 34899
This batch-file should do it:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "ROOT=D:\path\to\root\folder\"
set "PATTERN=*#*.*"
set "SEARCH=#"
set "REPLAC=No. "
for /R "%ROOT%" %%I in ("%PATTERN%") do (
set "FILENAME=%%~nI"
setlocal EnableDelayedExpansion
set "FILENAME=!FILENAME:%SEARCH%=%REPLAC%!"
ren "%%~fI" "!FILENAME!%~xI"
endlocal
)
endlocal
exit /B
Upvotes: 0
Reputation: 70
I forgot how to use batch few years ago ever since powershell came out. Powershell has an easy way of doing this.
$target = "Your Path"
Get-ChildItem -path $target -Recurse -Include *#* | rename-item -NewName { $_.name -replace '#','NO'}
-recurse will go through all folders and -include will filter files with # in it and pipe it to rename-item. Rename-item has a replace function that only replaces specified part with whatever you'd like.
Upvotes: 0