Batch file to rename 1500 files, replacing "%20" with a space.

I have to remote into a computer running Windows XP and ***without installing any software*** rename about fifteen hundred files that accidentally got renamed when I unzipped an archive. The files had spaces in the names, but the spaces were replaced with "%20".  I need to rename them, replacing the %20 with a space.  The file names are of various lengths, some have two words some more, i.e. "Document%20Management.xml" or "Pie%20Chart%20Color%20Scheme.xml".  The should be "Document Management.xml" and "Pie Chart Color Scheme.xml".

My first thought was PowerShell, but this is a Windows XP computer and I CAN NOT install any new utility or software under threat of unemployment (so please don't send me a freaking link to Bulk Rename Utility, I have it, but I can't use it on the computer I have to remote into to rename the files and it's not on that computer).  I have to use what already exists on that corporate Win XP box.  So it looks like DOS Batch is what I have to work with.

Thank you very much for any help.
LVL 1
megninAsked:
Who is Participating?
 
Paul TomasiCommented:
In fact, we can drop the source folder altogether and plop the batch file into the folder containing the .XML files and run it straight from their... See below!

I have removed the double-quote from around *.xml and !file! . I would normally leave include them as a precaution however, due to the nature of the question they are not necessary!

NOTE: Not a single character is wasted!



@echo off
setlocal enabledelayedexpansion
for %%a in (*.xml) do (
   set file=%%a
   ren !file! "!file:%%20= !"
)



I was tempted to change:

   ren !file! "!file:%%20= !"

to just:

   ren !f! "!f:%%20= !"

but that's going a bit too far!


PS. I'm not going to split hairs over the ren !file! and ren %%a thing...
0
 
celazkonCommented:
Hi, do you have totalcmd on the machine? If not, this can be downloaded as a portable app and it can bulk rename file names by patterns easily & via gui (www.ghisler.com)
Hope it can help you, good luck
0
 
Bill PrewCommented:
This should get the job done.

~bp

@echo off
setlocal EnableDelayedExpansion
set BaseDir=c:\temp\EE26637787
for %%A in ("%BaseDir%\*.*") do (
  set NewName=%%~nxA
  set NewName=!NewName:%%20= !
  ren "%%A" "!NewName!"
)

Open in new window

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Not quite certain, but shouldn't line 7 sound
   ren "%%~A" "!NewName!"

Open in new window

? BTW, a newline after the [code] tag is responsible for the empty line 9.
0
 
Bill PrewCommented:
In my test here it wasn't needed Qlemo.

~bp
0
 
megninAuthor Commented:
celazkon: Please re-read the part of my original question that's in all caps.  Being "portable" does not exclued it from being forbiden.
0
 
megninAuthor Commented:
billprew and Qlemo, I'll give that a try.  Thank you.
0
 
megninAuthor Commented:
bp, that works great.  Thank you.

To modify it to replace spaces with an underscore, would this be correct? I'm not sure what needs to be in quotes or what needs to have a "%" before it.

@echo off  
setlocal EnableDelayedExpansion  
set BaseDir=C:\MyTestFolder  
for %%A in ("%BaseDir%\*.*") do (  
  set NewName=%%~nxA  
  set NewName=!NewName:%" "= !  
  ren "%%A_!NewName!"  
)
REM:  This works great.  Thank you!
@echo off  
setlocal EnableDelayedExpansion  
set BaseDir=C:\MyTestFolder  
for %%A in ("%BaseDir%\*.*") do (  
  set NewName=%%~nxA  
  set NewName=!NewName:%%20= !  
  ren "%%A" "!NewName!"  
)

Open in new window

0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:

@echo off  
setlocal EnableDelayedExpansion  
set BaseDir=C:\MyTestFolder  
for %%A in ("%BaseDir%\*.*") do (  
  set NewName=%%~nxA  
  set NewName=!NewName:%%20=_!  
  ren "%%A" "!NewName!"  
)

Open in new window

0
 
Bill PrewCommented:
==> To modify it to replace spaces with an underscore, would this be correct?

Looks like Qlemo responded to this, but I'll add some explanation in case that helps.

One powerful syntax of batch files is the SET command.  If you do a SET /? at a command line you'll see a lot of information of the command and a number of capabilities.  You can even do some simple math with the /A option.

The capability we are interested in for this question is the "find and replace" capability.  In the help text it states:

Environment variable substitution has been enhanced as follows:

    %PATH:str1=str2%

would expand the PATH environment variable, substituting each occurrence
of "str1" in the expanded result with "str2".  "str2" can be the empty
string to effectively delete all occurrences of "str1" from the expanded
output.  "str1" can begin with an asterisk, in which case it will match
everything from the beginning of the expanded output to the first
occurrence of the remaining portion of str1.

So when we wanted to replace %20 in a string with a space we did:

set NewName=!NewName:%%20= !

Since we were doing this inside a FRO loop we needed to use delayed variable expansion, so the reference to NewName became !NewName! rather than %NewName%.  Then we wanted to use the search and replace capability of the SET, so we used this syntax:

set NewName=!NewName:old=new!

Where old=%20 and new = a space.

The only other interesting part of this statement is that since %20 had a % sign in it, and that is a special character in batch scripts, we had to replace the % with a %% so that it would be processed as a single % character, rather than a "special action" character in the script.

Hope this helps,

~bp
0
 
Bill PrewCommented:
And one more item.  If you truly wanted to replace spaces with underscores, rather than the %20 with an underscore then it would be:

 set NewName=!NewName: =_!  

~bp
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Not looking for the pound just putting my two cents in:

Line 7 is unneeded;  combine it with line 8.

Endlocal should be used with a setlocal.

See attached code below:


REM: This works great. Thank you!
@ECHO OFF
setlocal EnableDelayedExpansion
set "BaseDir=C:\MyTestFolder"
For %%A in ("%BaseDir%\*.*") do (
 set NewName=%%~nxA
 ren "%%A" "!NewName:%%20=_!"
)
Endlocal

Open in new window

0
 
Bill PrewCommented:
Hello QCubed,

==> Line 7 is unneeded;  combine it with line 8.

Agreed, but I often do this purposefully when supplying solutions here on EE, since I think a few extra lines of script can often help the poster better understand the logic.

==> Endlocal should be used with a setlocal.

Based on the following from the SETLOCAL help info, I think a SETLOCAL at the end of a batch script is somewhat redundant.

Begins localization of environment changes in a batch file.  Environment
changes made after SETLOCAL has been issued are local to the batch file.
ENDLOCAL must be issued to restore the previous settings.  When the end
of a batch script is reached, an implied ENDLOCAL is executed for any
outstanding SETLOCAL commands issued by that batch script.
~bp
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Good to know.  Still I recall a problem where the batch script was called by another and ran into erors because the inner script did not use endlocal.  Probably not going to run into that very often.
0
 
megninAuthor Commented:
billprew and QCubed, than you both for the very good explanations.  It's a lot to digest.  No pun intended (Happy Thanksgiving!)  ;-)

For such a short batch file it looks like some seriously advanced stuff!  ;-)  It seems not a single character is not doing it's share of work.

I'm too full to concentrate right now, so I'm going to have to look at this some more tomorrow.  ;-)
0
 
Paul TomasiCommented:
My only real gripe is the variable name 'NewName'. It's quite obvious at the time '%%~nxA' is assigned, it should actually be 'CurrentFile' (or just 'File').

Also, if the batch file is a one-off, then I personally would drop the 'set BaseDir...' thing and hardcode the source path straight into the FOR statement itself.

Furthermore, the author draws attention to just .XML files therefore, if the only files that require renaming are .XML files then personally I would specify this instead of '*.*'.

Lastly, I don't think you need to specify the '~nx' bit in '%%~nxA'.

Please see the modified (and quite possibley, improved) code below:




@echo off
setlocal enabledelayedexpansion
for %%a in ("c:\source\*.xml") do (
   set file=%%a
   ren "!file!" "!file:%%20= !"
)




Only NOW can the author say "It seems not a single character is not doing it's share of work".


Obviously, for the batch file to work, the author will need to replace 'c:\source\*.xml' with whatever path and filespec is appropriate in his case.


NOTE; You may notice the change from '"!file:%%20=_!"' to '"!file:%%20= !"' thereby returning '%20's back to their original spaces again, instead of underscores. This is ABSOLUTELY necessary in order for any embedded hypertext links to work correctly (assuming the files reference each other).
0
 
megninAuthor Commented:
Thank you all for the good solutions.  I'm sorry for my delay; I took a week of vacation after Thanksgiving.
This was actually a good primer for me in learning more advanced patch file programming.  Thanks again!
0
 
Paul TomasiCommented:
Thank you...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.