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

Create folder named according to date & time

Status
Not open for further replies.

lauraSatellite

Technical User
Jul 22, 2004
35
IE
Hi, im altering a script that creates a specifically named folder:

Sub CreateFolders(strPath)

Dim objFso
Dim arrFolders
Dim strDrive
Dim strFolder

Set objFso = CreateObject("Scripting.FileSystemObject")
' Format path (remove leading and trailing spaces, and final backslash)
strPath = Trim(strPath)
If Right(strPath, 1) = "\" Then
strPath = Left(strPath, Len(strPath) - 1)
End If
' Check that a drive is specified and that it exists (if not, exit sub)
If Mid(strPath, 2, 2) <> ":\" Then
WScript.Echo "The path specified is invalid."
Exit Sub
Else
strDrive = Left(strPath, 1)
If Not objFso.DriveExists(strDrive) Then
WScript.Echo "The drive specified does not exist."
Exit Sub
End If
End If
' Split the path into an array, first element is the drive, subsequent elements are the folders
arrFolders = Split(strPath, "\")
' Build the path, folder by folder. If folder doesn't exist, create it
strFolder = arrFolders(0)
For i = LBound(arrFolders) To UBound(arrFolders) - 1
strFolder = strFolder & "\" & arrFolders(i+1)
If Not objFso.FolderExists(strFolder) Then
objFso.CreateFolder(strFolder)
End If
Next
End Sub

If the param strPath is set to "C:\theNewFolder" the script works fine.
I have been tryin to create a folder named according to the date and time. I tried:

strDateTime = FormatDateTime(now, 3)
strPathParam = "C:\" & strDateTime
CreateFolders strPathParam

The following error is returned:

Line:38
Char:1
Error: Invalid procedure call or argument
Code: 800A0005
Source: MS VBS runtime error

Anyone have any ideas as to what im doing wrong?
I know it must be something small but its driving me dotty..
 
The : character is illegal in folder names.
You may try this:
strDateTime = Replace(FormatDateTime(Now, 3), ":", "_")


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top