Workaround for special characters in directory names?

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Workaround for special characters in directory names?

Post by MigrationUser »

02 Jun 2010 10:39
APMichael

Hi,

is there any way to use batch scripting while there are special chars in the directory names? Here are some examples:

1. Directory: D:\Temp\up&down\

Code: Select all

SET batchpath=%~dp0
ECHO %batchpath%
Output: D:\Temp\up

2. Directory: D:\Temp\up(down)

This time SET batchpath works but if you are using the variable in a command like FOR /F the script fails.

I did not found any way to escape the special chars. Also using the short names did not help, because they still include the special chars.

Thank you and best regards,
Michael

----------------------------

#2 02 Jun 2010 11:30
bluesxman


Special characters are a bit of a ball-ache. Double quotes should get round most problems.

Code: Select all

SET "batchpath=%~dp0"
ECHO:"%batchpath%"
If you want to "hide" the double quotes in the ECHO command for aesthetic reasons, you could do this:

Code: Select all

ECHO:"%batchpath%".
NB -- replace the "." after the "" at the end of the line with a space.

cmd | *sh | ruby | chef

----------------------------

#3 02 Jun 2010 13:30
APMichael


Thank you for your fast reply. I did not know that double quotes can be used this way. Great, this works perfect. smile

But the problem with FOR /F still occurs:

Directory: D:\Temp\up&down\

Code: Select all

SET "batchpath=%~dp0"
SET command="%batchpath%\tools\tool.exe"

ECHO %command%
%command%
All above works perfect now, but...

Code: Select all

FOR /F "tokens=1" %%a IN ('%command%') DO (SET value=%%a)
...still does not work. The script fails.

I know I can use a temporary text file as a workaround.

Code: Select all

%command% >temp.txt
FOR /F "tokens=1" %%a IN (temp.txt) DO (SET value=%%a)
But it would be better if it works directly in the FOR line. Is this possible?

----------------------------

#4 02 Jun 2010 13:57
bluesxman


Try it like so:

Code: Select all

SET "batchpath=%~dp0"
SET "command=%batchpath%\tools\tool.exe"

FOR /F "usebackq tokens=1" %%a IN (`"%command%"`) DO (SET value=%%a)
Note the moved quote on the "SET command" line as well as tweaks to the FOR command.

cmd | *sh | ruby | chef

----------------------------

#5 02 Jun 2010 16:53
APMichael


The suggestion did not work. The script stops with the error massage that the command "D:\Temp\up" and specified path could not be found. (I do not know the correct English error massage because I am using a German system.) The FOR command still interprets the "&" char not as a part of the string.

----------------------------

#6 02 Jun 2010 17:49
bluesxman


Yeah sorry I didn't test my suggestion.

I think you've exposed a bit of a bug in FOR. This is the only way I can make it work, but it's not a particularly elegant solution -- probably be better and easier just to use a temporary file.

Code: Select all

SET "batchpath=%~dp0"
SET "command=%batchpath%\tools\tool.exe"

set "command=%command:&=^&%"
set "command=%command:(=^(%"
set "command=%command:)=^)%"

FOR /F "usebackq tokens=1" %%a IN (`"%command%"`) DO (SET value=%%a)
cmd | *sh | ruby | chef

----------------------------

#7 02 Jun 2010 19:41
APMichael


It works! smile Thank you very much for your help.

As I wrote in my first post I already tried to escape the special chars - but it never worked because at that time I did not know your hint with the double quotes. So those double quotes are really the key to prevent problems with special chars.

I think the chars which causes the problem are all the "command symbols" with special scripting functions: & | ( ) ; , " ^ / < >

Some of them are not allowed for file names so there are not much left for escaping. I will check this during the next days. If I found more problematic special chars I will use the temporary file.

Thanks again and best regards,
Michael

----------------------------

#8 03 Jun 2010 11:36
APMichael


This FOR bug is really annoying. Found more problematic chars for it.

Only the following chars have to be escaped: ^ & ( ) , ; =

But I did not found a solution to do this for the "=".

Another problem occurs if the directory name includes a special char and a "space" together. I have to change the FOR line like this:

FOR /F "usebackq tokens=1" %%a IN (`""%command%""`) DO (SET value=%%a)

Note the doubled double quotes. It works but shows another bug. If you have "double (or more) spaces" in the directory name it fails again.

works: D:\Temp\up&do wn\
fails: D:\Temp\up&do wn\

This is a strange behaviour.

----------------------------

#9 03 Jun 2010 11:46
bluesxman


Aye, I've come across a few of these in the past. Usually I bite the bullet and write to a temp file, because mitigating these issues is usually far more effort than working around them.

cmd | *sh | ruby | chef

----------------------------

#10 03 Jun 2010 14:50
APMichael


Yes, you are right. I will use the double quotes with the SET command and a temporary file with the FOR command now. This solution works flawless. smile

----------------------------

#11 14 Jul 2010 19:20
Crash&Burn


Shouldn't this work?

Code: Select all

@ECHO OFF
SETLOCAL
CLS
SET DEBUG_ECHO=ECHO
SET DEBUG_VSET=SET
:: REMOVE ENDLOCAL if you dont want "value" to go into the GLOBAL ENVIRONMENT variables.
ENDLOCAL
SET i=0
SET cmd1=%~d0"%~p0ls.exe"
%DEBUG_ECHO% cmd1: %cmd1%
FOR /F "usebackq delims=?" %%F IN (`%cmd1%`) DO CALL:_SETVAL "%%F"

%DEBUG_ECHO% --value--
%DEBUG_VSET% value
GOTO:EOF

:_SETVAL
%DEBUG_ECHO% --SETVAL--
%DEBUG_ECHO% 1: "%~1"
SET /A i=%i% + 1
SET value%i%="%~1"
No Exceptions needed for special characters, they are all contained within quotes.

Tested within folders: "C:\BIN\xCMD\&)(,;='TEST"
UnxUtils, ls.exe was moved to this folder for the test, cmd prompt was opened to this path.

Last edited by Crash&Burn (14 Jul 2010 20:01)
Post Reply