What should I use to accomplish this task?
- CMD.exe?
- Powershell powerscript?
- Windows Script Host with VBScript or JScript?
Task:
- transverse into every sub-folder of a folder
- if the subfolder has a folder name XXX, invoke certain commands
What should I use to accomplish this task?
Task:
- transverse into every sub-folder of a folder
- if the subfolder has a folder name XXX, invoke certain commands
Powershell if it is between these two. CMD.exe is just too painful.
Alternatives are scripting languages, like Perl and Python.
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:
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'. :-)
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.
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