简体   繁体   English

如何在 windows 命令提示符中设置具有多行文件内容的环境变量

[英]How to set an env variable with multiline file content in windows command prompt

I have a file say my-file.dat which has multiple lines.我有一个文件说my-file.dat有多行。 I want to set an env variable say MYVAR that will contain the content of my-file.dat.我想设置一个环境变量,比如MYVAR ,它将包含 my-file.dat 的内容。 That is, in windows command prompt, the output of type my-file.dat be same as the output of echo %MYVAR% and with new-line preserved .也就是说,在 windows 命令提示符下, type my-file.dat的 output 与echo %MYVAR%的 output保留相同,并带有新的

In RHEL/MAC, it is typically done like this export MYVAR=$(cat my-file.dat) , but how do I do the similar in windows command prompt (not really interested in powershell, but feel free to share examples it might be my backup option)在 RHEL/MAC 中,它通常像这样export MYVAR=$(cat my-file.dat)完成,但是我如何在 windows 命令提示符下执行类似操作(对 powershell 并不真正感兴趣,但随时分享它可能的示例成为我的备份选项)

Following the answer of this Stackoverflow question Multiline text file, how to put into an environment variable I could get a variable to hold the content of my-file.dat.按照这个 Stackoverflow 问题Multiline text file, how to put into an environment variable我可以得到一个变量来保存 my-file.dat 的内容。 But this variable's scope seems to be the batch file.但是这个变量的 scope 似乎是批处理文件。 Even though I run the batch file from the command prompt, I don't see the batch-file's variable is available in the command prompt.即使我从命令提示符运行批处理文件,我也没有在命令提示符中看到批处理文件的变量。

I tried set /P MYVAR=<my-file.dat , but this sets only the first line whereas I want all lines when I echo MYVAR我尝试set /P MYVAR=<my-file.dat ,但这仅设置第一行,而当我回显 MYVAR 时我想要所有行

Pls help.请帮忙。

The technique you use requires delayed expansion, which is not enabled by default.您使用的技术需要延迟扩展,默认情况下不启用。 So the code issues setlocal enableDelayedExpansion .所以代码发出setlocal enableDelayedExpansion But that localizes environment changes.但这会本地化环境变化。 When the script ends there is an implicit endlocal , and the environment is restored to what existed before the setlocal .当脚本结束时有一个隐含的endlocal ,并且环境恢复到setlocal之前的状态。

The simplest solution is to run the script in a session where delayed expansion is already enabled so that you can remove setlocal enableDelayedExpansion .最简单的解决方案是在已启用延迟扩展的 session 中运行脚本,以便您可以删除setlocal enableDelayedExpansion You can do this simply by running你可以简单地通过运行来做到这一点
cmd /v:on from your command line before executing your script. cmd /v:on在执行脚本之前从命令行中打开。

There is another simple, but ugly option.还有另一个简单但丑陋的选择。 The implicit endlocal does not occur if your script has a fatal syntax error (I consider this to be a bug in cmd.exe).如果您的脚本有致命的语法错误(我认为这是 cmd.exe 中的错误),则不会发生隐式 endlocal。 So you can put the following at the end of your script:因此,您可以将以下内容放在脚本的末尾:

:: Your current script goes here.
:: I'm assuming your script falls through to the end and does not use EXIT /B

call :fatalErr 2>nul

:fatalErr
if

But I discourage you from using this technique if you might be executing your script multiple times - your dead environments will begin to pile up.但是,如果您可能多次执行脚本,我不鼓励您使用这种技术 - 您的死环境将开始堆积。

Also - please remember that variables are limited to ~8191 characters - your script will fail if the file you are trying to capture exceeds the limit.另外 - 请记住,变量限制为 ~8191 个字符 - 如果您尝试捕获的文件超出限制,您的脚本将失败。 This is a hard limit of cmd.exe这是 cmd.exe 的硬限制

Update更新

You could put the cmd /v:on command within your script if you add the /K option.如果添加 /K 选项,则可以将cmd /v:on命令放在脚本中。 The IF statement tests if delayed expansion is enabled or not. IF 语句测试是否启用延迟扩展。 If not, then it reruns the script via cmd.exe with the /K option and /V:ON.如果不是,则它通过 cmd.exe 使用 /K 选项和 /V:ON 重新运行脚本。

@echo off
if "!!" neq "" cmd /v:on /k "%~f0"
:: rest of your script goes here.

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

相关问题 如何将命令输出设置为env。 在Windows的CMD变量? - How to set command output as env. variable in windows cmd? 如何通过命令提示符模式在Windows中设置文件权限? - How to set file permissions in windows through the command prompt mode? 从Ruby在Windows命令中设置本地ENV变量 - Set local ENV variable in Windows command from Ruby 如何在Windows命令提示符下获取文件? - How to source a file in Windows command prompt? 设置变量未显示在Windows命令提示符下。 (C ++) - Set variable not displayed on windows command prompt. (C++) 在命令提示符下查看包含Windows剪贴板内容的文件 - View file containing Windows Clipboard content in Command Prompt 使用 echo 清空 Windows 命令提示符中的文件内容 - Use echo to empty file content in Windows Command Prompt 如何在Windows命令行中的环境变量中读取文件的内容? - how to read content of a file in a environment variable in windows command line? 如何在Windows命令提示符下将命令的输出值分配给变量? - How to assign the output value of a command to a variable in Windows command prompt? 如何在Windows命令提示符下将文件内容连接到环境变量? - How do I concatenate contents of a file to an environment variable in Windows Command prompt?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM