Reputation: 27
When I run the following lines:
set "Loc=%~dp0"
echo %Loc% > C:\PLACE\LOCFILE.txt
I receive the following in the LOCFILE:
C:\BATCHLOC
^
Note Space
I am trying to use %Loc% like this in a separate batch file:
(
set /p Loc=
)<C:\PLACE\LOCFILE.txt
)
call "%Loc%\FILENAME.bat"
But the space ruins the path, and so then the call command doesn't work. Does anyone know how to fix this, (Stop it from creating the space at the end)?
Upvotes: 1
Views: 156
Reputation: 41297
This is more robust.
>"C:\PLACE\LOCFILE.txt" echo(%Loc%
The redirection at the starts stops numbers like 3,4,5 in %loc%
from breaking your code when redirection is directly at the end (such as shown below).
When using the technique below then constructs like test 2
in %loc%
will also fail.
This is because single digit numbers at the end are interpreted as stream designators, so numerals 0 to 9 are all problems when placed directly before a redirection character (>
or >>
).
do not use this: echo %Loc%> C:\PLACE\LOCFILE.txt
Extra tips:
The echo(
protects from other failures in the echo command and the (
character has been tested as the least problematic character when used in this way.
The double quotes around the "
drive:\path\filename"
also protect against long filename elements such as spaces, as well as the &
character.
Upvotes: 3
Reputation: 6695
echo %Loc% > C:\PLACE\LOCFILE.txt
↑
This space is written to the file.
Fix:
echo %Loc%> C:\PLACE\LOCFILE.txt
Upvotes: 3