I have thousands of files whose filenames contain the "caret" symbol, but when I try to assign them to a variable, I get weird results. If the filename is "01 ^ Driver's Seat.flac,"
for example, the command echo %1
returns "(path)\01 ^^ Driver's Seat.flac,"
with an extra caret, and processing halts. How to I get the correct output?
Asked
Active
Viewed 128 times
0

JosefZ
- 28,460
- 5
- 44
- 83

Wally Walters
- 75
- 1
- 1
- 8
-
5There is a bug in your code in line 13 at the call statement! (Perhaps you should show code when you have problems with code) – jeb Feb 24 '17 at 09:27
-
5[Buggy behaviour when using CALL](https://ss64.com/nt/call.html): _If the CALL command contains a caret character within a quoted string `"test^ing"`, the carets will be doubled._ (`"test^^ing"`) – JosefZ Feb 24 '17 at 09:45
-
3@JosefZ `CALL` always doubles the carets, even without quotes, but then you normally can't see it (only with very special test cases) – jeb Feb 24 '17 at 12:03
-
3If the file name is stored in a variable `VAR`, you could write `call :SUB "%%VAR%%"` instead of `call :SUB "%BAR%"`, which should fix it (in case you are actually using call, which I do not know as you do not show your code)... – aschipfl Feb 24 '17 at 12:16
-
1If you are calling to a function then pass the variable name by reference to the function and expand it in the function. I showed you how to do this in one of your previous questions. – Squashman Feb 24 '17 at 13:40
-
2@jeb: Did you got another crystal ball? What model is this one? **`:)`** – Aacini Feb 24 '17 at 14:58
-
Thanks, everybody, expecially Squashman and JosefZ. Squashman, you did indeed answer me previously but I didn't understand that `CALL` always doubles the carets, until Josefz explained it here. That suggests I _always_ have to plan for this behavior in the way you described. Again, much obliged. – Wally Walters Feb 25 '17 at 17:42
1 Answers
2
Doubling of carets is a problem of the CALL
command (see: How the batch parser works).
This can be avoided by using calling a function with variables by reference instead of variables by value.
call :func "%variableName%" -- by value
call :func variableName -- by reference
call :func filename
...
:func
setlocal EnableDelayedExpansion
set "filename=!%1!"
echo filename: !filename!