?
Solved

Running vbs code on Windows 7 pc to copy files.

Posted on 2010-11-22
3
Medium Priority
?
843 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 1000 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
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). …
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

770 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