views:

508

answers:

2

Hi,

I want to copy a file to target directory. It is simple with copyFile command of File system object. But I need some enhancement like,

If target directory is not exist then it'll create target directory and then copy a file.

Can you help me achieve it?

Let me know if there are other ways to do same.

Thanks.

Solution:

'Create folder if it doesn't exist
If not oFSO.FolderExists(sDestinationFolder) then
    oFSO.CreateFolder(sDestinationFolder)
End If
+1  A: 

Something like this:

Set fs=Server.CreateObject("Scripting.FileSystemObject")

//Create folder if it doesn't exist
If fs.FolderExists("YOURFOLDERPATH") != true Then
    Set f=fs.CreateFolder("YOURFOLDERPATH")
    Set f=nothing
End If

//Copy your file

set fs=nothing

W3Schools has lots of examples on how to use the FileSystemObject [here][1].

EDIT:

Set fs=Server.CreateObject("Scripting.FileSystemObject")

folders = Split("YOURFOLDERPATH", "\")
currentFolder = ""

//Create folders if they don't exist
For i = 0 To UBound(folders)
    currentFolder = currentFolder & folders(i)
    If fs.FolderExists(currentFolder) != true Then
     Set f=fs.CreateFolder(currentFolder)
     Set f=nothing  
    End If  
    currentFolder = currentFolder & "\"
Next

//Copy your file

set fs=nothing
Tchami
Well, It works for only if there is exactly one folder to create. I need this for working for multiple create folder. e.g. /export/new/new1/new2 etc. and consider that only export folder is present.
Vikas
My VB is more than a little rusty, but wouldn't this (see my latest edit) work?
Tchami
A: 

This is my basic function for this job:-

Dim gfso : Set gfso = Server.CreateObject("Scripting.FileSystemObject")

Public Sub CreateFolder(path)

  If Len(path) = 0 Then Err.Raise 1001, , "Creating path: " & path & " failed"

  If Not gfso.FolderExists(path) Then
    CreateFolder gfso.GetParentFolderName(path)
    gfso.CreateFolder path
  End If

End Sub
AnthonyWJones