Solved

Login script checks free space and then copies files

Posted on 2012-03-29
22
361 Views
Last Modified: 2014-03-25
I will be adding a line into everybody's scripts which copies our custom help files onto their c: drive. I would like some help with this. All my computers are either WinXP or Win7, so the script will need to work on both.

I would like the script to verify there is at least 300mb of free space, if so it will copy files from a network share, if not it will produce a message something like "There is not enough free space to copy the Help files, please contact MIS" and exit.

Thanks!
0
Comment
Question by:hartwellcorp
  • 11
  • 8
22 Comments
 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
Why copy to the machines?  Why not place a shortcut to a network share?  Or a make the help files a series of web pages so they are easily accessed?

And how do you allow your workstations to get so low on disk space?  Don't you run management software that alerts you to this problem?  If not, consider it - you can use SpiceWorks for free and it can alert you to issues with disk space.

Next question, what is the script written in?  Batch?  VB?  Kix?  Something else?
0
 

Author Comment

by:hartwellcorp
Comment Utility
Standard batch file launched at login through group policy.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
Comment Utility
Hi, you can try this VBS code.

Regards,

Rob

' Specify threshold in MB
intThreshold = 300
strHelpFiles = "\\server\share\HelpFiles\"
strDestination = "C:\HelpFiles\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colDisks = objWMIService.ExecQuery("Select FreeSpace from Win32_LogicalDisk Where DeviceID='C:'")
For Each objDisk In colDisks
	' Get free space in MB
	intFreeSpace = Round(objDisk.FreeSpace / (1024 * 1024), 3)
Next
If intFreeSpace < intThreshold Then
	MsgBox "You have less than " & intThreshold & " MB of free space on C Drive. Please contact MIS"
Else
	If objFSO.FolderExists(strDestination) = False Then objFSO.CreateFolder strDestination
	objFSO.CopyFolder strHelpFiles, strDestination, True
End If

Open in new window

0
 

Author Comment

by:hartwellcorp
Comment Utility
This seems to be working, trying it out on a few computers and I'll post shortly.
0
 

Author Comment

by:hartwellcorp
Comment Utility
okay, this script seems to be working but just needs a couple tweaks maybe. for some reason, some folks are getting "invalid procedure call or argument" on line 17 char 2 (code:800a0005). Also, folks are running this every time they login so I wonder if there is a way to copy only newer files? thanks.
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Is your strDestination folder more than one level deep?  There's a chance that if it contains two levels that both don't exist, the CreateFolder method will fail, so we'll need to build the tree instead.  I have a function for that.

To copy only newer files, we'll need to iterate through each file in the source and target and check their last modified dates.  This can be done, but I'd like to know whether the HelpFiles folder contains subfolders or not?  Or if not, if you plan to have it contain any subfolders in the future?  If you do, this will require another small routine to go through each subfolder.

Ultimately though, since it's getting more complex for VBScript, have you though about using RoboCopy to copy the files?

Something like this should do the job:
' Specify threshold in MB
intThreshold = 300
strHelpFiles = "\\server\share\HelpFiles\"
strDestination = "C:\HelpFiles\"
strRoboCopy = "\\server\share\RoboCopy.exe"

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colDisks = objWMIService.ExecQuery("Select FreeSpace from Win32_LogicalDisk Where DeviceID='C:'")
For Each objDisk In colDisks
	' Get free space in MB
	intFreeSpace = Round(objDisk.FreeSpace / (1024 * 1024), 3)
Next
If intFreeSpace < intThreshold Then
	MsgBox "You have less than " & intThreshold & " MB of free space on C Drive. Please contact MIS"
Else
	strCommand = objFSO.GetFile(strRoboCopy).ShortPath & " """ & strHelpFiles & """ """ & strDestination & " /MIR /Z /W:5"
	objShell.Run strCommand, 0, True
End If

Open in new window


Regards,

Rob.
0
 

Author Comment

by:hartwellcorp
Comment Utility
Robocopy sounds like a good idea. So this will skip a file if it's already on the destination but it will copy any newer files based on modified date right?
0
 

Author Comment

by:hartwellcorp
Comment Utility
okay, so i tried the new robocopy script and nothing happens. at first, i got an error that the file could not be found, so i corrected the strRoboCopy. I tried it again and now nothing happens, no error message, no event log, no help files copied to the c: drive. hmm, what did i do wrong?
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
OK, try changing this:
      strCommand = objFSO.GetFile(strRoboCopy).ShortPath & " """ & strHelpFiles & """ """ & strDestination & " /MIR /Z /W:5"

to this
      strCommand = objFSO.GetFile(strRoboCopy).ShortPath & " """ & strHelpFiles & """ """ & strDestination & " /MIR /Z /W:5 /LOG:C:\Temp\RobocopyLog.log"

and then after running the script, open the RobocopyLog.log file and see what it says.

Regards,

Rob.
0
 

Author Comment

by:hartwellcorp
Comment Utility
I tried that and the log file is never created. I ran the script on another XP computer and i see this:

WINDOWS SCRIPT HOST
 
Script:\\hcterm\pub\copyavantehelp\copyavanetehelp.vbs
Line: 19
Char: 2
Error: property name is invalid
Code: 800700c1
Source: msxml3.d11
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:hartwellcorp
Comment Utility
here is what i tried from the command line and it worked:


C:\>\\hcterm\pub\copyavantehelp\robocopy \\hcnas\avantehelp\avante94 c:\avantete
st\ /MIR /Z /W:5 /LOG:f:\RobocopyLog.log
f:\RobocopyLog.log


here is what we have in the script so far and its failing. On my computer i get nothing at all. on some other computers they get an error:

' Specify threshold in MB
intThreshold = 200
strHelpFiles = "\\hcnas\avantehelp\Avante94"
strDestination = "C:\avantehelp\"
strRoboCopy = "\\hcterm\pub\copyavantehelp\RoboCopy.exe"

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colDisks = objWMIService.ExecQuery("Select FreeSpace from Win32_LogicalDisk Where DeviceID='C:'")
For Each objDisk In colDisks
      ' Get free space in MB
      intFreeSpace = Round(objDisk.FreeSpace / (1024 * 1024), 3)
Next
If intFreeSpace < intThreshold Then
      MsgBox "You have less than " & intThreshold & " MB of free space on C Drive. Please contact MIS"
Else
     strCommand = objFSO.GetFile(strRoboCopy).ShortPath & " """ & strHelpFiles & """ """ & strDestination & " /MIR /Z /W:5 /LOG:f:\Temp\RobocopyLog.log"
      objShell.Run strCommand, 0, True
End If
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Gee, that's odd.  Above this:
      objShell.Run strCommand, 0, True

put this:
strCommand = InputBox("About to run", "About to run", strCommand)

then when that inputbox appears, copy the text, and paste it directly to a command prompt to run it manually and see what Robocopy says.

Regards,

Rob.
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Try this code too.

' Specify threshold in MB
intThreshold = 200
strHelpFiles = "\\hcnas\avantehelp\Avante94"
strDestination = "C:\avantehelp\"
strRoboCopy = "\\hcterm\pub\copyavantehelp\RoboCopy.exe"
strLogFile = "F:\RobocopyLog.log"

If Right(strHelpFiles, 1) = "\" Then strHelpFiles = Left(strHelpFiles, Len(strHelpFiles) - 1)
If Right(strDestination, 1) <> "\" Then strDestination = strDestination & "\"
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colDisks = objWMIService.ExecQuery("Select FreeSpace from Win32_LogicalDisk Where DeviceID='C:'")
For Each objDisk In colDisks
      ' Get free space in MB
      intFreeSpace = Round(objDisk.FreeSpace / (1024 * 1024), 3)
Next
If intFreeSpace < intThreshold Then
      MsgBox "You have less than " & intThreshold & " MB of free space on C Drive. Please contact MIS"
Else
     strCommand = "cmd /c " & objFSO.GetFile(strRoboCopy).ShortPath & " """ & strHelpFiles & """ """ & strDestination & " /MIR /Z /W:5 /LOG:" & strLogFile
     strCommand = InputBox("About to run", "About to run", strCommand)
     objShell.Run strCommand, 0, True
End If 

Open in new window

0
 

Author Comment

by:hartwellcorp
Comment Utility
I tried using your input box:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\adanl.MERCURY>\\hcterm\pub\COPYAV~1\RoboCopy.exe "\\hcnas\avantehelp\Av
ante94" "C:\avantehelp\ /MIR /Z /W:5 /LOG:f:\Temp\RobocopyLog.log
\\hcnas\avantehelp\Avante94\C:\avantehelp\ \MIR \Z \W:5 \LOG:f:\Temp\RobocopyLog
.log\*.*
            *.* /COPY:DAT /R:1000000 /W:30 C:\avantehelp\ \MIR \Z \W:5 \LOG:f:\T
emp\RobocopyLog.log\The filename, directory name, or volume label syntax is inco
rrect.

C:\Users\adanl.MERCURY>
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Whoa?  That's repeated the command twice?  Can you try the latest code and see what you get?

Rob.
0
 

Author Comment

by:hartwellcorp
Comment Utility
Here is the text that i copied out of the "about to run" box:


C:\Users\adanl.MERCURY>cmd /c \\hcterm\pub\COPYAV~1\RoboCopy.exe "\\hcnas\avante
help\Avante94" "C:\avantehelp\ /MIR /Z /W:5 /LOG:F:\RobocopyLog.log

After I run that, the cmd windows has this:


C:\Users\adanl.MERCURY>cmd /c \\hcterm\pub\COPYAV~1\RoboCopy.exe "\\hcnas\avante
help\Avante94" "C:\avantehelp\ /MIR /Z /W:5 /LOG:F:\RobocopyLog.log
\\hcnas\avantehelp\Avante94\C:\avantehelp\ \MIR \Z \W:5 \LOG:F:\RobocopyLog.log\
*.*
            *.* /COPY:DAT /R:1000000 /W:30 C:\avantehelp\ \MIR \Z \W:5 \LOG:F:\R
obocopyLog.log\The filename, directory name, or volume label syntax is incorrect
.

C:\Users\adanl.MERCURY>
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
There seems to be a quote missing.  Change this:
     strCommand = "cmd /c " & objFSO.GetFile(strRoboCopy).ShortPath & " """ & strHelpFiles & """ """ & strDestination & " /MIR /Z /W:5 /LOG:" & strLogFile
 
to this
     strCommand = "cmd /c " & objFSO.GetFile(strRoboCopy).ShortPath & " """ & strHelpFiles & """ """ & strDestination & """ /MIR /Z /W:5 /LOG:""" & strLogFile & """"

and see what you get.

Rob.
0
 

Author Comment

by:hartwellcorp
Comment Utility
no dice.


C:\Users\adanl.MERCURY>cmd /c \\hcterm\pub\COPYAV~1\RoboCopy.exe "\\hcnas\avante
help\Avante94" "C:\avantehelp\" /MIR /Z /W:5 /LOG:"F:\RobocopyLog.log"
\\hcnas\avantehelp\Avante94\C:\avantehelp" \MIR \Z \W:5 \LOG:F:\RobocopyLog.log\
*.*
            *.* /COPY:DAT /R:1000000 /W:30 C:\avantehelp" \MIR \Z \W:5 \LOG:F:\R
obocopyLog.log\The filename, directory name, or volume label syntax is incorrect
.

C:\Users\adanl.MERCURY>
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 500 total points
Comment Utility
OK, I finally got around to testing it....it turns out that RoboCopy doesn't like having slashes on the end of the file paths, so I've changed the script to remove them if they have any.

Regards,

Rob.

' Specify threshold in MB
intThreshold = 200
strHelpFiles = "\\hcnas\avantehelp\Avante94"
strDestination = "C:\avantehelp\"
strRoboCopy = "\\hcterm\pub\copyavantehelp\RoboCopy.exe"
strLogFile = "F:\RobocopyLog.log"

If Right(strHelpFiles, 1) = "\" Then strHelpFiles = Left(strHelpFiles, Len(strHelpFiles) - 1)
If Right(strDestination, 1) = "\" Then strDestination = Left(strDestination, Len(strDestination) - 1)
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colDisks = objWMIService.ExecQuery("Select FreeSpace from Win32_LogicalDisk Where DeviceID='C:'")
For Each objDisk In colDisks
      ' Get free space in MB
      intFreeSpace = Round(objDisk.FreeSpace / (1024 * 1024), 3)
Next
If intFreeSpace < intThreshold Then
      MsgBox "You have less than " & intThreshold & " MB of free space on C Drive. Please contact MIS"
Else
     strCommand = "cmd /c " & objFSO.GetFile(strRoboCopy).ShortPath & " """ & strHelpFiles & """ """ & strDestination & """ /MIR /Z /W:5 /LOG:" & strLogFile
     strCommand = InputBox("About to run", "About to run", strCommand)
     objShell.Run strCommand, 0, True
End If 

Open in new window

0
 

Author Comment

by:hartwellcorp
Comment Utility
oh, didnt realize this had been updated. oops. i'll test.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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

10 Experts available now in Live!

Get 1:1 Help Now