0

I'm having a little issue creating a folder with a name that uses the current system date. I found a Date function but I can't use it (I keep getting "Path not found" error). follows my code:

directory = "C:\Users\f8057612\Desktop\Bancos\Script_Operadoras\TEST\TESTFOLDER" & " - Day " & Date() & "\"

Set FSO = CreateObject("Scripting.FileSystemObject")    

If Not FSO.FolderExists(directory) Then
    FSO.CreateFolder directory 
End If  

How could I use the "Date" function? Any other way of creating a folder with the system date on it is also welcome! Thanks!

  • 1
    Please specify *exactly*: (1) How should the date part look like (2) Why can't you use Date() – Ekkehard.Horner Jan 17 '14 at 17:32
  • (1) Year(4 digits)/Month(2digits)/Day(2digits). (2) When I use that "Date()" there i get an error. I can test now to see what error it was, but I suppose We cant just say "Date()" –  Jan 17 '14 at 17:37

2 Answers2

3

The syntax for string concatenation is

sA & sB

Compare that to your:

directory = "C:\Users\...\TESTFOLDER" & " - Day " Date() & "\"

To deal with the format part of your problem, see if you can't get an idea from this answer.

Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • I tried with the "&" that was missing... I still have an error in the "FSO.CreateFolder directory " line. It's a "Path not Found" –  Jan 17 '14 at 17:58
  • 1
    @CharlieVelez You get a `path not found` error, because your date format looks like this: `yyyy/mm/dd`. `FileSystemObject` methods consider both ``\`` and `/` as path separators, so you're trying to create a subfolder `dd` in a subfolder `mm` in a folder `...yyyy`, but `CreateFolder` can't create folders recursively. Replace `/` with a save character (e.g. `-`): `directory = "C:\Users\... - Day " & Replace(Date, "/", "-")`. – Ansgar Wiechers Jan 17 '14 at 18:59
-1

Date() most likely returned some characters that can not be used as a folder name. Try wrap the Date() with function Format.

Format(Date(), "yyyy.mm.dd")

PatricK
  • 6,375
  • 1
  • 21
  • 25
  • I'm trying making a variable receive this function as you told and placing it in the program, but i am having a "type mismatch: [:Format". Maybe it's function name is wrong? I'll take a look in MSDN... –  Jan 17 '14 at 18:36
  • VBScript doesn't have a built-in function `Format()`. – Ansgar Wiechers Jan 17 '14 at 18:53
  • Sorry, it may only work within vba. Doing this on my BlackBerry, try `Replace(CStr(Date()),"/",".")` – PatricK Jan 17 '14 at 18:54