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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append date and folder name to file

Status
Not open for further replies.

maltman1

Technical User
Nov 27, 2007
15
0
0
US
Hello all,

I am trying to append a date stamp to a file name that I am copying to another folder. Here is the script I have so far.

Code:
Dim strExtensions
Dim strExt
Dim strDate

strPath = "C:\Test\"
strDest = "C:\TestCopies\"
strExtensions = "txt"
strDate = "9/7/2010 6:00:00 PM"

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFolder = objFSO.GetFolder(strPath)
Set colSubfolders = objFolder.Subfolders
Set colFiles = objFolder.Files

For Each objSubfolder in colSubfolders   
    Set colFiles = objSubfolder.Files     
    For Each objFile in colFiles  
        For each strExt in SPLIT(UCASE(strExtensions),",")
            If RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt then
                IF objFile.DateLastModified > (strDate) THEN
                    objFile.copy strDest
                End If
            End If
        Next
    Next
Next

The script works fine in finding all text files and moves them to the second folder. However, I may have files with the same name that I do not want to overwrite. Is there anything i can add that will allow me to date stamp the file name? Also, if it is possible can I append the name of the parent folder to the file name as well?
 
Here is a simple example of how to grab the information you have requested and append it to the file name.

Code:
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

strDest = "C:\Temp\SomeFolder\"

Set objFile= objFSO.GetFile("C:\Temp\test.txt")
NameArray = Split(objFile.Name,".")
ParentFolder = objFile.ParentFolder
Parent = Right(ParentFolder,Len(ParentFolder)-InStrRev(ParentFolder,"\"))
today = Right("0" & Month(Date),2) & Right("0" & Day(Date),2) & Year(Date)
Suffix = "_" & Parent & "_" & today
objFile.Copy strDest & "\" & NameArray(0) & Suffix & "." & NameArray(1)

It will convert "test.txt" from C:\Temp to "test_Temp_09082010.txt" in the destination folder.

If you want the full parent folder path (including subdirectories) then replace this line:
Code:
Parent = Right(ParentFolder,Len(ParentFolder)-InStrRev(ParentFolder,"\"))
with this
Code:
Parent = Replace(ParentFolder,":","_")
Parent = Replace(Parent,"\","_")

That modification will produce output such as:
test_C__Temp_09082010.txt

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thank you for the reply.

I was able to incorporate what you said into my existing script. Will I be able to add a time stamp in there as well?

Here is the working script.

Code:
Dim strExtensions
Dim strExt
Dim strDate

strPath = "C:\Test\"
strDest = "C:\TestCopies\"
strExtensions = "txt"
strDate = "9/7/2010 6:00:00 PM"
strToday = Right("0" & Month(Date),2) & Right("0" & Day(Date),2) & Year(Date)

Set objFSO = CreateObject("Scripting.FileSystemObject")  
Set objFolder = objFSO.GetFolder(strPath)
Set colSubfolders = objFolder.Subfolders
Set colFiles = objFolder.Files

For Each objSubfolder in colSubfolders	
	Set colFiles = objSubfolder.Files  	
	For Each objFile in colFiles   
		For each strExt in SPLIT(UCASE(strExtensions),",")
			If RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt THEN
				IF objFile.DateLastModified < (strDate) THEN
					objFile.copy strDest & "\" & objFile.Name & "_" & strToday & "_" & objSubfolder.Name
				End If
			End If
		Next
	Next
Next
 
The 'Now()' function will return the date and time. You can use that for your timestamp. Also have a look at the string formatting functions.
 
Be careful with using Now as it will contain illegal characters for the file name just as the date did.

You will want to use somethign like this:
Code:
timstamp = Right("0" & Hour(Now()),2) & Right("0" & Minute(Now()),2) & Right("0" & Second(Now()),2)

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
I had tried the Now function but I was getting errors so I just scrapped that idea.

Also, I had appended the time stamp to the end of the date stamp but decided to create a new variable.

Thanks for the input and the script works great.
 
Actually I did have one more question. Will datelastmodified be able to compare itself to the format of the strDate? Meaning do I need to change the format of the hard coded date?
 
You would need to convert the dateLastModified to a string and compare it to the datestamp or vice versa.

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Got it

Here is what I did.

strDate = CDate("9/7/2010 6:00:00 PM")

 
Sorry to keep this going but I had one more question. What if I needed to go through multiple subfolders? The script I have seems to only look at subfolders that are in the root but will not look at subfolders inside those subfolders. Let me know if that makes sense.
 
I was just looking at that one and it looks like everything I have should be working. Yet it won't go any lower than one subfolder.
 
The code you posted only looks in subfolders (1 level deep). If you look closely at the code in the FAQ, you will see that it is a function that calls itself (recursion) so that it looks at the subfolders of the subfolders of the subfolders... etc.
 
Got it. I added a recursive function which works fine now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top