Link to home
Get AccessLog in
Avatar of IT CAMPER
IT CAMPERFlag for United States of America

asked on

Script to rename file based on date

I need to schedule a script to run each day that checks a specific UNC location for a file, based on the current date, and then renames the file. The file to check would be for the next day, so as an example, today is 200430 (YYMMDD) and so the script would need to look for file (tomorrow's date) 200501.log_PROCESSED. If the file is found then it needs to rename to 200501.log. I'd also like to manually run a version of this script that also is able to check more than one day into the future and rename. So on execution it can ask how many days to check. If I enter 3 then it will check for and rename 200501.log_PROCESSED, 200502.log_PROCESSED and 200503.log_PROCESSED.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
Avatar of IT CAMPER

ASKER

Worked perfectly! As always, you nailed it Bill. Thanks!
Avatar of Bill Prew
Bill Prew

Great, glad that was useful.


»bp
Bill, this has been running great. I'd like to add the ability to rename the current day file if parameter is 0. Is that possible? Let me know if I need to create a new question and simply link this one.
I can work it here, will post something tomorrow.

bp
SOLUTION
Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
Yes, I wanted to keep the existing behavior when passing it 1, 2 or 3 and just add the ability to pass it 0 for the current day. I tested and it appears to work exactly how I need it. Thanks again Bill!
Welcome, glad that helped.

»bp
Question. This is working great at one office but when I tried to deploy it at a second location, it does not execute. The paths are correct and the files are identical to the first location where it's working. I inserted a pause on the batch that is calling the vbs so that you can see what it's doing. I tried it on several different computers at the second location and also tried it on a computer that is not running anti-virus just in case. I am running other scripts without issue so this is something new to run into. Is there a way to turn on debug or log file for the script to see where it's bailing?

User generated image
Well, seems like one of two conditions could be causing that.  There are no errors so it seems to have run, just not found anything to do.  Looking at this part of the code:

    ' If the old file exists, and new doesn't, rename it
    If objFSO.FileExists(strOldFilePath) Then
        If Not objFSO.FileExists(strNewFilePath) Then
            objFSO.MoveFile strOldFilePath, strNewFilePath
        End If
    End If

Open in new window

So either the old file name didn't exist, or the new one already did.  Those seem like the ways I can see nothing being done.

Assuming that nothing was done, the script doesn't log anything, maybe it should?  Are you sure the file wasn't renamed (if both conditions were met in the IF statement)?


»bp
My bad. The file for this location is .asc instead of .log. So indeed it was not finding the file. Thanks for helping me think through that! I simply did a find and replace to customize it for the new location.
Great, happy to help, and glad you were able to sort it out.

»bp