computing
  • 7

MS Dos: How To Deal With An Em Dash In a Filename

  • 7

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”
cd..
)

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?

Share

1 Answer

  1. 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

    • 0