Solved

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

Posted on 2010-11-24
18
3,441 Views
Last Modified: 2012-05-10
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.
0
Comment
Question by:megnin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
  • +3
18 Comments
 
LVL 7

Expert Comment

by:celazkon
ID: 34209787
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
 
LVL 56

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 34209804
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
 
LVL 70

Expert Comment

by:Qlemo
ID: 34209891
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 56

Expert Comment

by:Bill Prew
ID: 34209948
In my test here it wasn't needed Qlemo.

~bp
0
 
LVL 1

Author Comment

by:megnin
ID: 34210152
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
 
LVL 1

Author Comment

by:megnin
ID: 34210156
billprew and Qlemo, I'll give that a try.  Thank you.
0
 
LVL 1

Author Comment

by:megnin
ID: 34212518
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
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 125 total points
ID: 34212842

@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
 
LVL 56

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 34213960
==> 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
 
LVL 56

Expert Comment

by:Bill Prew
ID: 34213966
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
 
LVL 13
ID: 34214073
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
 
LVL 56

Expert Comment

by:Bill Prew
ID: 34214141
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
 
LVL 13
ID: 34214199
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
 
LVL 1

Author Comment

by:megnin
ID: 34215035
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 34224015
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
 
LVL 11

Accepted Solution

by:
paultomasi earned 125 total points
ID: 34224083
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
 
LVL 1

Author Closing Comment

by:megnin
ID: 34282722
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 34284207
Thank you...
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question