Solved

Copy files from network shares using a vbs script

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now