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,
mcs26Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cyberkiwiCommented:
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
0
cyberkiwiCommented:
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%
0
mcs26Author Commented:
It is saying invalid command, please see screen shot attached.

Thanks,
Batch-File-Date.doc
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

mcs26Author Commented:
Sorry Kiwi,

What do you mean replace the angled single quotes?

Cheers
0
Bill PrewCommented:
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
0
cyberkiwiCommented:
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

0
mcs26Author Commented:
Hi CyberKiwi,

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

Thanks,
0
cyberkiwiCommented:
No,
   ftp -s:%ftpascii% indexftp.barcap.com
is correct

Originally, you had ftp -s:txtLehman.scp indexftp.barcap.com
which means run the commands in the scp file

Instead, ftp -s:%ftpascii% indexftp.barcap.com
says to run all commands in the file "C:\windows\temp\ftpcommands.txt"  (by way of %ftpcommands%)
I see... please fix these two lines from my prev comment above

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

The date is written into the last command added to that file
0
mcs26Author Commented:
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

0
Steve KnightIT ConsultancyCommented:
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:

http://www.experts-exchange.com/Software/Internet_Email/File_Sharing/FTP/Q_25645070.html?cid=1065#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
0
t0t0Commented:
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%
0
t0t0Commented:
This code is a more direct approach and slightly faster as there's no need to alter the long date format.




set dateformat=yyyy--MM--dd

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

reg add "hkcu\control panel\international" /v sshortdate /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

echo %currentdate%
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cyberkiwiCommented:
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
0
t0t0Commented:
Based on my date formating code, your ftp code and cyberkiwi's suggestion. here's the whole thing in a nutshell:



@echo off

for /f "tokens=3,3" %%a in ('reg query "hkcu\control panel\international" /v sshortdate') do set sfmt=%%a
reg add "hkcu\control panel\international" /v sshortdate /t reg_sz /d yyyyMMdd /f >nul
set mydate=%date%
reg add "hkcu\control panel\international" /v sshortdate /t reg_sz /d %sfmt% /f >nul

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:%ftpcommands% indexftp.barcap.com
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.