momo
momo

Reputation: 121

cmd save = character to a file without new line

Before ask this question,I have already ask another one

cmd save " character to a file without new line

Due to that question has already marked solved by myself,I have to ask it as a new one. I have a string like

"tasks"="my driver"

need to use cmd and save to a text file without new line,if use this code,the " is work,but the = is missing:

Works

<nul >>"c:\file.txt" set/p="""

Error! Shows "The syntax of the command is incorrect."

<nul >>"c:\file.txt" set/p="="

Then I tried follow methods:

echo|set /p="=" >>C:\text.txt
echo|set /p== >>C:\text.txt
echo|set /p=^= >>C:\text.txt
set /p="=" <nul >> C:\text.txt
set /p== <nul >> C:\text.txt
set /p=^= <nul >> C:\text.txt

Also as the same-"The syntax of the command is incorrect."

Is that possible to fix?Thanks!

Upvotes: 3

Views: 1029

Answers (2)

aschipfl
aschipfl

Reputation: 34929

To write any character string to a file without trailing line-break, you could use temporary files:

rem // Ensure the file exists which you want the string or character to append to:
set "TESTFILE=.\test.txt"
>> "%TESTFILE%" rem/ Append the output of a remark, so nothing, to the file;

rem /* Create a temporary file containing the demanded string or character,
rem    followed by a SUB/EOF character (end of file, code 0x1A): */
set "TEMPFILE=%TEMP%\%~n0_%RANDOM%-1.tmp"
rem /* The `for` loop simply resolves path and name of the original file; replace `%%`
rem    by `%` to use this in command prompt directly rather than in a batch file: */
for %%F in ("%TESTFILE%") do (
    rem // The `0xHH` replacement of the `forfiles` is used to get the EOF character:
    forfiles /P "%%~dpF." /M "%%~nxF" /C "cmd /C > 0x22%TEMPFILE%0x22 echo =0x1A"
)

rem /* Concatenate the original file and the temporary file from above, which is
rem    truncated at the EOF character due to the ASCII option `/A`; output the
rem    result to another temporary file: */
set "COPYFILE=%TEMP%\%~n0_%RANDOM%-2.tmp"
copy "%TESTFILE%" /B + "%TEMPFILE%" /A "%COPYFILE%" /B
rem // Overwrite the original file with the second temporary file:
move /Y "%COPYFILE%" "%TESTFILE%"
rem // Clean up the first temporary file:
del "%TEMPFILE%"

Upvotes: 1

jeb
jeb

Reputation: 82327

You can't write a single equal sign with set /p, but I can't see the reason why you should try it.

Just output the complete line (without line feed).

>>"c:\file.txt" <nul set/p =""tasks"="mydriver""

Upvotes: 5

Related Questions