Vikas
Vikas

Reputation: 24332

How to create target directory structure for copy files in classic ASP?

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

Upvotes: 1

Views: 3100

Answers (2)

AnthonyWJones
AnthonyWJones

Reputation: 189505

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

Upvotes: 2

Tchami
Tchami

Reputation: 4787

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

Upvotes: 1

Related Questions