Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Copy files from network shares using a vbs script

Posted on 2010-11-24
2
Medium Priority
?
952 Views
Last Modified: 2012-05-10
Hi
Is there a way to modify the below script to include a specific username and password (this user account will have permisiions to access both the source and destination network shares. Thanks

 Dim sOriginFolder, sDestinationFolder, sFile, oFSO, oShell  
 Set oFSO = CreateObject("Scripting.FileSystemObject")
 Set oShell = CreateObject("Wscript.Shell")
 sOriginFolder = "networkfoldera"
 sDestinationFolder = "networkfolderb"
 On Error Resume Next  
  sLastTime = oShell.RegRead("HKLM\SYSTEM\LastTimeCopied")
  iErr = Err.Number
 On Error GoTo 0
  If iErr <> 0 Then
   oShell.RegWrite "HKLM\SYSTEM\LastTimeCopied",Now,"REG_SZ"
   Call CopyFiles("ALL")
  Else
   oShell.RegWrite "HKLM\SYSTEM\LastTimeCopied",Now,"REG_SZ"
   Call CopyFiles(sLastTime)
  End If  
   
 Sub CopyFiles(sArg)
   If sArg = "ALL" Then
     For Each sFile In oFSO.GetFolder(sOriginFolder).Files
       If Not oFSO.FileExists(sDestinationFolder & "\" & oFSO.GetFileName(sFile)) Then
         oFSO.GetFile(sFile).Copy sDestinationFolder & "\" & oFSO.GetFileName(sFile),True
        End If
     Next
   Else
     For Each sFile In oFSO.GetFolder(sOriginFolder).Files
       If Not oFSO.FileExists(sDestinationFolder & "\" & oFSO.GetFileName(sFile)) Then
         If DateDiff("s",sLastTime,oFSO.GetFile(sFile).DateCreated) > 0 Then  
           oFSO.GetFile(sFile).Copy sDestinationFolder & "\" & oFSO.GetFileName(sFile),True
           
          End If
       End If  
     Next  
   End If  
0
Comment
Question by:kwatt562
2 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 1000 total points
ID: 34210701
Hi, you can map a network drive using the alternate credentials:

strUser = "domain\user"
strPass = "password"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive "Y:", "\\server\source", False, strUser, strPass
objNetwork.MapNetworkDrive "Z:", "\\server\destination", False, strUser, strPass

Then you can copy files between the drives.

Regards,

Rob.
0
 
LVL 5

Assisted Solution

by:MaxSoullard
MaxSoullard earned 1000 total points
ID: 34212507
You can use this script that uses RunAs.

Save your script on the same folder as this script. Instantiate the variables.

The password will be asked once and will be saved by RunAs which is a more secure way than having you password on a script.
strUser = "USER_NAME"
strDomain = "DOMAIN_NAME"
strScriptName = "SCRIPT_NAME.vbs" ' The name of the script you want to run with anotehr account
strCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
strRunAs ="runas /savecred /user:" & strDomain & "\" & strUser&" " & Chr(34)& "cscript.exe "& Chr(34)& strCurPath & "\"& strScriptName & Chr(34)

Set WshShell = CreateObject("WScript.Shell")

WshShell.Run (strRunAs)

Open in new window

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question