I’ve just hit again the following issue: I always liked to use spaces in the files names, while simple command line operations require special handling for such file names.
For a particular task, I had to use a set of test files and I’ve named them “Query CHM – 1.txt”, “Query CHM – 2.txt” and so on.
A batch file was supposed to iterate these test files and launch some other application with the path to these files on the command line. The batch file was supposed to be simple:
foreach %%i in (*.txt) do call LaunchProtem.bat %%i
Well, not quite. The launched application complained about not being able to open a file with the path E:\projects\sax\7-txt\MultipleQueriesAndOrders\TestFiles\Query.
It took me a few good minutes to trace the error: first, I’ve added the quotes to the command line:
foreach %%i in (*.txt) do call LaunchProtem.bat “%%i”
BTW: if you want to use foreach, you may have to also use a pushd command first, if the files to be enumerated are in a different directory. Yet another reason to bite the bullet and learn PowerShell, allthough I’m still puzzled by the weird syntax (we, the developers, we need a C# shell over PowerShell).
The batch LaunchProtem.bat contains a simple line like
Start %PROTEM% file=”%CD%\%1″
Even with the quotes, the application still complained about not being able to find the input file.
As a test, I’ve renamed one of the input files to Query_CHM_1.txt. And the file was processed, while the application complained about the other files. Now, the said (and sad) application has been written with Delphi 7 quite a long time ago, so I didn’t bother analyzing further. I’ve decided to rename all files and replace the spaces with underscores. This should be easy, shouldn’t it? I’ve realized I do not know an easy way to rename a batch of files with a DOS command, so I’ve thought I have to find a way to do this with PowerShell. With thanks to the community, here is the simplest way to achieve this:

gci  | rename-item -newname { $_.name -replace " ", "_"}