Solved

Running vbs code on Windows 7 pc to copy files.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Hi Friends, These registry tweaks will help you optimizing your Windows 7 system for any VDI. This will improve the machine performanance and can be used on normal systems also. These are few registry tweaks which will add value by enhancing the …
So many times I have seen the words written in a question "if only I could show you" or " I know how hard it is for you since you can't see it" in any zone. That has inspired me to write about this tool in windows 7 called "Problem Steps Recorder…
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). …
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…

860 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