Solved

Running vbs code on Windows 7 pc to copy files.

Posted on 2010-11-22
3
832 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I found out last night windows update has a problem regarding 4 latest updates that fail.  The way to get all 4 them installed is install sp1 first and restart then one by one with a resart in between as they fail every time if all the four updates …
I recently purchased an HP EliteBook 2540p notebook/laptop. It has two video ports on it – VGA and DisplayPort. HP offers an optional docking station for the 2540p that also has both a VGA port and a DisplayPort. There are numerous online reports do…
This Micro Tutorial will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.

762 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

23 Experts available now in Live!

Get 1:1 Help Now