Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving folders based on date 1

Status
Not open for further replies.

Daywatch

MIS
Aug 12, 2009
7
CA
I need to come up with a script to do the following:

I have a folder on a server in which people scan documents to PDF. The folders are simply named for the day (for example 2009-08-12 for August 12, 2009). At the end of the day, I would like to move the daily folder into a monthly folder (currently formatted as 2009-08 for August 2009). If possible, I would also like to have the script create a new monthly folder automatically (so when 2009-09-01 gets copied, a 2009-09 folder would be created if it doesn't exist).

I started trying to piece together a script from various Google searches, but my scripting kung-fu is weak, and this is as far as I got before I realized I need help...

-----------------------------------------------------------

Option Explicit

dim destination
dim sourcefolder

destination = "c:\scanusers\dataentry\completed archive\" & Year(Date) & "-" & Month(Date) & "-" & Day(Date)
sourcefolder = "c:\scanusers\dataentry\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder(destination)
objFSO.MoveFolder sourcefolder, destination

-----------------------------------------------------------

I know there is lots I am missing, but I am unsure of where to go next. Any assistance would be greatly appreciated!
 
I think you were on the right track. I used variable names that made more sense to me for what you're trying to do but you can change them back if you prefer.

The Dir() function tests if a file or folder exists and returns blank if not. In this case, you can use it to test if the Monthly folder exists. If not, create it.

Code:
Dim fldDaily
Dim fldMonth

fldDaily = "c:\scanusers\dataentry\completed archive\" & Year(Date) & "-" & Month(Date) & "-" & Day(Date)
fldMonth = "c:\scanusers\dataentry\" & Year(Date) & "-" & Month(Date)

Set objFSO = CreateObject("Scripting.FileSystemObject")

If Dir(fldMonth)="" Then
   Set oFldMonth = objFSO.CreateFolder(fldMonth)
End If
objFSO.MoveFolder fDaily, fldMonth
 
This is the 2nd time I see Dir(). It is not vbscript, mind you.
 
does the CreateFolder method create the folder tree if it is not already there? im not so sure it does, you might need to sneak in a recurse or be sandal&slippers and use the shell
 
Sorry, thought I'd used Dir in vbscript before...

Code:
Dim fldDaily
Dim fldMonth

fldDaily = "c:\scanusers\dataentry\completed archive\" & Year(Date) & "-" & Month(Date) & "-" & Day(Date)
fldMonth = "c:\scanusers\dataentry\" & Year(Date) & "-" & Month(Date)

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(fldMonth) Then
   Set oFldMonth = objFSO.CreateFolder(fldMonth)
End If

objFSO.MoveFolder fDaily, fldMonth
 
Thanks for the help. This is throwing an error on the last line 'Path not found'. I was getting the same thing with my attempts as well. I tried removing the '& Year...' from each line as a test, but still got the same error.

I did notice a typo in the objFSO.MoveFolder command that I fixed (changed fDaily to fldDaily, but that didn't seem to fix it...

Could the error be because I am trying to move a folder from one sub-directory level to another sub-directory nested further below it?
 
whoops...forgot the NOT.

Code:
Dim fldDaily
Dim fldMonth

fldDaily = "c:\scanusers\dataentry\completed archive\" & Year(Date) & "-" & Month(Date) & "-" & Day(Date)
fldMonth = "c:\scanusers\dataentry\" & Year(Date) & "-" & Month(Date)

Set objFSO = CreateObject("Scripting.FileSystemObject")

If [COLOR=red]Not[/color] objFSO.FolderExists(fldMonth) Then
   Set oFldMonth = objFSO.CreateFolder(fldMonth)
End If

objFSO.MoveFolder fldDaily, fldMonth
 
I think I am getting closer - the script will create the new folder in the correct place, but it is not using leading zeroes in the date, so I think that is why it is throwing the 'Path not found' errors?

Example: When the script creates the new folder, it is in the format 2009-8, instead of 2009-08, so I think it is crapping out because it doesn't see the 2009-08-13 folder correctly?

I did some more searching, and changed the (Date) parts to (Now) as I saw in some other posts, but that didn't work.

Do I need to add a function to define all the parts of the date separately, and pad with a zero? I tried to add that on my own, but it didn't work out too well... but I will keep trying.

:)
 
Closer still... I can now create the desired monthly folder, but I get a 'File already exists' error. I did some more testing and modified the script a little, and I think it is almost there...

------------------------------------------------------------
Dim fldDaily
Dim fldMonth

fldDaily = "d:\scanusers\dataentry\" & Year(Now) & "-" & Right("0" & Month(Now),2) & "-" & Right("0" & Day(Now),2)
fldMonth = "d:\scanusers\dataentry\completed archive\"
'& Year(Now) & "-" & Right("0" & Month(Now),2)

Set objFSO = CreateObject("Scripting.FileSystemObject")

If Not objFSO.FolderExists(fldMonth) Then
Set oFldMonth = objFSO.CreateFolder(fldMonth)
End If

objFSO.MoveFolder fldDaily, fldMonth

-----------------------------------------------------------

I took the '& Year(Now) & "-" & Right("0" & Month(Now),2)' off the end of fldmonth, and when I run the script it will move the 2009-08-13 folder to the right spot. Now I need to figure out how to get it to move to the proper monthly folder.
 
hmm...did you check to make sure the monthly folder is empty before you run the script? It sounds like the today's daily folder is inside of the monthly folder already.
 
That was one thing I was thinking, but I created the folder structure locally for testing, and the folder where the monthly folders are located is empty prior to running the script. When I run the script, it creates the proper monthly folder (2009-08), but then won't copy the daily folder into the monthly folder that was created.

When I remove the "& Year(Now) & "-" & Right("0" & Month(Now),2)" from the fldmonth line, the daily folder will move properly it just doesn't end up in a monthly folder.

Now if I can only get it all to work together... :)
 
try:
Code:
fldMonth = "c:\scanusers\dataentry\" & Year(Date) & "-" & Month(Date) & "\"
 
Success! Works like a charm!

Thanks very much for all your help!

I had to make one other small change - I forgot that when I get around to moving the folder it is likely to be past midnight, so I had to add a '-1' to the day portion of the date, so it goes back one day.

Do you think this will work with month-end days as well? That is, on September 1st will it know to copy the files for August 31st into the August folder, or will I need to code something in for that?

In either case, this is working now, so I have some time to work out the rest.

Thanks again! [thumbsup2]
 
I don't think this code will account for days previous since it's determining the folder paths off of today's date. You might be able to do something that analyzes the completed archives folder and moves subfolders based on name.
 
will I need to code something in for that
What is YOUR actual code ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
This is the script as I have it now, and it will successfully move a folder from the previous day to the current monthly folder (ie. on August 19th (2009-08-19) it will take the folder named 2009-08-18 and move it to the monthly folder 2009-08). I plan to schedule this to run Tuesday through Friday.

I have created a second file I am going to schedule on Mondays to go back 2 days and grab the folder from Friday (there are no files created on Saturdays or Sundays).

Dim fldDaily
Dim fldMonth

fldDaily = "d:\scanusers\dataentry\" & Year(Now) & "-" & Right("0" & Month(Now), 2) & "-" & Right("0" & Day(Now-1), 2)
fldMonth = "d:\scanusers\dataentry\completed archive\" & Year(Now) & "-" & Right("0" & Month(Now), 2) & "\"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If Not objFSO.FolderExists(fldMonth) Then
Set oFldMonth = objFSO.CreateFolder(fldMonth)
End If

objFSO.MoveFolder fldDaily , fldMonth

I am now looking into adding logic to account for month end, etc. so on September 1st the script will go back to August 31st and put the files in the correct monthly folder. I am going to try adding this logic by adapting some code I have found from various searches.

PHV - I was not asking for anyone to write the code for me, I was just asking a question... Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top