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!

Trying to copy files to a folder based on creation date

Status
Not open for further replies.

Councilk

MIS
May 14, 2003
72
US
Hello all,

I'm trying to put together a script to copy files to a folder based on the date.

The script needs to look at the month and year and based on this information, the folder with the corresponding month is created and files copied to it.

This is what I've come up with so far - see script below

========================Script==============================
'Option Explicit
'On error resume next

Dim FolderPath ' folder to be searched for files
Dim objFSO
Dim objFolder
Dim colFiles
Dim objFile

SearchStrJan = "1"
SearchStrFeb = "2"
SearchStrMar = "3"
SearchStrApr = "4"
SearchStrMay = "5"
SearchStrJun = "6"
SearchStrJul = "7"
SearchStrAug = "8"
SearchStrSep = "9"
SearchStrOct = "10"
SearchStrNov = "11"
SearchStrDec = "12"


FolderPath = "C:\temp"

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

For Each objFile In colFiles
Wscript.Echo objFile.DateCreated

If Instr objFile.DateCreated
Next

'====== Trying to get going with string functions and file properties

Any assistance with this is greatly appreciated

Thanks all

 
I have not fully tested this.

Code:
Private Sub Command1_Click()
FolderPath = "C:\temp\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.Getfolder(FolderPath)
Set colFiles = objFolder.Files
For Each objFile In colFiles
    MsgBox objFile.DateCreated
    NewFolder = FolderPath & Format(Month(objFile.DateCreated), "00") & _
    Year(objFile.DateCreated) & "\"
    ' Checks to see if the folder is already created
    ' and if not creates it
    If objFSO.FolderExists(NewFolder) = False Then objFSO.CreateFolder NewFolder
    ' 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(NewFolder & objFile.Name) Then objFSO.DeleteFile NewFolder & objFile.Name, True
    ' Moves the file to the new folder
    objFSO.MoveFile objFile, NewFolder & objFile.Name
Next
End Sub

Swi
 
Hi SWI

Thanks for the code and I'm testing this now where I'mreceiving a "Type mismatch: 'Format' on the "Format" method located on line 8

It does display the date and I was getting that much from the code I posted in the opening thread.

I'll keep looking for descriptions of this error to see if I can get around it

THX! :)
 
Shoot, that is what I get for using the VB6 IDE. The format function does not exist in VBScript. Try this to get the new folder name:

Code:
    NewFolder = FolderPath & Right(("0" & Month(objFile.DateCreated)), 2) & _
    Year(objFile.DateCreated) & "\"



Swi
 
I was trying to zero fill the month so the folders looked uniformly named. You could also put a dash in between the month and year if you would like.

Code:
    NewFolder = FolderPath & Right(("0" & Month(objFile.DateCreated)), 2) [COLOR=red]& "-" [/color]& _
    Year(objFile.DateCreated) & "\"

Swi
 
Councilk,

Did everything work out for you?

Swi
 
Hi Swi,

It's working mostly, but I still need to fine tune it bacause it looks at the modified date on one file and the creation on others.

But for the most part, it's doing pretty well

Thx for your help in getting me going in the right direction


 
How will you determine what date to look at for each file?

Swi
 
Hello Swi

I'm using the script to look through in a specific location and create a collection files.

The next thing the script does is cycle through the files and creates a corresponding folder based on the creation date taking the month and year.

I had to modify the code here in red due to the folder being created with only a "0" for the name

See code below

================== FileCopy script =======================

'Private Sub Command1_Click()

Flag = "0"

FolderPath = "." ' ts and Settings\USPFF\My Documents"

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.Getfolder(FolderPath)

Set colFiles = objFolder.Files

For Each objFile In colFiles

' MsgBox objFile.DateCreated

NewFolder = Right(("0" & Month(objFile.DateCreated)), _
((year(objFile.Datecreated))))

' NewFolder = Right(("0" & Month(objFile.DateCreated)) & Right(("0" & Year(objFile.DateCreated))))

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

Myfolder = NewFolder & mine

' MsgBox(myfolder)

' Checks to see if the folder is already created

' and if not creates it

' If objFSO.FolderExists(NewFolder) = False Then objFSO.CreateFolder NewFolder

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

' 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(NewFolder & objFile.Name) = True Then objFSO.DeleteFile NewFolder & objFile.Name, True

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

' Moves the file to the new folder

'objFSO.CopyFile objFile.Path, objFile.Name, NewFolder

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

Next

'MsgBox("This script will terminate on a" & " " & flag & " " & "result")

'End Sub
 
Hello Swi

I'm using the script to look through in a specific location and create a collection files.

The next thing the script does is cycle through the files and creates a corresponding folder based on the creation date taking the month and year.

I had to modify the code here in red due to the folder being created with only a "0" for the name

See code below and you may be able to find why I get the folder named in this manner

072008 and any files created in the month of July with the year 2008 is copied into this folder

Thanks for any input on this item



================== FileCopy script =======================

'Private Sub Command1_Click()

Flag = "0"

FolderPath = "." ' ts and Settings\USPFF\My Documents"

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.Getfolder(FolderPath)

Set colFiles = objFolder.Files

For Each objFile In colFiles

' MsgBox objFile.DateCreated

NewFolder = Right(("0" & Month(objFile.DateCreated)), _
((year(objFile.Datecreated))))

' NewFolder = Right(("0" & Month(objFile.DateCreated)) & Right(("0" & Year(objFile.DateCreated))))

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

Myfolder = NewFolder & mine

' MsgBox(myfolder)

' Checks to see if the folder is already created

' and if not creates it

' If objFSO.FolderExists(NewFolder) = False Then objFSO.CreateFolder NewFolder

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

' 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(NewFolder & objFile.Name) = True Then objFSO.DeleteFile NewFolder & objFile.Name, True

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

' Moves the file to the new folder

'objFSO.CopyFile objFile.Path, objFile.Name, NewFolder

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

Next

'MsgBox("This script will terminate on a" & " " & flag & " " & "result")

'End Sub
 
072008
Right("0" & Month(objFile.DateCreated), 2) & Year(objFile.DateCreated)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top