Login script checks free space and then copies files

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!
hartwellcorpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee W, MVPTechnology and Business Process AdvisorCommented:
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
hartwellcorpAuthor Commented:
Standard batch file launched at login through group policy.
0
RobSampsonCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

hartwellcorpAuthor Commented:
This seems to be working, trying it out on a few computers and I'll post shortly.
0
hartwellcorpAuthor Commented:
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
RobSampsonCommented:
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
hartwellcorpAuthor Commented:
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
hartwellcorpAuthor Commented:
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
RobSampsonCommented:
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
hartwellcorpAuthor Commented:
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
hartwellcorpAuthor Commented:
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
RobSampsonCommented:
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
RobSampsonCommented:
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
hartwellcorpAuthor Commented:
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
RobSampsonCommented:
Whoa?  That's repeated the command twice?  Can you try the latest code and see what you get?

Rob.
0
hartwellcorpAuthor Commented:
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
RobSampsonCommented:
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
hartwellcorpAuthor Commented:
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
RobSampsonCommented:
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
hartwellcorpAuthor Commented:
oh, didnt realize this had been updated. oops. i'll test.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Legacy OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.