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
  • 2
  • 2
LVL 68

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 68

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 52

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
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 …
Sending a Secure fax is easy with eFax Corporate ( First, just open a new email message. In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

864 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now