将批处理变量保存在文本文件中 [英] Saving a batch variable in a text file

查看:797
本文介绍了将批处理变量保存在文本文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将批处理变量保存到文本文件中.我目前有此代码:

I am trying to save a batch variable into a text file. I currently have this code:

@echo off

Set var=6
@echo %var%>txt.txt

For /f "tokens*" %%i in (txt.txt) do @echo %%i
Pause

应该将6保存到变量var中,然后将变量写入文本文件中.我想这样做是为了将用户输入保存到文本文件中,以便在批处理程序终止时将保存变量.

It's supposed to save the 6 into the variable var and then write the variable in a text file. I want to do this to save user input into a text file so that when the batch program is terminated it will hold the variables.

推荐答案

重定向存在一些问题.您正在重定向流";它们编号为0-9. 0表示标准输入"(STDIN),1表示标准输出"(STDOUT),2表示错误输出"(STDERR).

There is a little problem with redirection. You are redirecting a "stream"; they are numbered 0-9. 0 is for "Standard Input" (STDIN), 1 is for "Standard Output" (STDOUT), 2 is for "Error Output" (STDERR).

如果使用不带流号的重定向符号>,则默认为"1".

If you use the redirection symbol > without a stream number, it defaults to "1".

所以echo text>txt.txt只是echo text 1>txt.txt

现在变得棘手:echo 6>txt.txt不会在文件上回显"6",而是尝试将流6"(为空)重定向到文件.标准输出echo is off转到屏幕,因为未重定向"Stream1".

Now it's getting tricky: echo 6>txt.txt won't echo "6" to the file, but tries to redirect "Stream 6" (which is empty) to the file. The Standard Output echo is off goes to the screen, because "Stream1" is not redirected.

解决方案:

如果您尝试重定向数字或以数字结尾的字符串,请使用其他语法:

If you try to redirect a number or a string which ends with a number, just use a different syntax:

>txt.txt echo 6

这篇关于将批处理变量保存在文本文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆