0

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.

Stephan
  • 47,723
  • 10
  • 50
  • 81
jay dee
  • 1
  • 1
  • if you look for a language independent method to get the date string, see [here](https://stackoverflow.com/a/18024049/2152082) – Stephan Dec 21 '17 at 16:27
  • 2
    In a batch file you will need to double the percent characters, `%a`, `%b` and `%c` become `%%a`, `%%b` and `%%c` respectively. – Compo Dec 21 '17 at 16:31
  • Mofi's answer below is a direct quote from the help file for the `FOR` command. Please consider reading the help file in the future before asking a question. – Squashman Dec 21 '17 at 18:51

3 Answers3

1

Open a command prompt window and run 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
of %variable.  Variable names are case sensitive, so %i is different from %I.

Next I suggest to run set /? and read at least last page of output help listing the environment variable 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.  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.

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

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

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

%ERRORLEVEL% - expands to the current ERRORLEVEL value

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

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

%HIGHESTNUMANODENUMBER% - expands to the highest NUMA node number
    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.

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"

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.

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.

Mofi
  • 38,783
  • 14
  • 62
  • 115
0

Batch variable need to have %% instead of only one

0

It appears you are looking for the output to be YYMMDD, if so try this:

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