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!

Copy files to a folder based on creation date (minus one month)

Status
Not open for further replies.

tomuk1982

IS-IT--Management
Jun 16, 2011
1
GB
thread329-1485856

I have the following working really well thanks to Councilk on thread but I need to remove one month.

e.g.

Date Created: 16/06/2011
New Folder: 201105

Flag = "0"
FolderPath = "C:\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.Getfolder(FolderPath)
Set colFiles = objFolder.Files

Set objShell = CreateObject("WScript.Shell")
objShell.CurrentDirectory = "C:\"

For Each objFile In colFiles

MonthNumber = Right(("0" & Month(objFile.DateCreated)), _
((Year(objFile.Datecreated))))

YearNumber = Right(("" & Year(objFile.DateCreated)), _
(("0" & Year(objFile.DateCreated))))

Folder = YearNumber & MonthNumber

'Checks to see if the folder is already created and if not creates it

If objFSO.FolderExists(Folder) = False Then objFSO.CreateFolder Folder

'Checks to see if the file already exists in the folder and if it does deletes it before copying the new version of the file over

If objFSO.FileExists(Folder & objFile.Name) = True Then objFSO.DeleteFile Folder & objFile.Name, True

'Moves the file to the new folder

objFSO.CopyFile objFile.Path , Folder & "\" & objFile.Name

Next

'Deletes the *.csv files in the FolderPath

Set obj = CreateObject("Scripting.FileSystemObject")
obj.DeleteFile("C:\*.csv")
 
What do you mean "remove one month"?

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
I agree with Geates, have no idea what "I need to remove one month" means. Do you want the previous month? Like, the file was in June 2011, so you want 201105 (May 2011) ?

Also, that script you chose as a base for your script really isn't so hot... Do yourself a favor and replace the MonthNumber / YearNumber lines with below, which is much simpler and correct also:
Code:
MonthNumber = Right("0" & Month(objFile.DateCreated), [highlight]2[/highlight])
YearNumber = Year(objFile.DateCreated)
 
Also, because month("1/1/2011") - 1 will NOT roll over to 12/1/2010, you need to add code to account for this

Code:
YearNumber = Year(objFile.DateCreated)

if ((Month(objFile.DateCreated) - 1) = 0) then
   MonthNumber = "12"
   YearNumber = YearNumber - 1
else
   MonthNumber = Right("0" & (Month(objFile.DateCreated) - 1), 2)
end if

-Geates


"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top