Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Replace a character or symbol from a file name

Posted on 2008-06-17
8
Medium Priority
?
2,938 Views
Last Modified: 2012-06-27
Hi I need a batch file (bat) that can look at a directory d:\test and search for all files names that contain # symbol and simply remove them from the file names.

Any help would be appreciated.

Thanks
0
Comment
Question by:proudpaki9
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 85

Expert Comment

by:oBdA
ID: 21806007
Try this; note that the script is currently in test mode, it will only display the "ren" commands it would otherwise run.
When you want to run it for real, remove the capitalized ECHO in front of 'ren "%Folder%\%OldName%" "%NewName%"'

@echo off
set Folder=D:\Test
for /f "delims=" %%a in ('dir /b /a:-d "%Folder%\*.*" | find "#"') do call :process "%%a"
goto :eof
:process
set OldName=%~1
set NewName=%OldName:#=%
echo Processing %OldName% ...
ECHO ren "%Folder%\%OldName%" "%NewName%"
goto :eof

Open in new window

0
 
LVL 33

Assisted Solution

by:knightEknight
knightEknight earned 200 total points
ID: 21806108
line 3 above should be:

for /f "delims=" %%a in ('dir /b /a:-d "%Folder%\*.*" ^| find "#"') do call :process "%%a"
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 85

Expert Comment

by:oBdA
ID: 21806164
Indeed it should; thanks.
0
 

Author Comment

by:proudpaki9
ID: 21806393
obda:

I tried the following but nothing happens the window disappeared right away nor it removed the # from the files after removing the ECHO from the code.

@echo off
set Folder=c:\test
for /f "delims=" %%a in ('dir /b /a:-d "%Folder%\*.*" ^| find "#"') do call :process "%%a""
goto :eof
:process
set OldName=%~1
set NewName=%OldName:#=%
echo Processing %OldName% ...
ren "%Folder%\%OldName%" "%NewName%"
goto :eof
0
 
LVL 85

Accepted Solution

by:
oBdA earned 1800 total points
ID: 21806548
Okay, that seems to be a bug in the "%~1" expansion (which should strip quotes around the parameter %1). Interestingly enough, when a # is in the file name, the last quote isn't removed anymore.
Btw.: when testing batch scripts, it's better to run them from a command window, because that way, you'll see errors produced by the script.

@echo off
set Folder=c:\test
for /f "delims=" %%a in ('dir /b /a:-d "%Folder%\*.*" ^| find "#"') do call :process "%%a""
goto :eof
:process
set OldName=%1
set OldName=%OldName:"=%
set NewName=%OldName:#=%
echo Processing %OldName% ...
ECHO ren "%Folder%\%OldName%" "%NewName%"
goto :eof

Open in new window

0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 21806569
that's why I directed you to programs -- in my experience, command or DOS scripts will not properly handle characters like # in file names, as well as "illegal" characters.   But the GUI will -- hence use a program.
0
 

Author Comment

by:proudpaki9
ID: 21806654
worked great.

thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

916 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