Loading...
 
JoiWiki » Developer » Using Windows » Windows Scripting » Windows Script Snippets » Moving files with VBScript Moving files with VBScript

Moving files with VBScript

Moving files with VBScript

 

Copying files from one place to another can be a pain when it needs to be done repeatedly, for example when a series of output files like letters or grabbing the latest test results, especially if you’ve got to fish them out of a folder with a lot in it, this is where a quick VBScript can work wonders. As is so often the case this was adapted from code found online (there’s no need to re-invent the wheel) from Tester101 on StackOverflow.

There are a few tweaks though, the below will take in a list of files to transfer from a source folder to a specific destination and it will skip any files that don’t exist. it could easily be adapted to take in an array of output folders or write a log to a new text file but those can be enhancements for another day, here’s the script. To use it save the code into a text file and set the extension to be .vbs:

Dim sourceFolder, destFolder, fileArr 
 
sourceFolder = "C:\Users\ThisUser\Documents\Source\"
destFolder = "F:\Work\Subject\Research\"
' Files to copy across
fileArr = Array("FirstSubjectPaper.docx"_
                ,"Thesis.docx")
 
copyFiles sourceFolder, destFolder, fileArr
 
sub copyFiles(sourceFolder, destFolder, fileArr)                    
  Set fso = CreateObject("Scripting.FileSystemObject")
  for i = 0 to ubound(fileArr)
     Dim SourceFile
     Dim DestinationFile
     SourceFile = sourceFolder & fileArr(i)
     DestinationFile = destFolder & fileArr(i)
 
     ' Check to see if source file exists
     If fso.FileExists(SourceFile) Then
        'Check to see if the file already exists in the destination folder
        If fso.FileExists(DestinationFile) Then
           'Check to see if the file is read-only
           If Not fso.GetFile(DestinationFile).Attributes And 1 Then 
              'The file exists and is not read-only.  Safe to replace the file.
              fso.CopyFile SourceFile, DestinationFile, True
              msgbox "Copied: " & fileArr(i), 0, "Success"
           Else 
              'The file exists and is read-only.
              'Remove the read-only attribute
              fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes - 1
              'Replace the file
              fso.CopyFile SourceFile, DestinationFile, True
              'Reapply the read-only attribute
              fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes + 1
              msgbox "Copied: " & fileArr(i), 0, "Success"
           End If
        Else
           'The file does not exist in the destination folder.  Safe to copy file to this folder.
           fso.CopyFile SourceFile, DestinationFile, True
           msgbox "Copied: " & fileArr(i), 0, "Success"
        End If
     Else
        'The source file does not exist don't do mutch
        msgbox "Source file doesn't exist: " & fileArr(i) & vbNewLine & "Skipping.." , 0, "Ah.."
     End If    
  next
  Set fso = Nothing
end sub

 

Created by admin. Last Modification: Monday December 10, 2018 21:19:46 GMT by admin.

Developer