Solved

Replace a character or symbol from a file name

Posted on 2008-06-17
8
2,925 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 44

Expert Comment

by:scrathcyboy
ID: 21805943
0
 
LVL 83

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 50 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 83

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 83

Accepted Solution

by:
oBdA earned 450 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
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…
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.

773 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