Date Calculation

155 pts.
Tags:
Microsoft Windows
Windows Script Host
WSH
WSH scripts
I am needing to rename files based on the previous day's date. I am looking for help in calculating this taking into account the number of days in a given month. I.e. If I run the script on March 1st, the date in the name will need to be Feb. 28th. I am currently using Windows batch files. I have seen some WSH examples that seem to do the calculation more efficiently (less lines of code) but we are not that strong in WSH to support this - yet. Any help is appreciated.

Answer Wiki

Thanks. We'll let you know when a new response is added.

A colleague came to my rescue on this. He created a VB script for this that also runs the dir command. Here is the content of that:
<pre>
Dim getDate, getDay, getMonth, getYear, oShell, setPath, Response, Yesterday

Set oShell = CreateObject(“WScript.Shell”)

getDate = Date()-1
getDay = Day(Yesterday)
getMonth = Month(Yesterday)
getYear = Year(Yesterday)

Yesterday = getYear & “-” & getMonth & “-” & getDay

setPath = “cmd /c dir <i>path</i> <i>/variables</i> ><i>FileToPipeResultsTo</i>” & Yesterday & “<i>FileExtension</i>”

oShell.Exec setPath
————————————————————————————————

I don’t know of an easy way to do it using a BAT. You could need to program the date calculation by yourself in a similar way that the one used <a href=”http://www.computing.net/answers/windows-xp/delete-files-older-then-xdays-v2/171309.html”>here</a>.

Another option could be to have another bat scheduled to run daily, which writes the current date to a file, and then your other script reads the file created the day before.

In other words, one day, some script does this:

<pre>echo %date% > c:\previous_date.txt</pre>

And the next day, your script reads the file, and use that value to rename the desired files.

<pre>for /f %%a in (c:\previous_date.txt) do (
set previous_date=%%a
exit /b
)</pre>

————————</pre>

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following