Capturing output from date.exe

Jan 2, 2018
2
0
Hi,

The following seems to work under CMD but not under TCC:

FOR /f "tokens=1,2,3" %%i in ('r:\gnu\bin\date.exe +"%%Y %%m %%d"') DO (
SET YY=%%i
SET MO=%%j
SET MD=%%k
)

I've been trying all sorts of variations, to no avail...
Also, is there a way to know if a script is being run from CMD or from TCC?

Thanks!
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,689
106
Albuquerque, NM
prospero.unm.edu
I think, if you want to use FOR to parse the output of a command, you will need to use the USEBACKQ option:
Code:
for /f "usebackq tokens=1,2,3" %%i in ( `r:\gnu\bin\date.exe +"%%Y %%m %%d"` ) ....

However, that seems to me like an awfully Rube Goldberg way to get the information. TCC provides built-in variables for date information:
Code:
echo %_year :: %_month :: %_date
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,689
106
Albuquerque, NM
prospero.unm.edu
Also, is there a way to know if a script is being run from CMD or from TCC?

To answer your second question: There are a lot of ways to test for TCC. Here's one that I like:
Code:
if 01 == 1 echo I'm running in TCC!

This is legal syntax in both shells. But in TCC, it's a numeric comparison and true; in CMD.EXE, it's a string comparison and false.
 
Jan 2, 2018
2
0
Thanks Charles for both messages.

My problem is (was) that this is part of a small script that will be used, basically, to extract a file from a repository and rename it, including a time stamp.

It will be run in different client systems. Some of them might be using TCC by default, while others will be using CMD.

It is working now...
 
May 20, 2008
12,169
133
Syracuse, NY, USA
I think, if you want to use FOR to parse the output of a command, you will need to use the USEBACKQ option
You can also use single quotes in both TCC and CMD.
Code:
v:\> ver

TCC  24.02.49   Windows 7 [Version 6.1.7601]

v:\> for /f "tokens=1,2,3" %i in ( 'echo a b c' ) do echo %i %j %k
a b c

v:\> cmd
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

v:\> for /f "tokens=1,2,3" %i in ( 'echo a b c' ) do echo %i %j %k

v:\> echo a b c
a b c
 

rconn

Administrator
Staff member
May 14, 2008
12,556
167
What's happening here is you're running into a TCC feature. TCC supports variable expansion in the FOR argument list; CMD does not. So the %Y %m %d are being expanded prematurely (before being passed to date.exe). 99.9% of the time this is desirable; this is the first time I've seen a construct that *doesn't* want the variables expanded.

You could kludge around this in TCC by enclosing the arguments in single back quotes:

Code:
FOR /f "tokens=1,2,3" %%i in ('r:\gnu\bin\date.exe +`"%%Y %%m %%d"'`) DO (

But you wouldn't want to. That FOR statement is a grotesque kludge to work around CMD limitations, and TCC has built-in variables to return those values.