简体   繁体   English

我应该使用PowerShell还是CMD.exe?

[英]Should I use PowerShell or CMD.exe for this?

What should I use to accomplish the task desribed below? 我应该用什么来完成下面描述的任务?

Task: 任务:

  1. transverse into every sub-folder of a folder 横向进入文件夹的每个子文件夹
  2. if the subfolder has a folder name XXX, invoke certain commands 如果子文件夹具有文件夹名称XXX,则调用某些命令

If you decide to go PowerShell, which is what I would recommend, this task is pretty simple: 如果您决定使用PowerShell,这是我推荐的,这项任务非常简单:

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. 删除-wh(whatif)以使其实际删除文件夹并告诉您删除了哪些文件夹。 This is the "production" aspect of PowerShell. 这是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. 您可以在大多数具有破坏性的cmdlet上使用-WhatIf(或-wh),以便在不实际执行破坏性操作的情况下查看命令“将”执行的操作。 This is very handy when you want to kill processes based on a wildcard eg: 当您想要基于通配符杀死进程时,这非常方便,例如:

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. 另一个选择是使用-Co​​nfirm,它会询问你每个进程,你可以告诉它是,不,是对所有人,对所有人都是。 From the production point of view, I think PowerShell has a pretty clear advantage over some of the other options presented here. 从生产的角度来看,我认为PowerShell比其他一些选项有明显的优势。 I'm just sayin'. 我只是在说'。 :-) :-)

PowerShell if it is between these two. PowerShell如果介于这两者之间。 CMD.exe is just too painful. CMD.exe太痛苦了。

Alternatives are scripting languages, like Perl and Python. 替代品是脚本语言,如Perl和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. 批处理文件,powershell,VBScript,JScript,Ruby,Perl,Python,C#等等。 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. Powershell和更高端的东西将为您提供更多控制,但您甚至可以使用带有VBScript或JScript的Windows脚本宿主来完成此操作。 Before Powershell, I would handle many simple tasks like this by just writing simple VB scripts and running them with WScript. 在Powershell之前,我会通过编写简单的VB脚本并使用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. 我还要提一下,如果你需要在另一台机器上使用这么简单的脚本,你几乎可以保证WScript将在那里,如果它是Windows XP或更高版本,而Powershell只在较新版本的Windows上。 Or using C#... if you know it. 或者使用C#...如果你知道的话。 Your machine probably has the C# compiler on it whether you realize it or not. 无论你是否意识到,你的机器上都可能有C#编译器。

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. 简单示例:在我的音乐收藏中,我需要将具有图像相关扩展名和音乐播放列表扩展名的所有文件的属性设置为隐藏,以防止Windows Media Player自动将这些文件添加到我的库中。 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. 我刚刚编写了一个快速的JScript,它使用WScript.exe执行遍历目录结构并设置这些属性。 I run it periodically, when I get new music and either iTunes or WMP adds those images to the directory automatically. 我定期运行它,当我收到新音乐时,iTunes或WMP会自动将这些图像添加到目录中。

Links: 链接:

I've done this in VBScript a million times. 我已经在VBScript中完成了这一百万次。 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. 而且,作为VBScript,可以完成整个“它”。

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. 否则,如果您愿意每天学习新东西并使用Windows机箱,请使用PowerShell。 PowerShell is now all over MS products like Exchange 2007, SQL Server 2008 and comes mounted with Windows 7. PowerShell现在遍布MS产品,如Exchange 2007,SQL Server 2008,并随Windows 7一起安装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM