Solved

Running vbs code on Windows 7 pc to copy files.

Posted on 2010-11-22
3
835 Views
Last Modified: 2012-05-10
I want to run a simple vbs script to copy files from one folder to another using fso.  

I’m still learning windows 7 and I’m wondering how to run this code and not have issues with permissions.  The folders all reside on the same usb drive.  The code is attached.

IF I need to invoke administrator privileges , how do I do this using a script?  Since this script will be run often I can’t be at the pc to manually set privileges

' ================= Copy Bkup Files.vbs ================

Option Explicit

Dim intDayOfWeek
Dim datTodaysDateYYYYMMDD
Dim strBackupFileName
Dim strWeekDayName
Dim strSourcePath
Dim strDestinationPath
Dim strLogPath
Dim strErrorFilePath
Dim strFullSourcePath
Dim strErrorFileName
Dim strLogDestinationPath
Dim strErrorMsg
Dim strDummy

Dim intAppendData
Dim intOverWrite

intDayOfWeek = WeekDay(date)

strWeekDayName = fnDayOfWeek(intDayOfWeek)

If strWeekDayName <> "FAILED" Then
  ' == Copy xw6Matrox bkup file
  strDestinationPath	=	"\\sbs2003server\Backup200\xw6MatroxBkupStrg\xw6Matrox Backup Strg\"  & strWeekDayName & "\"
  strSourcePath		= 	"\\backup100\NetFolder\xw6MatroxBkup\"
  strBackupFileName	=	"xw6Matrox_4Oct2008.bkf"
  strErrorFilePath	=	"D:\Batch\Errors\"
  strErrorFileName	=	"xw6Matrox_Copy_Bkup_ErrorFile.txt"
  intAppendData		=	8 'If file exists open with Append
  intOverWrite		=	2 'Overwrite error file
  
  strFullSourcePath	= strSourcePath & strBackupFileName
  
  strDummy	= fnCopyFile(strFullSourcePath, strDestinationPath, strBackupFileName, strErrorFilePath, strErrorFileName)
End If 


Wscript.Quit ()

Function fnDayOfWeek(intDayOfWeek)
  Dim  strWeekDayText
  
  fnDayOfWeek = "FAILED"

  If intDayOfWeek	= 1 Then
    strWeekDayText	= "Sunday"     
  ElseIf  intDayOfWeek	= 2 Then
    strWeekDayText	= "Monday"     
  ElseIf  intDayOfWeek	= 3 Then
    strWeekDayText	= "Tuesday"     
  ElseIf  intDayOfWeek	= 4 Then
    strWeekDayText	= "Wednesday"     
  ElseIf  intDayOfWeek	= 5 Then
    strWeekDayText	= "Thursday"     
  ElseIf  intDayOfWeek	= 6 Then
    strWeekDayText	= "Friday"     
  ElseIf  intDayOfWeek	= 7 Then
    strWeekDayText	= "Saturday"     
  Else 
    strWeekDayText	= "FAILED"  
  End If

      
    fnDayOfWeek	=	strWeekDayText
    	  
  
End Function

Function fnCopyFile(strSourcePath, strDestinationPath, strBackupFileName, strErrorFilePath, strErrorFileName)
  Dim oFso
  Dim objFolder
  Dim strDummy
  Dim strFullDestPath
  Dim strErrorMsg
  
  fnCopyFile	=	 "TRUE"
  
  Set oFso	=	Wscript.CreateObject("Scripting.FileSystemObject")
  If (oFso.FolderExists(strDestinationPath)) Then

    strFullDestPath	= strDestinationPath & strBackupFileName
    
    ' Set objFolder	= oFso.GetFile(strSourcePath)   << OLD LINE
    If oFso.FileExists(strFullSourcePath) Then
       Set objFolder	= oFso.GetFile(strFullSourcePath)
       objFolder.Copy(strFullDestPath)
    Else
       strErrorMsg	= "Source file " & strFullSourcePath & " Does NOT exist : Backup Failed?"
       strDummy = fnError(strErrorFilePath, strErrorFileName, intAppendData, intOverWrite, strErrorMsg)
    End If
    Set objFolder	= 	Nothing
  Else
    strErrorMsg		= "Path " & strDestinationPath & " Does NOT exist"
    strDummy	= fnError(strErrorFilePath, strErrorFileName, intAppendData, intOverWrite, strErrorMsg)
    fnCopyFile	=	"FAILED"
  End If
  
  
End Function

Function fnDeleteErrorFile(strErrorFilePath, strErrorFileName)
  Dim oFso
  Dim objLogFile
  
  Dim strFullPath
  
  strFullPath	=	strErrorFilePath & strErrorFileName
  
  Set oFso	=	Wscript.CreateObject("Scripting.FileSystemObject")
  If (oFso.FileExists(strFullPath)) Then
    oFso.DeleteFile strFullPath
  End If 
End Function

Function fnError(strErrorFilePath, strErrorFileName, intAppendData, intOverWrite, strErrorMsg)
  Dim oFso
  Dim objLogFile
  
  Dim strFullPath
  
  strFullPath	=	strErrorFilePath & strErrorFileName
  
  Set oFso	=	Wscript.CreateObject("Scripting.FileSystemObject")
  If (oFso.FileExists(strFullPath)) Then
    Set objLogFile	= oFso.OpenTextFile(strFullPath, intAppendData)
  Else
    Set objLogFile	= oFso.OpenTextFile(strFullPath, intOverWrite, "True")
  End If
  
  objLogFile.WriteLine(Date)
  objLogFile.WriteLine(strErrorMsg)
  
  objLogFile.Close()
  
End Function

Open in new window

0
Comment
Question by:donpick
3 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 250 total points
ID: 34191018
I don't think that Windows will automatically detect that Administrator privileges are required of a script, the file copy would simply fail with an access denied error.

It doesn't appear you're writing to or reading from any "special" areas protected by UAC, so the "Requires Elevation Prompt" shouldn't be an issue, you just need to either handle the case where files aren't copied due to permissions, or ensure you have the appropriate file permissions.

Technically, you don't need to be an administrator - so long as whatever user is running the script has access to the files in question.
0
 
LVL 12

Assisted Solution

by:John Griffith
John Griffith earned 250 total points
ID: 34191259
To run the VBS script at elevated admin level, call it from a batch file.

Contents of batch script -  

cscript /nologo copyfiles.vbs

Then RIGHT-click on the batch file, select "Run as Administrator"

Regards. . .

jcgriff2
0
 

Author Closing Comment

by:donpick
ID: 34191614
Thank you for fast answers
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
A quick guide on how to use Group Policy to create a custom power plan and set it active on Windows 7.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.

867 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

21 Experts available now in Live!

Get 1:1 Help Now