Built on Sin
Built on Sin

Reputation: 351

Special Characters in a Variable

I have searched for an way to pass on the special characters located in a string but cannot find anything relevant. Currently the string is concatenated at the first special character.

for /D /R "%cd%" %%d IN (*) do (call :Write_File "%%~nd")

Can escape character be used or will I need a for loop to substitute the special characters, and another to replace them?

The variable is supposed to return the folder name. It returns the full length string unless it contains a special character %, &, ,, ., etc. at which point it returns everything up until that point. Example B&B returns B, where as I require it to return B&B.

With three example folder named: "%$", "01, Copy" and 03&

echoing the variable as it reads it (&&echo "%%~nd"): "$%", "01, Copy" and misses the third Renaming the variable within :Write_File (set "FOLDER=%~1"): "$", "01, Copy"

Edit for clarification:

Special Characters: %, $, &, *, ~, etc.

Use: When echoing the folder names Sometimes it misses the special character, sometimes it skips the folder entirely.

Upvotes: 0

Views: 5705

Answers (1)

jeb
jeb

Reputation: 82410

You can't use CALL here with the value, instead use a reference to a variable.

for /D /R %%d IN (*) do (
  set "myDir=%%~nd"
  call :Write_File myDir
)
exit /b

:Write_File
setlocal EnableDelayedExpansion
set "param1=!%1!"
echo !param1!
endlocal
exit /b

Upvotes: 1

Related Questions