views:

429

answers:

5

What should I use to accomplish this task?

  • CMD.exe?
  • Powershell powerscript?
  • Windows Script Host with VBScript or JScript?

Task:

  1. transverse into every sub-folder of a folder
  2. if the subfolder has a folder name XXX, invoke certain commands
+2  A: 

Powershell if it is between these two. CMD.exe is just too painful.

Alternatives are scripting languages, like Perl and Python.

Peter Mortensen
in Perl, use File::Find module to traverse recursively or a glob to traverse immediate children of current dir.
DVK
I figure it'd be easy in perl or python, but I don't want the to install anything as pre-req of this simple script. Thanks.
Henry
In 2nd thought, PowerShell needs to be installed before windows can run .ps1 as well.
Henry
Windows 7 and Server 2008 R2 ship with PowerShell (it isn't optional). The download for PowerShell on XP/Vista/Server 2003/2008R1 is pretty small (< 10MB) if you already have .NET 2.0 installed.
Keith Hill
+2  A: 

I'd recommend whatever you're most comfortable with accomplishing this task with, especially if you have to do it on a regular basis. Batch files, powershell, VBScript, JScript, Ruby, Perl, Python, C#, whatever. Whatever you can do it faster in.

Powershell and higher end stuff will give you more control, but you could even do this with the Windows Script Host with VBScript or JScript. Before Powershell, I would handle many simple tasks like this by just writing simple VB scripts and running them with WScript.

I should also mention that if you ever need to use a script this simple on another machine, you can pretty much guarantee that WScript will be on there if it's Windows XP or above, whereas Powershell is only on newer versions of Windows. Or using C#... if you know it. Your machine probably has the C# compiler on it whether you realize it or not.

Simple example: In my music collection, I needed to set the attributes of all files with image related extensions and music playlist extensions to hidden to prevent Windows Media Player from adding these files automatically to my library. It would get populated with all sorts of album art in the pictures library. I just wrote a quick JScript that executes with WScript.exe to traverse the directory structure and set those attributes. I run it periodically, when I get new music and either iTunes or WMP adds those images to the directory automatically.

Links:

snicker
thx, where can I learn how to use JScript to accomplish my task?
Henry
I added some links for you that should be helpful =]
snicker
great, thanks!
Henry
ok, PowerShell won: http://www.microsoft.com/technet/scriptcenter/resources/qanda/jan07/hey0126.mspx compare to http://thepowershellguy.com/blogs/posh/archive/2007/01/27/hey-powershell-guy-how-can-i-get-a-list-of-all-the-subfolders-in-a-folder-and-then-put-that-list-into-an-array.aspx
Henry
+4  A: 

If you decide to go PowerShell, which is what I would recommend, this task is pretty simple:

PS> Get-ChildItem <path> -r | Where {$_.PSIsContainer -and $_ -match '<regex>'} |
    Foreach { <do something with each DirectoryInfo object stored in $_> }

Short version using aliases is (note this version deletes the specified dirs from the current dir recursively):

PS> dir . -r | ?{$_.PSIsContainer -and $_ -match 'Backup'} | Remove-Item -v -wh

This version shows you which dirs it would delete without actually deleting them. Remove the -wh (whatif) to have it actually delete the folders and tell you which ones it deleted. This is the "production" aspect of PowerShell. You can use -WhatIf (or -wh) on most cmdlets that are destructive to see what the command "would" do without actually performing the destructive operation. This is very handy when you want to kill processes based on a wildcard e.g.:

PS> Get-Process *host | Stop-Process -WhatIf

Another option is to use -Confirm and it will ask you for each process and you can tell it yes, no, yes to all, no to all. From the production point of view, I think PowerShell has a pretty clear advantage over some of the other options presented here. I'm just sayin'. :-)

Keith Hill
Wow, this is nice! :)Where do you learn PowerShell? I downloaded the useless *.doc in a .zip and can't learn anything useful from it.
Henry
@Henry, ignore the official tutorial. It is useless. There are much better tutorials out there on the web.
dangph
Henry, check out this free eBook I wrote (it's short) - http://tinyurl.com/efposh. Also check out this full blown free book: http://powershell.com/cs/blogs/ebook/ by Dr. Tobias Weltner. If you ever decide to jump into PowerShell enough to warrant buying a book, check out: http://www.manning.com/payette2/. BTW, I've been following PowerShell since PDC 2003 and have been a PowerShell MVP for the past four years.
Keith Hill
+1  A: 

I've done this in VBScript a million times. It takes only a pair of thin recursed functions to get, "it," done. And, being VBScript, there's a whole of, "it," that can be done.

inked
+1  A: 

If you have to do the job in a rush go with the language you know ! Otherwise if you're willing to learn new stuff and works with Windows boxes on a daily basis go with Powershell. Powershell is now all over MS products like Exchange 2007, Sql Server 2008 and comes mounted with Windows 7