Solved

Replace a character or symbol from a file name

Posted on 2008-06-17
8
2,923 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Windows 10 is here and for most admins this means frustration and challenges getting that first working Windows 10 image. As in my previous sysprep articles, I've put together a simple help guide to get you through this process. The aim is to achiev…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

14 Experts available now in Live!

Get 1:1 Help Now