在批处理文件中定义变量不起作用 [英] Defining a variable in a batch file not working

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

问题描述

我有一个如下所示的批处理文件:

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

当我在cmd窗口中分别输入这两个命令时,它似乎工作得很好,但是当放入批处理文件并手动运行时,它不起作用.

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.

推荐答案

打开命令提示符窗口并为/?运行.输出是此命令的帮助,该命令顶部包含以下信息:

Open a command prompt window and run for /?. Output is the help for this command containing at top the information:

要在批处理程序中使用FOR命令,请改为指定%% variable
的%variable.变量名称区分大小写,因此%i与%I不同.

To use the FOR command in a batch program, specify %%variable instead
of %variable.  Variable names are case sensitive, so %i is different from %I.

接下来,我建议运行 set/?并至少阅读输出帮助的最后一页,其中列出了环境变量 DATE .

Next I suggest to run set /? and read at least last page of output help listing the environment variable DATE.

如果启用了命令扩展,则有多个动态
可以扩展但不会显示的环境变量
在SET显示的变量列表中.这些变量值是
每次扩展变量的值时都会动态计算.
如果用户使用以下名称之一明确定义变量,则
该定义将覆盖下面描述的动态定义:

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.  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%-扩展到当前目录字符串.

%CD% - expands to the current directory string.

%DATE%-使用与DATE命令相同的格式扩展到当前日期.

%DATE% - expands to current date using same format as DATE command.

%TIME%-使用与TIME命令相同的格式扩展到当前时间.

%TIME% - expands to current time using same format as TIME command.

%RANDOM%-扩展为0到32767之间的随机十进制数.

%RANDOM% - expands to a random decimal number between 0 and 32767.

%ERRORLEVEL%-扩展为当前的ERRORLEVEL值

%ERRORLEVEL% - expands to the current ERRORLEVEL value

%CMDEXTVERSION%-扩展到当前的命令处理器扩展
版本号.

%CMDEXTVERSION% - expands to the current Command Processor Extensions
    version number.

%CMDCMDLINE%-扩展到调用
的原始命令行命令处理器.

%CMDCMDLINE% - expands to the original command line that invoked the
    Command Processor.

%HIGHESTNUMANODENUMBER%-扩展到最高NUMA节点号
在这台机器上.

%HIGHESTNUMANODENUMBER% - expands to the highest NUMA node number
    on this machine.

因此,也许不需要像那样在 cmd.exe/C 命令行 date/T 的后台在单独的命令进程中运行FOR (使用已发布的命令行),捕获此命令过程的输出,然后按 FOR (行)进行逐行处理.

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.

好吧,由 date/T 或使用%DATE%输出的日期格式取决于Windows区域设置.并没有使用旧帐户在使用过的计算机上发布日期格式.但是我想下面的工作也快一点.

Well, the format of date output by date /T or on using %DATE% depends on Windows region setting. And it was not posted what is the date format on used machine with used account. But I suppose that following works also a very little bit faster.

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

我想对于日期格式为 MM/dd/yyyy dddd,MM/dd/yyyy 的帐户,仅使用字符串替换在您的计算机上也有效:

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:

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

最后一个解决方案比其他解决方案快几微秒.

This last solution is some microseconds faster than the others.

还有一个与区域无关的解决方案,例如为什么%date%在批处理文件是否按计划任务执行?但是与动态环境变量 DATE 的使用相比,使用 WMIC 的与区域无关的解决方案要慢得多.

There is also a region independent solution as explained in detail for example by the answer on Why does %date% produce a different result in batch file executed as scheduled task? But the region independent solution using WMIC is much slower in comparison to the usage of the dynamic environment variable DATE.

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

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