Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a directory and copy a file

Tags:

vbscript

In VBS how do you make a directory and then copy a file into it?

Id like to make a folder in the root of C e.g. C:\folder and then copy a file from \server\folder\file.ext into that new folder

like image 851
Arcath Avatar asked Dec 10 '09 13:12

Arcath


2 Answers

Use the FileSystemObject object, namely, its CreateFolder and CopyFile methods. Basically, this is what your script will look like:

Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Create a new folder
oFSO.CreateFolder "C:\MyFolder"

' Copy a file into the new folder
' Note that the destination folder path must end with a path separator (\)
oFSO.CopyFile "\\server\folder\file.ext", "C:\MyFolder\"

You may also want to add additional logic, like checking whether the folder you want to create already exists (because CreateFolder raises an error in this case) or specifying whether or not to overwrite the file being copied. So, you can end up with this:

Const strFolder = "C:\MyFolder\", strFile = "\\server\folder\file.ext"
Const Overwrite = True
Dim oFSO

Set oFSO = CreateObject("Scripting.FileSystemObject")

If Not oFSO.FolderExists(strFolder) Then
  oFSO.CreateFolder strFolder
End If

oFSO.CopyFile strFile, strFolder, Overwrite
like image 183
Helen Avatar answered Dec 12 '22 17:12

Helen


You can use the shell for this purpose.

Set shl = CreateObject("WScript.Shell") 
shl.Run "cmd mkdir YourDir" & copy "
like image 22
Răducu's Computer Avatar answered Dec 12 '22 17:12

Răducu's Computer