Solved

Copy files from network shares using a vbs script

Posted on 2010-11-24
2
923 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 250 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 250 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 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