简体   繁体   English

在批处理文件中定义变量不起作用

[英]Defining a variable in a batch file not working

I have a batch file that looks like the following: 我有一个如下所示的批处理文件:

For /f "tokens=2-4 delims=/ " %a in ('date /t') do (set newdate=%c%a%b)
blat my_file_%newdate% -to test@email.com -f test_email.com

When I enter this two commands separately in a cmd window, it seems to work perfectly fine, but when placed into a batch file and ran manually, it does not work. 当我在cmd窗口中分别输入这两个命令时,它似乎工作得很好,但是当放入批处理文件并手动运行时,它不起作用。

Open a command prompt window and run for /? 打开命令提示符窗口并运行for /? . Output is the help for this command containing at top the information: 输出是此命令的帮助,该命令顶部包含以下信息:

To use the FOR command in a batch program, specify %%variable instead 要在批处理程序中使用FOR命令,请改为指定%% variable
of %variable. 的%variable。 Variable names are case sensitive, so %i is different from %I. 变量名称区分大小写,因此%i与%I不同。

Next I suggest to run set /? 接下来我建议运行set /? and read at least last page of output help listing the environment variable DATE . 并至少阅读输出帮助的最后一页,其中列出了环境变量DATE

If Command Extensions are enabled, then there are several dynamic 如果启用了命令扩展,则有几个动态
environment variables that can be expanded but which don't show up 可以扩展但不会显示的环境变量
in the list of variables displayed by SET. 在SET显示的变量列表中。 These variable values are 这些变量值是
computed dynamically each time the value of the variable is expanded. 每次扩展变量的值时都会动态计算。
If the user explicitly defines a variable with one of these names, then 如果用户使用以下名称之一显式定义变量,则
that definition will override the dynamic one described below: 该定义将覆盖下面描述的动态定义:

%CD% - expands to the current directory string. %CD%-扩展到当前目录字符串。

%DATE% - expands to current date using same format as DATE command. %DATE%-使用与DATE命令相同的格式扩展到当前日期。

%TIME% - expands to current time using same format as TIME command. %TIME%-使用与TIME命令相同的格式扩展到当前时间。

%RANDOM% - expands to a random decimal number between 0 and 32767. %RANDOM%-扩展为0到32767之间的随机十进制数。

%ERRORLEVEL% - expands to the current ERRORLEVEL value %ERRORLEVEL%-扩展为当前的ERRORLEVEL值

%CMDEXTVERSION% - expands to the current Command Processor Extensions %CMDEXTVERSION%-扩展到当前的命令处理器扩展
version number. 版本号。

%CMDCMDLINE% - expands to the original command line that invoked the %CMDCMDLINE%-扩展到调用该命令的原始命令行
Command Processor. 命令处理器。

%HIGHESTNUMANODENUMBER% - expands to the highest NUMA node number %HIGHESTNUMANODENUMBER%-扩展到最高NUMA节点号
on this machine. 在这台机器上。

So there is perhaps no need to run in a separate command process in background with cmd.exe /C the command line date /T as done by FOR with the posted command line, capture output of this command process, and process it line by line by FOR . 因此,也许不需要在后台使用cmd.exe /C在命令行中运行单独的命令进程,而在命令行中使用date /T f则由FOR用发布的命令行完成,捕获该命令进程的输出,并逐行处理由FOR

Well, the format of date output by date /T or on using %DATE% depends on Windows region setting. 好吧,按date /T或使用%DATE%输出的date /T格式取决于Windows区域设置。 And it was not posted what is the date format on used machine with used account. 并没有发布使用旧帐户的旧计算机上的日期格式。 But I suppose that following works a very little bit faster too. 但是我想跟踪的速度也快一点。

for /F "tokens=2-4 delims=/ " %%a in ("%DATE%") do set "newdate=%%c%%a%%b"

I suppose using only string substitution works also on your machine for your account with a date format mm/DD/yyyy or dddd, mm/DD/yyyy : 我想对于日期格式为mm/DD/yyyydddd, mm/DD/yyyy帐户,也仅在您的计算机上使用字符串替换:

set "newdate=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%"

This last solution is some microseconds faster than the others. 最后一个解决方案比其他解决方案快几微秒。

There is also a region independent solution as explained in detail for example by the answer on %date% produces different result in batch file when run from Scheduled Tasks in Server 2016 . 还有一个与区域无关的解决方案,例如通过从Server 2016中的“计划任务”运行时%date%的答案在批处理文件中生成不同的结果来详细说明。 But region independent solution using WMIC is really much slower in comparison to using dynamic environment variable DATE . 但是,与使用动态环境变量DATE相比,使用WMIC进行区域独立的解决方案确实要慢得多。

批处理变量需要具有%%而不是仅一个

看来您正在寻找的输出是YYMMDD,如果是这样,请尝试以下操作:

For /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set newdate=%%c%%a%%b)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM