Should I use PowerShell or CMD.exe for this?

What should I use to complete the task below?

A task:

  • crosswise to each subfolder of the folder
  • If the subfolder has a folder name XXX, call certain commands
+2


source to share


5 answers


If you decide to go PowerShell, which 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 (note that this version removes the specified directories from the current one recursively):

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

      



This version shows which ones will be removed without actually removing them. Remove -wh (whatif) so that it actually deletes the folders and tells you which ones were deleted. This is the "production" aspect of PowerShell. You can use -WhatIf (or -wh) on most cmdlets, which are destructive, to see what the command β€œdid” without performing the destructive operation. This comes in very handy if you want to kill processes based on a wildcard, for example:

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 everyone, no to everyone. From a production standpoint, I think PowerShell has a pretty clear edge over some of the other options presented here. I'm just saying. :-)

+7


source


PowerShell if it's in between. CMD.exe is just too painful.



Alternatives are scripting languages ​​such as Perl and Python.

+2


source


I would recommend whatever you like best about this task, especially if you need to do it on a regular basis. Batch files, powershell, VBScript, JScript, Ruby, Perl, Python, C #, whatever. Whatever you can do faster.

Powershell and higher end materials will give you more control, but you can even do it with Windows Script Host with VBScript or JScript. Before Powershell, I would have done a lot of simple tasks like just writing simple VB scripts and executing them with WScript.

It should also be mentioned that if you ever need to use this simple Script on another computer, you can pretty much guarantee that WScript will be there if it runs on Windows XP or higher, whereas Powershell will only be on newer versions. Window. Or using C # ... if you know that. Your machine probably has a C # compiler on it, whether you understand it or not.

A simple example: In my music collection, I needed to set the attributes of all files with image-related extensions and music playlist extensions so that they would be hidden so that Windows Media Player could not automatically add these files to my library. It will be filled with all sorts of album covers in the image library. I just wrote a quick JScript that runs with WScript.exe to traverse the directory structure and set these attributes. I run it periodically when I get new music, and iTunes or WMP automatically adds those images to the catalog.

Links:

+2


source


I've done this in VBScript a million times. It only takes a couple of subtle recursive functions to get "this". And, being VBScript, there is all the "it" you can do.

+1


source


If you need to complete a task in a hurry, switch to a language you know! Otherwise, if you're ready to learn new things and work with Windows boxes on a daily basis, use PowerShell. PowerShell now includes all MS products like Exchange 2007, SQL Server 2008 and ships with Windows 7.

+1


source







All Articles