cmd save =没有换行的文件的字符

时间:2017-01-31 13:42:14

标签: batch-file cmd

在提出这个问题之前,我已经问了另一个问题

{{3}}

由于这个问题已经由我自己解决了,我不得不问它是一个新问题。 我有一个像

这样的字符串

“tasks”=“我的司机”

需要使用cmd并保存到没有新行的文本文件中,如果使用此代码,“是工作,但是=缺失:

作品

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

错误!显示“命令的语法不正确。”

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

然后我尝试了以下方法:

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

同样 - “命令的语法不正确。”

这可以解决吗?谢谢!

2 个答案:

答案 0 :(得分:4)

你不能用set /p写一个等号,但我看不出你应该尝试的原因。

只输出完整的行(没有换行符)。

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

答案 1 :(得分:1)

要将任何字符串写入文件而不拖尾换行符,您可以使用临时文件:

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%"
相关问题