views:

284

answers:

7

I just got a project where I have to do the following on a Windows OS:

  1. detect how many drives (C: D: E: ..etc) are connected to current system
  2. what the system labels are for each volume
  3. how much storage (both used and free) for each of the drives
  4. what format each drive is (NTFS/FAT32)
  5. how many files are in a given directory in any of those drives
  6. how big each file size is
  7. File processing (each file is about 2GB) where I have to do a lot of C-like fseek(), and binary data parsing, and big to little-endian conversion. Have to write some logic code as well.

I'm an experienced C\C++ programmer, but I thought this would be a perfect time for me to start learning about scripting.

Candidates that I thought of are: ( Python || Ruby ) && PowerShell.

Are those the kinds of things I can accomplish with, say, IronPython+Powershell? Or are there better tools/languages out there?

PS: Is PowerShell meant to replace VBScript? Also, what is VB.net good for anyway now that C#, WPF, and Powershell exist?

+8  A: 

Except for the seventh item on your list this should be fairly trivial using Powershell and WMI, as this is perhaps the natural domain for Powershell. Since you won't need another language for the first six list items it shouldn't really matter what you use for the last one. You probably can use PS (I've never done IO with it, though) or whatever suits you.

As for your second question: VBScript is probably not going to go away in the near future as the Windows Script Host is still a safer bet when writing small scripts for deployment, as it comes preinstalled on every Windows since 98. Powershell is only included in Windows 7 and later. That being said, Powershell is surely targeted at obsoleting WSH and CMD for automation purposes since it offers the same features of the aforementioned ones and much more (like easy .NET and WMI access).

VB.NET on the other hand is one of the primary .NET languages marketed by Microsoft. It has little to no relation to VBScript, is no competitor to Powershell or WPF (heck, those are completely different technologies). You may see some convergence with C# going on as both languages still seem to struggle a little finding their intended market. Still, VB.NET is the easiest choice of switching to .NET when you're a VB programmer and there were/are lots of them MS didn't want to lose just because they created .NET.

Joey
+4  A: 

You can do that in either IronPython|IronRuby or Powershell. I'd recommend IronPython since it's a real language (older than C# or even Java), with lots of support and full access to the .NET APIs you will need (or even COM APIs).

I'd suggest you to read this article that answers your exact question: http://pythonconquerstheuniverse.blogspot.com/2009/04/ironpython-windows-scripting-language.html

Alcides
Python is a language older than C# or Java, but IronPython is certainly not. Even if it's older it doesn't automatically make it better (I'm not saying it makes it worse either). Also, not sure what you mean by a "real" language.
Rohit
Yea, PowerShell is real as well. It certainly isn't fictional. :)
JasonMArcher
+6  A: 

PowerShell can easily handle 1-6. Number 7 can probably be done in PowerShell, but there may be better options depending on the details.

See What tutorial do you recommend for learning PowerShell? for some good info on learning PowerShell.

aphoria
A: 

I'll give you the unpopular answer then since no one else has added it: Perl.

If you're comfortable with the Win32 API as a C/C++ programmer, Perl may be the easier way to go. It has modules for accessing the Win32 API and Perl is quite easy for C/C++ programmers to get up to speed in. Perl has always done the job for me in the past.

Dr. Watson
A: 

As for Perl, Ruby too has access to all Win32 API and WMI functions.

MickTaiwan
+1  A: 

Powershell was designed as an automation platform for Windows. So sure, Ruby, Python, Perl, VBScript etc can do the job, why not choose the tool best for the job. Here's how to get some basic drive info:

$drives = Get-WmiObject -Class  Win32_LogicalDisk | ? {$_.DriveType -eq 3 }
$drives | select DeviceId,VolumeName,FileSystem,FreeSpace, Size

Number of files in a directory

Get-ChildItem c:\path -Recurse | Measure-Object
Get-ChildItem c:\path | Measure-Object

With info on file size

Get-ChildItem c:\path -Recurse |  Measure-Object -Average -Sum 
                                 -Maximum -Property Length

And for file processing, you can use native cmdlets such as get-content or invoke and instantiate .Net types.

Scott Weinstein
+4  A: 

I think PowerShell is ideal for this task because it is optimized for this sort of system information retrieval. WMI is exceptionally easy to use from PowerShell as opposed to C# IMO (and I'm a C# dev by day). Heck, let's see how easy this is from PowerShell:

#1,2, 3 and 4
PS> Get-WmiObject Win32_LogicalDisk | 
>>  Format-Table Name, VolumeName, FileSystem, Size, FreeSpace -auto

Name VolumeName FileSystem         Size    FreeSpace
---- ---------- ----------         ----    ---------
C:              NTFS       160038907904 100353536000
D:
E:
F:   PDC2008    NTFS       160039239680  40155922432
V:   Vista      NTFS       250056704000  33944559616

#5
PS> dir <path> -r | where {!$_.PSIsContainer} | Measure-Object

#6
PS> dir <path> -r | where {!$_.PSIsContainer} | 
>>  Format-Table Fullname, Length -auto

And PowerShell can handle #7 easily via its ability to use the .NET Framework. For an example of fseek style programming in PowerShell check out this tail-content script starting around line 139.

Keith Hill