rename files containing special characters
Moderator: DosItHelp
rename files containing special characters
CH wrote:
"I do not know bat commands and I need to go through a folder structure and recursevly
find all files and rename them, I mean if the file name has special characters
( ?<>@#$ also space) I need to rename them into an underscore."
C:\test>type 2oldnew.bat
@echo off
echo newname > newname.txt
echo.
echo. > one.txt
setlocal enabledelayedexpansion
echo.
rem char.txt =( ?<>@#$ also space) I need to rename them into an underscore."
for /f %%i in (char.txt) do (
rem echo i=%%i
findstr %%i oldname.txt > one.txt
set /p str=<one.txt
set str=!str:%%i=_!
echo !str! >> newname.txt
)
findstr one oldname.txt > one.txt
set /p str=<one.txt
set str=%str: =_%
echo %str% >> newname.txt
echo.
type oldname.txt
echo.
type newname.txt
rem use copy, rename or move.
rem copy is safest until procedure works for you
C:\test>
C:\test>2oldnew.bat
file one.txt
file#two.txt
file?four.txt
file<five.txt
file>six.txt
file@seven.txt
newname
file_two.txt
file_four.txt
file_five.txt
file_six.txt
file_seven.txt
file_one.txt
C:\test>
"I do not know bat commands and I need to go through a folder structure and recursevly
find all files and rename them, I mean if the file name has special characters
( ?<>@#$ also space) I need to rename them into an underscore."
C:\test>type 2oldnew.bat
@echo off
echo newname > newname.txt
echo.
echo. > one.txt
setlocal enabledelayedexpansion
echo.
rem char.txt =( ?<>@#$ also space) I need to rename them into an underscore."
for /f %%i in (char.txt) do (
rem echo i=%%i
findstr %%i oldname.txt > one.txt
set /p str=<one.txt
set str=!str:%%i=_!
echo !str! >> newname.txt
)
findstr one oldname.txt > one.txt
set /p str=<one.txt
set str=%str: =_%
echo %str% >> newname.txt
echo.
type oldname.txt
echo.
type newname.txt
rem use copy, rename or move.
rem copy is safest until procedure works for you
C:\test>
C:\test>2oldnew.bat
file one.txt
file#two.txt
file?four.txt
file<five.txt
file>six.txt
file@seven.txt
newname
file_two.txt
file_four.txt
file_five.txt
file_six.txt
file_seven.txt
file_one.txt
C:\test>
Re: rename files containing special characters
Don't know who CH isbillrich wrote:CH wrote:
"I do not know bat commands and I need to go through a folder structure and recursevly find all files and rename them, I mean if the file name has special characters ( ?<>@#$ also space) I need to rename them into an underscore."

For one thing, "?<>" are not valid in Windows pathnames to begin with, so if you see those characters in a filename then there must be some deeper issues left untold. Also, "@#" are not "special" as far as cmd is concerned, while other characters like "%!^*~=" may be "special" in batch-land, depending on usage. Lastly, it's not always possible to do what you ask - suppose for example two files "@.txt" and "#.txt", they can't possibly be both renamed to "_.txt".
For some general discussion of special characters find/replace in batch code, see for example viewtopic.php?f=3&t=2710, viewtopic.php?f=3&t=1485.
Liviu
Re: rename files containing special characters
billrich wrote:CH wrote:
"I do not know bat commands and I need to go through a folder structure and recursevly
find all files and rename them, I mean if the file name has special characters
( ?<>@#$ also space) I need to rename them into an underscore."
Do you mean to say that you want all the filenames to use alpha and numeric characters only, and to replace every character that is not alpha or numeric with an underscore?
if you have "dog123#.txt" and "dog123$.txt" then one of the renames will fail because you can only have one "dog123_.txt" file. Have you thought of that?
Re: rename files containing special characters
Powershell by Computer Hope Sidewinder:
Get-ChildItem -Path c:\test -recurse | Where-Object {
-not $_.psIsContainer } | ForEach-Object {
$newName = $_.Name -replace "\Where-Object", "_" `
-replace "<", "_" `
-replace ">", "_" `
-replace "@", "_" `
-replace "#", "_" `
-replace " ", "_"
Rename-Item -Path $_.Fullname -NewName $newName
}
Error:
C:\test> powershell side7912.ps1
The term 'side7912.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the pat
h is correct and try again.
At line:1 char:13
+ side7912.ps1 <<<<
+ CategoryInfo : ObjectNotFound: (side7912.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
C:\test>
Get-ChildItem -Path c:\test -recurse | Where-Object {
-not $_.psIsContainer } | ForEach-Object {
$newName = $_.Name -replace "\Where-Object", "_" `
-replace "<", "_" `
-replace ">", "_" `
-replace "@", "_" `
-replace "#", "_" `
-replace " ", "_"
Rename-Item -Path $_.Fullname -NewName $newName
}
Error:
C:\test> powershell side7912.ps1
The term 'side7912.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the pat
h is correct and try again.
At line:1 char:13
+ side7912.ps1 <<<<
+ CategoryInfo : ObjectNotFound: (side7912.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
C:\test>
Re: rename files containing special characters
billrich wrote:Powershell by Computer Hope Sidewinder:
This is a BATCH file forum. Not powershell. If you want help with your PowerShell script then go back to the Computer Hope Website and ask SideWinder to fix it for you.
Re: rename files containing special characters
Sqashman wrote:
"This is a BATCH file forum. Not powershell. If you want help with the Sidewinder PowerShell script then go back to the Computer Hope Website and ask SideWinder to fix it for you."
ComputerHope.com is a BATCH file forum and neither forum knows how to fix powershell or special characters in file names.
"This is a BATCH file forum. Not powershell. If you want help with the Sidewinder PowerShell script then go back to the Computer Hope Website and ask SideWinder to fix it for you."
ComputerHope.com is a BATCH file forum and neither forum knows how to fix powershell or special characters in file names.
Re: rename files containing special characters
FoxDrive wrote:
"if you have "dog123#.txt" and "dog123$.txt" then one of the renames will fail because you can only have one "dog123_.txt" file. Have you thought of that?"
That is an easy fix. Add a number with the underscore "dog123_88.txt" "dog123_99"
Thanks for your code.
"if you have "dog123#.txt" and "dog123$.txt" then one of the renames will fail because you can only have one "dog123_.txt" file. Have you thought of that?"
That is an easy fix. Add a number with the underscore "dog123_88.txt" "dog123_99"
Thanks for your code.
Re: rename files containing special characters
billrich wrote:Sqashman wrote:
"This is a BATCH file forum. Not powershell. If you want help with the Sidewinder PowerShell script then go back to the Computer Hope Website and ask SideWinder to fix it for you."
ComputerHope.com is a BATCH file forum and neither forum knows how to fix powershell or special characters in file names.
Computer Hope is more than just a Batch File forum. There are dozens of categories to post in over there. This Website forum is strictly Batch Files.
Re: rename files containing special characters
foxidrive wrote:Do you mean to say that you want all the filenames to use alpha and numeric characters only, and to replace every character that is not alpha or numeric with an underscore?
Is this what you want to do??
Will any filenames have % in them?
I presume the filename extensions will all be ok as they are, is that so?
Re: rename files containing special characters
foxidrive wrote:foxidrive wrote:Do you mean to say that you want all the filenames to use alpha and numeric characters only, and to replace every character that is not alpha or numeric with an underscore?
Is this what you want to do??
Will any filenames have % in them?
I presume the filename extensions will all be ok as they are, is that so?
C:\test>type foxi.bat
Code: Select all
@echo off
cd schar
for /f "delims=" %%a in ('dir /a:-d /o:n /b /s') do call :next "%%a"
GOTO:EOF
:next
set "newname=%~nx1"
set "newname=%newname: =_%"
set "newname=%newname:)=_%"
set "newname=%newname:(=_%"
set "newname=%newname:&=_%"
set "newname=%newname:^=_%"
set "newname=%newname:$=_%"
set "newname=%newname:#=_%"
set "newname=%newname:@=_%"
set "newname=%newname:!=_%"
set "newname=%newname:-=_%"
set "newname=%newname:+=_%"
set "newname=%newname:}=_%"
set "newname=%newname:{=_%"
set "newname=%newname:]=_%"
set "newname=%newname:[=_%"
set "newname=%newname:;=_%"
set "newname=%newname:'=_%"
set "newname=%newname:`=_%"
set "newname=%newname:,=_%"
echo ren %1 "%newname%
C:\test>foxi.bat
ren "C:\test\schar\file one.txt" "file_one.txt
ren "C:\test\schar\file#two.txt" "file_two.txt
ren "C:\test\schar\file$three.txt" "file_three.txt
Excellent code. Foxidrive works for Microsoft?
Re: rename files containing special characters
Thanks, but Microsoft don't pay me enough. 
Note that you can't replace these characters easily using that technique = ~ %

Note that you can't replace these characters easily using that technique = ~ %
Re: rename files containing special characters
foxidrive wrote:Note that you can't replace these characters easily using that technique = ~ %
Also * unless one considers http://stackoverflow.com/questions/7022640/string-substitution-asterisks-in-batch-files overly easy

Now, I am well aware that * is not allowed in filenames, but the OP never made it completely clear what "names" needed be handled, and the original list included other characters like ?<> which are illegal in pathnames (though ?* are valid in wildcards).
Liviu
Re: rename files containing special characters
Liviu wrote:foxidrive wrote:Note that you can't replace these characters easily using that technique = ~ %
Also * unless one considers http://stackoverflow.com/questions/7022640/string-substitution-asterisks-in-batch-files overly easy
Now, I am well aware that * is not allowed in filenames, but the OP never made it completely clear what "names" needed be handled, and the original list included other characters like ?<> which are illegal in pathnames (though ?* are valid in wildcards).
Liviu
I suspect the original poster was computerhope.com staff. There was only the one post by the original poster.
The thread at computerhope.com was a spoof testing objections you raise.
I treated the names as strings and not filenames.
The code by foxidrive did work on certain filenames that appeared to have a special character in the file name.
Re: rename files containing special characters
Double post. Sorry.
I blame my mouse?
I blame my mouse?
Re: rename files containing special characters
billrich wrote:The code by foxidrive did work on certain filenames that appeared to have a special character in the file name.
Surely must have, and foxidrive was helpful as usual - including his note on the limitations of the method. I only added to the latter, did not mean any negative connotation. It's always good practice to state/know what's covered vs. what's not. Your original question was vague enough (as to which specific characters) to leave a lot to the imagination. Even now, the other issue of name clashes is not quite settled - for example if you had two files ";-).txt" and "%-(.txt" they'd both compete for the same "___.txt" new name.
Liviu