Solved

Login script checks free space and then copies files

Posted on 2012-03-29
22
374 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
ID: 37784514
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
ID: 37784542
Standard batch file launched at login through group policy.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 37927266
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

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

Author Comment

by:hartwellcorp
ID: 37953191
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
ID: 37955083
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
ID: 37957931
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
ID: 37958132
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
ID: 37960197
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
ID: 37965733
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
 

Author Comment

by:hartwellcorp
ID: 37968016
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
ID: 37968022
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
ID: 37968043
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
ID: 37968048
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
ID: 37968090
Whoa?  That's repeated the command twice?  Can you try the latest code and see what you get?

Rob.
0
 

Author Comment

by:hartwellcorp
ID: 37971098
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
ID: 37973004
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
ID: 37976049
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
ID: 37978172
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
ID: 38033372
oh, didnt realize this had been updated. oops. i'll test.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

828 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