?
Solved

please explain script and how to change

Posted on 2013-05-18
5
Medium Priority
?
281 Views
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.
0
Comment
Question by:xzay1967
[X]
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
5 Comments
 
LVL 71

Expert Comment

by:Qlemo
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

0
 

Author Comment

by:xzay1967
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.
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 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.
0
 
LVL 58

Expert Comment

by:Bill Prew
ID: 39177819
@xzay1967

(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

~bp
0
 

Author Comment

by:xzay1967
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

650 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