rename files containing special characters

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

billrich
Posts: 70
Joined: 24 Apr 2012 05:36
Location: USA

rename files containing special characters

#1 Post by billrich » 08 Jul 2012 17:20

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>


Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: rename files containing special characters

#2 Post by Liviu » 08 Jul 2012 19:46

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."

Don't know who CH is ;-) but the question is way too vague.

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


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

Re: rename files containing special characters

#3 Post by foxidrive » 09 Jul 2012 00:08

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?


billrich
Posts: 70
Joined: 24 Apr 2012 05:36
Location: USA

Re: rename files containing special characters

#4 Post by billrich » 10 Jul 2012 23:35

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>


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

Re: rename files containing special characters

#5 Post by Squashman » 11 Jul 2012 05:51

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.


billrich
Posts: 70
Joined: 24 Apr 2012 05:36
Location: USA

Re: rename files containing special characters

#6 Post by billrich » 11 Jul 2012 07:07

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.


billrich
Posts: 70
Joined: 24 Apr 2012 05:36
Location: USA

Re: rename files containing special characters

#7 Post by billrich » 11 Jul 2012 07:20

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.


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

Re: rename files containing special characters

#8 Post by Squashman » 11 Jul 2012 07:31

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.


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

Re: rename files containing special characters

#9 Post by foxidrive » 11 Jul 2012 07:42

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?


billrich
Posts: 70
Joined: 24 Apr 2012 05:36
Location: USA

Re: rename files containing special characters

#10 Post by billrich » 11 Jul 2012 10:12

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?


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

Re: rename files containing special characters

#11 Post by foxidrive » 11 Jul 2012 10:45

Thanks, but Microsoft don't pay me enough. ;)

Note that you can't replace these characters easily using that technique = ~ %


Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: rename files containing special characters

#12 Post by Liviu » 11 Jul 2012 18:28

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


billrich
Posts: 70
Joined: 24 Apr 2012 05:36
Location: USA

Re: rename files containing special characters

#13 Post by billrich » 11 Jul 2012 19:04

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.



Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: rename files containing special characters

#15 Post by Liviu » 11 Jul 2012 21:29

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