please explain script and how to change

Posted on 2013-05-18
Last Modified: 2013-05-31
Hello all, I need some help with a script. Please explain this line of code and how to get it to pull the correct date
or /f "tokens=2" %%d in ('date /t') do set dte=%%d
for /f "delims=/ tokens=1-3" %%a in ('echo %dte%') do set ymd=%%c%%a%%b
set DATESTAMP=%ymd: =%
set hhmmss=%time:~0,2%%time:~3,2%%time:~6,2%
set DATESTAMP=20130516
This is for an FTP job that is supposed to perform GET %DATESTAMP%dep_0501.txt. The job failed cause it looked for  a file with a date of 20130516. As shown in the log entry: Failure in command [GET 20130516dep_0501.txt]
There is however a file called 20130517dep_0501.txt on the ftp site. I am assuming that te date syntax is not calculating the date correctly so it appended the wrong file name, hence the failure. Am I correct in my assumption about the date calculation? if so, what change do I need to make so the job looks for the correct file name. This is a scheduled job so I don't want to have to manually move the file every time. Thanks in advance.
Question by:xzay1967
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 70

Expert Comment

ID: 39177381
Though that is pretty much overcomplicated code, it should work if you remove the last line (set DATESTAMP), which overrides the datestamp calculated based on the current date.

If you are just after todays date in yyyymmdd format, this should do:
set DATESTAMP=%date:~-4%%date:~-10,2%%date:~-7,2%

Open in new window


Author Comment

ID: 39177481
for /f "tokens=2" %%d in ('date /t') do set dte=%%d
for /f "delims=/ tokens=1-3" %%a in ('echo %dte%') do set ymd=%%c%%a%%b
set DATESTAMP=%ymd: =%
set hhmmss=%time:~0,2%%time:~3,2%%time:~6,2%
set DATESTAMP=20130516

Thanks for the response, I am not sure what the code is supposed to look or what file name is expected. Please break down the code above for me line by line so I have an understanding what it conveys. Knowing that might give me a better idea if I need to change to what is suggested. Thing is, this code worked for years, so not sure why now all of a sudden it failed. Another assumption is that person putting the file in the source destination may have named the file in error not mindful of the date.
LVL 70

Accepted Solution

Qlemo earned 500 total points
ID: 39177803
If "that" code ran for years, someone has changed it, because it will always return a DATESTAMP of 20130516.
As I said, the last line is wrong and needs to get removed. Or follow my advice and just use the single line I provided (and nothing else). It does not make much sense to explain the long-winded code line by line if you can do the same with a simple one-liner.

Here are some basics:
%date% and %time% are current date and time in national format, so %date% will be 05/19/2013 in US based countries, 19.05.2013 in some other and so on. From the code you used I take it you have US date format.

%date:~-4% takes the last 4 characters of the var %date% - the 4-digit year.
%date:~0,2% would get the first two chars of the date ("start at position 0, and take 2 characters"). I could have used that to get the month, but instead I went from the end: %date:~-10,2%.
That substring operator can be applied to %time% or any other variable, of course.

for /F "tokens=... delims=..." takes a string (in double quotes) or result of a command (in single quotes), and splits it in "tokens" at delimiters. The first token is stored in the FOR var you provide, the next tokens in the alphabetical following. E.g.
for /F "tokens=1,2 delims=." %A in ("1.2.3") do echo %A %B

Open in new window

(outside of a batch file) will echo
 1 2
If you do not provide a "delims" clause, whitespace (tabs and space) is used.
If you do not provide a "tokens" clause, only the first token is used.

That should give you an idea how your existing code works:
1. execute date /t, which just outputs the current date. Take the second part, which is preceded by a space, and store it in the var dte. This only works if date /t gives a result like Sat, 05/19/2013.

2. Echo the var generated in the first step, build tokens at slashes, and provide the first three tokens in %%a (month), %%b (day) and %%c (year). Set ymd accordingly.

3. Remove any space from ymd, and store that in DATESTAMP.

4. Store the time portion - not used anywhere.

5. Set DATESTAMP to a constant (and wrong) value.
LVL 55

Expert Comment

by:Bill Prew
ID: 39177819

(1) If you do DATE /T at a command prompt, what does it display?

(2) Is the following line really in the script as you show it?  If so then this will be always override the other lines and set the date to 20130516.

set DATESTAMP=20130516


Author Comment

ID: 39177905
@BP, yes it actually is.
@Qlemo thanks for the input, I gather what you are saying, it makes perfect sense. I will consult with my supervisor to see if he wants to make that change you suggested.

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
This article was inspired by a question here at Experts Exchange ( The requirements stated in that question are (1) reduce the file size of a large number of…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question