?
Solved

How do I remove special characters from multiple files/directories?

Posted on 2007-11-15
9
Medium Priority
?
3,288 Views
Last Modified: 2010-04-21
On a Windows machine, I have a folder containing several thousand files, with hundreds of subfolders.  Scattered throughout these are several files and folders that have characters such as '&' that cause problems on some applications.(In this instance, I'm trying to copy them into SharePoint, and it doesn't like them.)  What I would like to do is run a batch file to replace all of the '&' characters with "and", although I would settle for simply removing them completely.  I've looked around and found solutions for similar problems, but none of them quite fit my specific problem.

While a batch file seems the simplest solution, I'm not against using a free utility if it provides this functionality as well.  My searches online found numerous options that may have done the job, but they were generally either not free, or the description was so vague I couldn't tell if they could do the job or not.
0
Comment
Question by:Freeman-B
[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
  • 4
  • 4
9 Comments
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20292115
The enclosed script will do as you ask.
@echo off
 
setlocal ENABLEDELAYEDEXPANSION
 
if "%~1"=="" (
	echo # ERROR - supply the directory from which to begin the search for
	echo           filenames containing the ampersand symbol.  Any file
	echo           encountered with have the ampersand replaced by 'and'.
	goto :EOF
)
 
if not exist "%~1" (
	echo # ERROR - directory NOT found
	echo          = '%~1'
	goto :EOF
)
 
echo/
echo + Parsing "%~1", please wait ...
 
for /f "tokens=*" %%F in ('dir "%~1" /s/b') do (
	set fileNAME=%%F
	set newFILEname=!fileNAME:^&=and!
	if not "!newFILEname!"=="!fileNAME!" (
		for /f %%N in ("!newFILENAME!") do (
			echo   + renaming "!fileNAME!" to "!newFILEname!"
			ren "!fileNAME!" "%%~nxN"
			if not errorlevel 1 (
				echo     - SUCCESS
			) else (
				echo     # FAILED to rename "!fileNAME!"
			)
		)
	)
)
 
echo - COMPLETE

Open in new window

0
 
LVL 2

Author Comment

by:Freeman-B
ID: 20298022
Thanks, MSE-dwells.  That works in most cases.  As expected though, I ran into some situations where it has problems.  Apparently if there are commas in the file path, it causes problems with the rename command, truncating some of the file names.  Of course, the files I'm working with have a significant number of commas spread around as well.  Why does Windows allow you to use characters in the file names if they aren't going to work with some of their own software?  I've added that to the list of characters that I warned the users to never use in filenames again, upon pain of death.  I'm trying to remove the commas first, then go back and remove the ampersands, but meetings are slowing me down.  I think between your code and what I've done before, I may be able to figure it out.
0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20298041
Hmmm ... I should be able to deal with the commas.  I'll take a guess at the naming syntax for now but could you provide a severe example please.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 9

Accepted Solution

by:
MSE-dwells earned 800 total points
ID: 20298072
Don't worry about that earlier request, I found thr culprit -


@echo off
 
setlocal ENABLEDELAYEDEXPANSION
 
if "%~1"=="" (
	echo # ERROR - supply the directory from which to begin the search for
	echo           filenames containing the ampersand symbol.  Any file
	echo           encountered with have the ampersand replaced by 'and'.
	goto :EOF
)
 
if not exist "%~1" (
	echo # ERROR - directory NOT found
	echo          = '%~1'
	goto :EOF
)
 
echo/
echo + Parsing "%~1", please wait ...
 
for /f "tokens=*" %%F in ('dir "%~1" /s/b') do (
	set fileNAME=%%F
	set newFILEname=!fileNAME:^&=and!
	if not "!newFILEname!"=="!fileNAME!" (
		for /f "tokens=*" %%N in ("!newFILENAME!") do (
			echo   + renaming "!fileNAME!" to "!newFILEname!"
			ren "!fileNAME!" "%%~nxN"
			if not errorlevel 1 (
				echo     - SUCCESS
			) else (
				echo     # FAILED to rename "!fileNAME!"
			)
		)
	)
)
 
echo - COMPLETE

Open in new window

0
 
LVL 2

Author Closing Comment

by:Freeman-B
ID: 31409362
That did the trick, although I had to run it twice to complete it.  Looks like it renamed the directories, then tried to rename the files beneath them, but that's not a problem.  Then I modified it to get rid of the '#' symbol as well, since that was used many times as well.  Now, I'm working on getting rid of the temporary files that are still hanging around.  Who would have thought copying files would be this much trouble.

Thanks for the help!
0
 
LVL 2

Author Comment

by:Freeman-B
ID: 20298982
The above script worked fine.  You may have to run it twice if it changes directory names.(It can't find files below the new directory name)  In case anyone else is ever moving files into SharePoint, here are some things it doesn't like:

1.  '&'
2.  '#'
3.  Files that start with a blank space.
4.  Files that have two periods in a row. "blank..txt"
5.  Temporary files.(Anything starting with a '~')
6.  Me.(Okay, maybe it just started to feel like that)

That's all I've found so far, but the script above helped out with the worst problems.  I was able to delete the temporary files with a simple Windows search, and the others were fairly uncommon.
0
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20299038
That could also be rectified by making 2 passes, necessary?
0
 
LVL 2

Author Comment

by:Freeman-B
ID: 20299182
That's probably not necessary.  I've gotten almost all of the files that I was working on copied already.  For someone else, the number of passes would depend on how many directories and sub-directories had special characters that needed to be removed.  I think making one pass and reporting the failures should be more than sufficient for someone to determine the causes and whether it needs to be run multiple times.  At most, perhaps add a prompt at the end to see if the user would like to run it again, but even that isn't necessary.

Thanks again for the help.  I was fairly decent at writing scripts on a Solaris system at my last job, but I haven't had to do anything complicated in a batch file since the Windows 3.1 days.
0
 

Expert Comment

by:pbaxter2402
ID: 23602015
Any idea on how to expand this script to crawl subdirectories in that folder to do the same?

Thanks in advance.

P.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

743 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