Solved

Replace a character or symbol from a file name

Posted on 2008-06-17
8
2,924 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
 
LVL 83

Expert Comment

by:oBdA
ID: 21806164
Indeed it should; thanks.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Windows 7 does not have the best desktop search built in. This is something Windows 7 users have struggled with. You type something in, and your search results don’t always match what you are looking for, or it doesn’t actually work at all. There ar…
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.

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now