简体   繁体   English

如何从bat文件向txt写“退出”字?

[英]How can I write “exit” word to txt from bat file?

I want to write to txt from bat file 我想从bat文件写入txt

I write this code: 我写这段代码:

@echo off
cd C:\test_folder
set st1=cd  newfolder/
set st2=mget xxxx*.csv
set st3=exit 0
echo %st1%> test2.bat
echo %st2%>>test2.bat
echo %st3%>>test2.bat

Bu the output is (test2.bat) : Bu输出为(test2.bat):

cd  newfolder/
mget xxxx*.csv

and the "exit" word is missed. 并且缺少“退出”字样。

How can I do this? 我怎样才能做到这一点?

thanks in advance 提前致谢

because your resulting line is echo exit 0>>test2.bat - where 0>>test2.bat tries to redirect the INPUT (STDIN) of echo exit to the file. 因为您得到的行是echo exit 0>>test2.bat其中0>>test2.bat尝试将echo exit的INPUT(STDIN)重定向到文件。 This results in an empty output to the file and the word echo on the screen. 这将导致文件输出为空,屏幕上将显示单词echo
echo exit 1>>test2.bat would redirect STDOUT ("normal" output) of echo exit to the file (which is just the word exit ). echo exit 1>>test2.bat将重定向STDOUT的(“正常”输出) echo exit到该文件(这仅仅是字exit )。
Even worse with exit 2 : 2 means ErrorStream, which is empty here, so your file would receive nothing at all, but STDOUT still goes to screen. 更糟糕的是, exit 22表示ErrorStream,此处为空,因此您的文件将什么都收不到,但是STDOUT仍然显示在屏幕上。

Whenever your line may end in a number, better use a different syntax: 每当您的行以数字结尾时,最好使用其他语法:

>>test2.bat echo exit 0

or 要么

(echo exit 0)>>test2.bat

If you have to write several lines, you can even write all of them in one go (instead of opening the file for each file separately, write the line, close the file before opening it again for writing the next line) 如果您必须写几行,甚至可以一次写完所有行(而不是分别为每个文件打开文件,而是写一行,在再次打开以写下一行之前关闭文件)

>test2.bat (
  echo %st1%
  echo %st2%
  echo %st3%
) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Windows bat 文件重命名 txt 文件中的文件? - How can I rename a file from a txt file with Windows bat file? 在.bat文件中执行程序后如何写程序? - How can I write to a program after executing it in a .bat file? 从txt中获取一个数字加1并将其写回到bat文件中 - get a number from txt add 1 and write it back in bat file 如何将.bat文件中的变量导入PowerShell脚本? - How can I source variables from a .bat file into a PowerShell script? 我如何从bat / cmd文件启动控制台应用程序,所以控制台输出将在bat内部? - How I can start console application from bat / cmd file, so the console output will be inside bat? 如何编写将杀死8个特定进程的.bat文件? - How do I write a .bat file that will kill 8 specific processes? 如何使用输入参数从另一个.bat调用.bat。 文本 - How to call .bat from another .bat with input parameters . txt 如何创建.bat文件,该文件将从文件夹中删除一个txt文件并重命名其他文件? - How to create .bat file that will delete one txt file from folder and rename other? 如何使用SQL触发器运行批处理文件(.Bat) - How Can I Run A Batch File (.Bat) Using SQL Triggers 如何使用 .bat 文件打开新的 Chrome 标签页 - How can i use a .bat file to open new Chrome Tabs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM