我该如何在Windows批处理文件中实现以下操作?
- 将内容写入名为subdir/localsettings.py的文件中
- 覆盖所有已有内容...
- ...包括多行文本...
- ...其中包括一个字符串"[当前工作目录]/subdir"(我认为这可能是
%cd%/subdir
?)
请注意,我希望将此作为批处理脚本的一部分来执行,因此无法使用con
+Enter(至少我可能可以,但我不知道如何在批处理脚本中模拟Enter键)。
谢谢!
我该如何在Windows批处理文件中实现以下操作?
%cd%/subdir
?)请注意,我希望将此作为批处理脚本的一部分来执行,因此无法使用con
+Enter(至少我可能可以,但我不知道如何在批处理脚本中模拟Enter键)。
谢谢!
使用输出重定向符号>
和>>
echo one>%file%
echo two>>%file%
echo three>>%file%
或者更易读的方式:(在cmd.exe
中,使用"echo one >%file%
"将包括>
前的空格。)
>%file% echo one
>>%file% echo two
>>%file% echo three
你也可以使用:
(
echo one
echo two
echo three
) >%file%
echo Line 1^
Line 2^
Line 3 >textfile.txt
请注意双换行符以强制输出:
Line1
Line2
Line3
另外:
(echo Line 1^
Line 2^
Line 3)>textfile.txt
如果你想用一行代码实现,这里有一个更复杂的例子
\n
模拟回车\n
替换为批处理所需的内容set text=Cols:\n- Port de Lers (1517m) avec le col d'Agnès (1570m)\n- Col de la Core (1395m)\n- Col de Menté (1349m)\n- Col de Peyresourde (1569m)\n- Col du Tourmalet (2115m)\n- Col d'Aubisque (1709m)\n- Col de Marie-Blanque (1035m)\n- Col de Labays (1354m)
set "text=%text:(=^(%" :: I escape special character for parenthesis
set "text=%text:)=^)%" :: I escape special character for parenthesis
set "text=%text:\n= >> temp.txt & echo %" :: I replace the `\n` with what is needed in batch
set "text=%text:"=%"
if exist "temp.txt" rm temp.txt :: just remove the file if exist to avoid to append in it
echo %text% >> temp.txt
cat temp.txt :: print result
C:\ > cat temp.txt
Cols:
- Port de Lers (1517m) avec le col d'Agnès (1570m)
- Col de la Core (1395m)
- Col de Menté (1349m)
- Col de Peyresourde (1569m)
- Col du Tourmalet (2115m)
- Col d'Aubisque (1709m)
- Col de Marie-Blanque (1035m)
- Col de Labays (1354m)
如果您想删除最后的\r\n,请使用truncate -s -2 temp.txt
。
在Windows上安装git
以便能够使用truncate
。
echo.
或者echo=
或者echo:
等命令(cmd.exe
识别许多分隔符,不仅限于空格。到目前为止,我发现在echo
命令中可以使用的分隔符包括:.、;、,、/、=、+、\
)。 - user1686echo.
和其他大多数命令可能会与现有文件“echo”发生冲突,并且它们总是强制进行文件系统访问,echo(
似乎是“安全”的。 - jeb/?
或on
)“安全”。在这里讨论了echo. fails。 - jeb=
与echo=/?
失败,但其余的都可以。 (关于您在论坛上的帖子:对于/
,您可以使用echo//?
,而不是echo/?
。对于单个问号,只需使用echo ?
即可。但是现在任何理智的人都会将脚本移植到另一种语言中(提问者已经安装了Python)。无论如何,这就是我会做的事情。) - user1686