用另一个蝙蝠文件中的变量创建一个蝙蝠文件 [英] Create a bat file with variables from another bat file

查看:67
本文介绍了用另一个蝙蝠文件中的变量创建一个蝙蝠文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用另一个bat文件中的设置变量创建一个bat文件。
用于启动 .bat 文件,该文件映射网络驱动器并将某些文件复制到本地计算机,并查看需要运行的系统服务。

每次登录.bat文件运行时,它还需要创建一个日志文件。这是我所做的一个示例。

  ECHO set SERVER IP =>> V:\GENESIS\ GENESIS INSTALL FILESLogGenesisLogonUser.bat 
ECHO set DRIVE1 = V>> V:; GENESIS\GENESIS INSTALL FILES\GenesisLogonUser.bat
ECHO set MAPDRIVE1 = \\ %SERVER IP%\v / P:是> V:\GENESIS\GENESIS安装文件\GenesisLogonUser.bat
ECHO净使用%DRIVE1%:%MAPDRIVE1%> %userprofile%\Documents\scripts\logonlog.txt>> V:\GENESIS\GENESIS安装文件\GenesisLogonUser.bat


解决方案

您需要避免变量的百分比扩展。这可以通过将批处理脚本中的每个符号加倍来完成:

 设置 VAR =值 
回声%% VAR %%

这将回显:


 %VAR%


请注意,这不能直接在控制台窗口中使用;您需要改为:

 >设置 VAR = Value 

> ; echo ^%VAR ^%
%VAR%






因此将其应用于您的脚本时,它看起来像这样(我从转义中排除了%USERPROFILE%,因为我不知道您是否喜欢它;当然您也可以写 %% USERPROFILE %% ):

 
ECHO设置为 SERVER IP =
ECHO设置为 DRIVE1 = V
ECHO设置为 MAPDRIVE1 = \\ %% SERVER IP %% \v / P:是
ECHO净使用率%% DRIVE1 %%:%% MAPDRIVE1 %%> ^>%USERPROFILE%\Documents\scripts\logonlog.txt
)> V:\GENESIS\GENESIS安装文件\GenesisLogonUser.bat

重定向到回显的文本中,需要像 ^> ^> 这样转义。



括号内的所有 ECHO 命令,仅需要一个重定向操作;



此外,我改进了 set 语法,以便放置整个赋值表达式放在一对引号之间,这样可以抵抗特殊字符(引号不会成为值的一部分)。


I want to create a bat file with set variables from another bat file. This is for a startup .bat file that maps network drives and copies some files to the local computer and looks at the system services that needs to run.

It also needs to create a log file every time the logon .bat file runs. Here is a sample what I have done.

ECHO set SERVER IP=>>"V:\GENESIS\GENESIS INSTALL FILES\GenesisLogonUser.bat"
ECHO set DRIVE1=V>>"V:\GENESIS\GENESIS INSTALL FILES\GenesisLogonUser.bat"
ECHO set MAPDRIVE1=\\%SERVER IP%\v /P:Yes>>"V:\GENESIS\GENESIS INSTALL FILES\GenesisLogonUser.bat"
ECHO net use %DRIVE1%: %MAPDRIVE1% >>"%userprofile%\Documents\scripts\logonlog.txt">>"V:\GENESIS\GENESIS INSTALL FILES\GenesisLogonUser.bat"

解决方案

You need to escape percent expansion of the variables. This can be done by doubling each % sign in a batch script:

set "VAR=Value"
echo %%VAR%%

This will echo:

%VAR%

Note that this does not work directly in the console window; you need to do it like this instead:

>set "VAR=Value"

>echo ^%VAR^%
%VAR%


So to apply this to your script, it looks like this (I excluded %USERPROFILE% from the escaping as I do not know how you like it; of course you could write %%USERPROFILE%% instead as well):

(
    ECHO set "SERVER IP="
    ECHO set "DRIVE1=V"
    ECHO set "MAPDRIVE1=\\%%SERVER IP%%\v /P:Yes"
    ECHO net use %%DRIVE1%%: %%MAPDRIVE1%%^>^>"%USERPROFILE%\Documents\scripts\logonlog.txt"
) > "V:\GENESIS\GENESIS INSTALL FILES\GenesisLogonUser.bat"

Since there is some redirection in the echoed text, this needs to be escaped like ^>^>.

I also put all ECHO commands within parentheses, which requires a single redirection operation only; this improves legibility and performance.

In addition, I improved the set syntax so that the entire assignment expression is placed in between a pair of quotation marks, which make it robust against special characters (the quotes do not become part of the value).

这篇关于用另一个蝙蝠文件中的变量创建一个蝙蝠文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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