Windows 批处理脚本 - 尝试将变量保存到文本文件,然后在重新启动时将其加载回变量中
我正在尝试做一些可能非常简单的事情。
我试图允许 Windows 批处理脚本的用户设置背景颜色并将其存储在 txt 文件中,然后每次运行脚本时将其加载到变量中。
到目前为止,我已经:
set /p ColourOpt=<%LogPath%\%UserName%_Options.Colour.log
它将 .log 文件的内容加载到 ColourOpt 变量中(%username% 和 %logpath% 已定义并且是工作正常)。
然后我使用以下代码来应用该选项:
If /I '%Colouropt%'=='0f' Color 0f
If /I '%Colouropt%'=='08' Color 08
If /I '%Colouropt%'=='0c' Color 0c
If /I '%Colouropt%'=='0d' Color 0d
If /I '%Colouropt%'=='0e' Color 0e
If /I '%Colouropt%'=='0a' Color 0a
If /I '%Colouropt%'=='1a' Color 1a
If /I '%Colouropt%'=='1c' Color 1c
If /I '%Colouropt%'=='1e' Color 1e
If /I '%Colouropt%'=='1f' Color 1f
这也可以正常工作,并且颜色设置没有问题。
现在的问题是,当我使用脚本将用户颜色设置输出到 .log 文件时,它会添加空格,这会导致脚本关闭而不是应用更改。
用于写入 .log 文件的代码:
IF /I '%Options%'=='1' Echo 0f>%LogPath%\%UserName%_Options.Colour.log & Set ColourOpt=0f & Color 0f
这会将选项保存到 .log 文件并立即设置颜色,以及将其设置在变量中。
然而,这输出的文本文件在“0f”后面有一个空格,并且在它下面有一个空行。
有没有办法不带空格输出,或者允许 if 命令忽略空格?
任何帮助将不胜感激!
谢谢!
I'm attemting to do something probably pretty easy.
I'm trying to allow users of a windows batch script to set the background colour and store it in a txt file which is then loaded into a variable each time they run the script.
So far I have:
set /p ColourOpt=<%LogPath%\%UserName%_Options.Colour.log
Which loads the contents of the .log file into the ColourOpt variable (%username% and %logpath% are already defined and are working fine).
Then I use the following code to apply the option:
If /I '%Colouropt%'=='0f' Color 0f
If /I '%Colouropt%'=='08' Color 08
If /I '%Colouropt%'=='0c' Color 0c
If /I '%Colouropt%'=='0d' Color 0d
If /I '%Colouropt%'=='0e' Color 0e
If /I '%Colouropt%'=='0a' Color 0a
If /I '%Colouropt%'=='1a' Color 1a
If /I '%Colouropt%'=='1c' Color 1c
If /I '%Colouropt%'=='1e' Color 1e
If /I '%Colouropt%'=='1f' Color 1f
This also works fine and the colour is set without issue.
Now the problem, When i use the script to output the users colour setting to the .log file it adds spaces, which now cause the script to close instead of applying the change.
The code used to write to the .log file:
IF /I '%Options%'=='1' Echo 0f>%LogPath%\%UserName%_Options.Colour.log & Set ColourOpt=0f & Color 0f
This will save the option to the .log file and set the colour straight away, aswell as setting it in the variable.
However this outputs the text file with a space after "0f" and a blank line under it.
Is there a way to output without the spaces, or allow the if command to ignore spaces?
Any help would be greatly apreciated!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么您尝试将其作为单个操作来完成? Windows 多年来一直拥有多行
if
块:它实际上是第一个
&
之前的空格输出到文件,您同样可以使用以下方法修复该问题:(那里没有空间)。
如果您甚至不想要行结尾(即,如果您希望文件仅
0f
组成),那么您已经知道一半了窍门:这将创建一个仅包含这两个字符的文件
xyz.txt
- 没有多余的空格,也没有行结尾。有关更多信息和所有其他答案,请参阅此答案,包括我的一些技巧,一些巧妙的技巧。
Why are you trying to do it as a single operation? Windows has had multi-line
if
blocks for ages:It's actually the space preceding the first
&
which is being output to the file, you could equally well fix that with:(no space in there).
If you don't even want the line endings (i.e., if you want your file to consist of only
0f
), you already know half the trick:This will create a file
xyz.txt
with only those two characters - no extra space and no line ending afterwards.See this answer for more information and all the other answers, including some more of mine, for some neat tricks.