C:\PLACE\LOCFILE.t...">

Additional space issues when redirecting variables to a text file

When I run the following lines:

set "Loc=%~dp0" echo %Loc% > C:\PLACE\LOCFILE.txt 

I get the following in 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 space destroys the path, and therefore the call command does not work. Does anyone know how to fix this (stop creating space at the end)?

+1
source share
2 answers
 echo %Loc% > C:\PLACE\LOCFILE.txt ↑ This space is written to the file. 

Fix:

 echo %Loc%> C:\PLACE\LOCFILE.txt 
+3
source

It is more reliable.

 >"C:\PLACE\LOCFILE.txt" echo(%Loc% 

Redirection at startup stops a number, such as 3.4.5 in %loc% , from breaking the code when the redirection occurs immediately at the end (for example, shown below).

Using the technique below, constructs such as test 2 in %loc% will also fail.

This is because single digits at the end are interpreted as flow pointers, so numbers 0 through 9 are problems when placed immediately before the redirection character ( > or >> ).

do not use this: echo %Loc%> C:\PLACE\LOCFILE.txt

Additional tips:

echo( protects against other failures in the echo command, and the character ( was tested as the least problem character when used this way.

Double quotes around the " : \ path \ filename " disk also protect against long file names such as spaces, as well as the & character.

+3
source

Source: https://habr.com/ru/post/989677/


All Articles