Solved

please explain script and how to change

Posted on 2013-05-18
5
270 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
  • 2
  • 2
5 Comments
 
LVL 68

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 68

Accepted Solution

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

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

24 Experts available now in Live!

Get 1:1 Help Now