Solved

Running vbs code on Windows 7 pc to copy files.

Posted on 2010-11-22
3
841 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Article by: Lee
Windows 7 Ultimate and Enterprise (and 2008 R2) introduced a new feature you may not be aware of - Boot from VHD.   Boot from VHD (or what Microsoft refers to asNative Boot allows you to install Windows to a VHD (Virtual Hard Disk) file that is t…
A quick guide on how to use Group Policy to create a custom power plan and set it active on Windows 7.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…

724 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