Link to home
Start Free TrialLog in
Avatar of mcs26
mcs26

asked on

MS DOS Get Date in format YYYYMMDD

Hi

I am trying to get the current date and pass it into my batch file so that it can copy todays file from an FTP site. I do not know how to get the data in the YYYYMMDD format?

Thanks,
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

FOR /F "TOKENS=1* DELIMS= " %%A IN (’DATE/T’) DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN (’DATE/T’) DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN (’echo %CDATE%’) DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN (’echo %CDATE%’) DO SET yyyy=%%B
SET mydate=%yyyy%%mm%%dd%

Replaced word's left right double quotes with the straight ones
try again..
replaced the angled single quotes

FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET mydate=%yyyy%%mm%%dd%
Avatar of mcs26
mcs26

ASKER

It is saying invalid command, please see screen shot attached.

Thanks,
Batch-File-Date.doc
Avatar of mcs26

ASKER

Sorry Kiwi,

What do you mean replace the angled single quotes?

Cheers
Basically, you just take apart the %DATE% variable and reorder as needed.  The only thing that complicates this slightly is different machines can be configured for different date format settings (based on country, etc), so one solution doesn't work exactly for everybody.

To get started do the following command at a command line:

ECHO %DATE%

This will show you the format for the date in that variable on your system.  Mine looks like this:

Mon 04/26/2010

Then, in the bat file, you can use the "substring" and "concatenate" capability to rearrange the pieces anyway you want.  So to get YYYYMMDD from my format, I would do:

SET MYDATE=%DATE:~10,4%%DATE:~7,2%%DATE:~4,2%

There are other ways to chop up the formatted date, but this is one of the easiest.

Help on the substring capability can be found via SET /? at a prompt.  Just keep in mind the offset is zero based, so the first character is offset 0, not 1.

~bp
Hi,

The code is correct, but you cannot pass DOS variables directly into ftp commands
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET mydate=%yyyy%%mm%%dd%

set ftpcommands="C:\windows\temp\ftpcommands.txt"
copy /y txtLehman.scp txtLehman.scp.copy

echo mget *_TAPLATRI_%mydate%_Global_Barcap_HY_Index.csv "G:\Shared\Mark\FTP\" overwrite >> %ftpcommands%

ftp -s:%ftpascii% indexftp.barcap.com

-----------
the file txtLehman.scp should NOT have the mget line in it. this adds it

Open in new window

Avatar of mcs26

ASKER

Hi CyberKiwi,

is that supposed to be -s:%ftpascii% - %ftpcommands%?

Thanks,
SOLUTION
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcs26

ASKER

Sorry I am being stupid here I know but do not get DOS at all. Below is what my batch file looks like. Am I then right to say it is running all the commands in ftpcommands.txt? Which I guess means I have to change the location of ftpcommands?

Thanks again
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET mydate=%yyyy%%mm%%dd%

set ftpcommands="C:\windows\temp\ftpcommands.txt"
copy /y txtLehman.scp %ftpcommands%

echo mget *_TAPLATRI_%mydate%_Global_Barcap_HY_Index.csv "G:\Shared\Fixed Income\Sovereign\Mark\FTP" overwrite >> %ftpcommands%

ftp -s:%ftpascii% indexftp.barcap.com

Open in new window

I would suggest getting your date using VBScript instead as it is less open to changes in date formats. See previous question on same subject here:

https://www.experts-exchange.com/questions/25645070/using-ftp-commands.html?cid=1065&anchorAnswerId=29443946#a29443946

and my article on how to get different date formats.

Other ways of doing the ftp bit itself are here as there are a few methods:

http://scripts.dragon-it.co.uk/links/batch-ftp-scripting

No time to check against your script at the mo. sorry but would be worth posting your ftp command script or checking it against the examples in my link above.

Steve
Please try this code.

This will work for all date formats by forcing windows to save the current date format, set it's date format to the required date format before reading the system date then restore the date format.



set dateformat=yyyyMMdd

for /f "tokens=3,3" %%a in ('reg query "hkcu\control panel\international" /v sshortdate') do set sfmt=%%a
for /f "tokens=3,3" %%a in ('reg query "hkcu\control panel\international" /v slongdate') do set lfmt=%%a

reg add "hkcu\control panel\international" /v sshortdate /t reg_sz /d %dateformat% /f >nul
reg add "hkcu\control panel\international" /v slongdate /t reg_sz /d %dateformat% /f >nul

set currentdate=%date%

reg add "hkcu\control panel\international" /v sshortdate /t reg_sz /d %sfmt% /f >nul
reg add "hkcu\control panel\international" /v slongdate /t reg_sz /d %lfmt% /f >nul

echo %currentdate%
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The last line in batch file should be

ftp -s:%ftpcommands% indexftp.barcap.com

----

In the file txtLehman.scp, the last line in there for get/mget should be deleted
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial