Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Replace a character or symbol from a file name

Posted on 2008-06-17
8
Medium Priority
?
2,933 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
[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
  • 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
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.

715 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