Perl questions - more

More questions for Perl on Windows (again I apologize its on windows... )

  1. How can I check free disk space on a drive in windows using perl command in a script?

  2. How can I check processes running using perl command (as I would normally be able to see in task manager for example)

  3. I need to check to see if particular files exist in a directory within the last 1 hour or last 2 hours... How can I do this in perl?

Again - I dont have a lot of experience in perl so any specific examples you can give me would be much appreciated :confused:

My impression is that most of the requirements you mentioned suggest some plausible use cases for Windows Script Host (WSH), which gives scripting access to a subset of the Windows API via a scripting environment. ActivePerl on Windows has an extension to support WSH if all you want to access are Windows-specific facilities. I do not have much exposure to this kind of scripting so I cannot give you any concrete code here.

I would suggest you look at ActivePerl's interface to WSH at
ActivePerl 5.8 - Online Docs : Windows Script Host

and MSDN's scripting reference Scripting

for further information. A quick browse I see the Scripting.FileSystemObject that supports a "freespace" attribute, a "dateCreated" property for files. I can't find process management in this glance but probably I just overlooked it.

Afraid I cannot figure our the code that I would need to use with wsh - I have no objections to using it (assuming I dont have to install anything else)

Can anyone help?

Examples of code to help me out would be great if possible..

I assume you can use vbscript besides perl.

Const HARD_DISK = 3
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "")
For Each objDisk in colDisks
    Wscript.Echo "DeviceID: "& vbTab &  objDisk.DeviceID       
    Wscript.Echo "Free Disk Space: "& vbTab & objDisk.FreeSpace
Next

usage: save as checkDiskSpace.vbs then on command line, type:

c:\> cscript checkDiskSpace.vbs
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process")
For Each objProc in colProcesses
    Wscript.Echo "Caption: "& vbTab &  objProc.Caption    
    Wscript.Echo "Command Line: "& vbTab & objProc.CommandLine
Next

usage: save as checkProcess.vbs and type

c:\> cscript checkProcess.vbs
Set objFSO = CreateObject("Scripting.FileSystemObject")
myFileToCheck = "c:\test\test.txt"
If objFSO.FileExists(myFileToCheck) Then
	WScript.Echo "file exists"
Else
	WScript.Echo "file does not exist"
End If 

Thanks - this helps..

  1. Instead of echo the output to terminal can I output it to a file? If so how.
    I am sure this is simple but I dont know vbs...

  2. Can I run the cscript itsself using a perl call?
    ie I have a perl script checking a few other things - can I get the perl script to execute the vbs script ?

use the redirection operator ! > or >>

you can use system().? or other methods in perl that can call external commands.
try this: perldoc -q external

Back again - thanks for all your help to date - really getting me through what I need...

I need to know how to check if the modified time of a directory is within the last 1 or 2 hours using either vbs or perl script..

How can I do this?

Found something that might help - I can get the modified time of a directory..

$filename='D:\Documents and Settings\admin1\Desktop\Perltest';

@attrs = stat($filename);
print "Time of last inode change (ctime) = " . localtime($attrs[9]) . "\n";

How do I tell in perl now that the modification time is within last 2 hours?

ie. I want to alert if modification time is not within last 2 hours or 1 hours... :confused::confused: