1

I am trying to run this line:

for /f "tokens=1* delims=" %%a in ('date /T') do set datestr=%%a

From command line, but got the error:

%%a was unexpected at this time.

Then I searched and found:

  1. "was unexpected at this time."
  2. %%A was unexpected at this time

The problem is to escape % on batch file I need to add two of them, but on the command line I only need one of it. How can I write it compatible with both command line and batch file keeping it in one and same line of code?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144
  • A _batch file_ and the _command line_ are entirely different things. When you _type_ the command in the command line, just use one percent-sign... What exactly is your problem? – Aacini Jun 22 '17 at 18:17
  • I am writing I tutorial for installing, then I instruct the user open the command line and run the commands, however if the user feels like to put these commands in a batch file, they will get trouble. – Evandro Coan Jun 22 '17 at 18:22
  • 3
    IMHO your "problem" is purely artificial. If you say nothing about a "batch file", how the user will know that such a feature exists, so he/she "feels like to put these commands" in a batch file? If the user know about batch files, then he/she must also know about the details on how to use them... You may include this footnote in your tutorial: "Remember that if you include these commands in a batch file, you must double the percent-signs". This solution is _much simpler_ and better than any other artificial one! – Aacini Jun 22 '17 at 18:36
  • Now I have to options, put the notice you mention, or use [@jeb](https://stackoverflow.com/users/463115/) solution :) – Evandro Coan Jun 22 '17 at 18:47

3 Answers3

3

Not a one liner, but when you can live with two lines

set "pp=%%"
for /f "tokens=1* delims=" %pp:~-1%a in ('date /T') do set datestr=%pp:~-1%a
jeb
  • 78,592
  • 17
  • 171
  • 225
  • 1
    @user, if you are writing a tutorial why would you want to teach it like that? If you are teaching people how to code batch files or use the cmd prompt then teach them the correct way so that they can understand how it all works. No offense jeb. You always got some sneaky trick up your sleeve. – Squashman Jun 22 '17 at 19:42
  • It is not a tutorial about batch files. It about installing a tricky package for Sublime Text. On the windows version they need to run some batch commands. Otherwise for the Linux/Mac version, just they need to run the same commands, but on shell script, which runs nice both from a shell script file or the command line. – Evandro Coan Jun 22 '17 at 20:13
3

Why not use

set "datestr=%date%"

which will set datestr to the current value of date as a built-in magic variable?

Of course, this doesn't cover the manipulation that is likely to be required since %date% and date /t both return the date in the format that the user has chosen - there's no universal format available directly in batch; you'd need to use wmic which may or may not be available to the user.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • I am actually using this `Command Prompt/Bat file - Create new folder named with today's date ` https://stackoverflow.com/a/17186963/4934640 to print date, which uses wmic. – Evandro Coan Jun 22 '17 at 18:00
  • @op: So - there appear to be no logical reason why you'd need to switch to `date /t` then - unless that was simply an example of the general direct-is-different-from-batch-file principle? – Magoo Jun 22 '17 at 18:18
  • Yes, I had just picked the first example about the problem I found and pasted on the question. – Evandro Coan Jun 22 '17 at 18:28
1

if you are executing this from command line you need single %:

for /f "tokens=1* delims=" %a in ('date /T') do set datestr=%a

Double percentage is for running this from .bat or .cmd file.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • But the problem is, if someone copy it to a batch file, or vice and versa, they will need to change the code. How to avoid that? – Evandro Coan Jun 22 '17 at 17:21
  • 3
    OP: you can't. it's just the way batch works - double `%` for metavariables *within* a batch file, but single directly from the prompt. – Magoo Jun 22 '17 at 17:39
  • @user - What you're asking isn't possible in batch. – SomethingDark Jun 22 '17 at 17:39
  • @SomethingDark, Why it shouldn't be possible? It looks strange, but still possible – jeb Jun 22 '17 at 17:46
  • 3
    @jeb : no doubt it *is* possible, if you take the time to research it. If a user can't follow the difference between from-the-prompt and within-batch, what are the chances of their being able to follow the convolutions required? – Magoo Jun 22 '17 at 17:54
  • 1
    @Magoo That is only too true, but as you know me, I have fun to build solutions for such a kind of questions – jeb Jun 22 '17 at 18:05