MS Dos: How to deal with an em dash in a filename

March 26, 2014 at 21:04:36
Specs: Windows Server 2003

On a PC with Windows 7, I'm using a simple DOS script to rename some Excel files, pre-pending their parent folder name:

for /f "delims=" %%i in ('dir /b /AD') do (
cd "%%i"
for /f "delims=" %%j in ('dir /b *.xl*') do ren "%%~j" "%%i_%%~j"

I noticed that some files generated an error, "System cannot find the specified file." These files all had an em dash (—) in the filename. After I changed the em dash to a regular hyphen (-), using Windows Explorer, the script worked fine on these files.

How can I help MS Dos recognize and rename these files?

See More: MS Dos: How to deal with an em dash in a filename

Report •

March 27, 2014 at 02:21:13
Use the ren command for rename.

Report •

March 27, 2014 at 06:38:45
Help me out here. What is an em dash?

M2 Golden-Triangle

Report •

March 27, 2014 at 16:41:18
This should explain it. Normally the only people that are concerned with em and en dashes are type setters


message edited by StuartS

Report •

Related Solutions

March 27, 2014 at 19:07:24
On my system, it's char. 151, so this might help:
for /f "tokens=1* delims=ù" %%a in ('dir /b /a-d *ù*') do ren "%%aù%%b" "%%a-%%b"

not tested, use at own risk and with caution.

message edited by nbrane

Report •

March 28, 2014 at 11:50:39
Thanks for answering. I may have misrepresented my intention, I actually want to keep the em dash and just prepend the folder name. With some help, I figured out how to do it:

message edited by alhambra

Report •

March 28, 2014 at 13:21:07
Note that there is a difference in behavior between "for (*)" and "for /f ('dir /b')". Namely, the former can lead to a race condition, where you'd end up renaming the files multiple times. As the latter enumerates then acts, the race condition is avoided. If you're having trouble, I suggest a language with Unicode support. I think one of the service packs for Win2003 added powershell, so you can either use that, or VBScript. VBScript's execution is probably the fastest of the 3 Windows native scripting languages.

How To Ask Questions The Smart Way

Report •

March 29, 2014 at 06:30:25
Yes, and I figured out a trick to avoid a race condition using "for (*)" - make a temporary folder to move the file into after renaming, then, when the loop is done, move the files back and delete the temporary directory.

Report •

Ask Question