Quote:
Originally Posted by Squashman TheOutCaste will probably be stopping by in about 14 hours |
13 1/2 hours, I think your precognition needs a little tweaking
Quote:
Originally Posted by Squashman Basically each For loop will only run once because you are breaking out of it with a Goto statement. |
This is a good way to get the newest file in a folder that contains many files. By sorting "newest first" and jumping out after the first file you avoid processing a Set statement for each file in the folder. Slightly faster than using the default "oldest first" sort and just let the loop run though all the files.
This also lets you check if the folder is empty without having to clear the variable before each loop and then checking to see if it's Defined or not. If the folder is empty, the Goto will not be executed and it will fall through the loop, so if the loop completes you have no file found.
This version doesn't allow for that possibility and will just fall through use the last value for the
latest variable which will likely cause an error as it will be either null for the first loop, or will have the name of the file from the previous log folder for the rest.
Shouldn't be too hard. Will definitely need some serious tweaking as you'll have to determine the previous months name as well as the year for the previous month so you can create the new and access the old.
A few questions:
- You say you have to manually create the Month YY folder. Does this mean you also have to manually change the path in the program that creates the reports?
If so, might be easier to let the program always create the reports in the same folder.
Then, Move the report to the appropriate Year\Month YY folder and then create the copy with the name you wish. - Do you really want two copies of the report, one with the original name plus the renamed one?
- Does the program that creates the report make a new folder each year?
Again, if this has to be manually changed, easier to just always use the same folder, then move the reports to the dated folders each day.
This would also prevent you from copying yesterday's report and naming it with today's date if you happen to run the batch file before today's reports are created, as the folder will be empty. - Does the path for each folder actually end with Folder Log X\2009
If so, can create one loop to go through all 4 folders instead of 4 nearly identical loops. - You are using a space as the delimiter in the For loops with the DIR command. Is there a reason you specified a space?
If the file name starts with one or more spaces, they will be removed, and the file will not be renamed.
For example, if you have a report named ...New Report.xls (pretend the periods are spaces), you'll get this:
Code:
latest=New Report.xls
instead of
latest= New Report.xls
which will cause an error.
I'm assuming the folder structure looks something like this (showing just the tail end and a couple of folders for each year):
Code:
C:.
├───Folder Log 1
│ ├───2008
│ │ ├───December 08
│ │ └───November 08
│ └───2009
│ ├───August 09
│ └───September 09
├───Folder Log 2
│ ├───2008
│ │ ├───December 08
│ │ └───November 08
│ └───2009
│ ├───August 09
│ └───September 09
└───Folder Log 3
├───2008
│ ├───December 08
│ └───November 08
└───2009
├───August 09
└───September 09
I would let the report generator create the reports in the
Folder Log X folder, then have the batch file move it to the
2009\September 09 subfolder, then create a copy and rename it with today's date.
So other observations:
You've used the
:OK label in 4 different places. While this works for labels that come after the Goto, it's good practice to use unique labels to avoid confusion. And if the specified label is not found after a Goto, it will start searching from the start of the file, and jump back to the first one, which can cause unexpected results.
Plus, never can tell when that "feature" will be removed, and unique label names will be required.
It's also good practice to use a
PopD statement for each
PushD. When the batch file ends, it does not clear the stack used to store the current path so it will use a little memory, and you'll also be in the last folder you switched to, instead of the one you started from. Not a big problem as this is cleared when the Command Prompt closes, but if you are using UNC paths you can quickly run out of drive letters, as
PushD maps UNC paths to Drive letters, and these are
not released when the Command Prompt closes. They will not be removed by a reboot either, unless you've changed the Net Use setting to be
not persistant