2008-10-30 72 views

回答

16

+1給EBGreen,除了(至少在XP上)get-childitem的「-exclude」參數似乎不起作用。幫助文本(gci - ?)實際上表示「此參數在此cmdlet中無法正常工作」!

所以,你可以手動篩選這樣的:

gci 
    | ?{ !$_.PSIsContainer -and !$_.Name.EndsWith(".xyz") } 
    | %{ ren -new ($_.Name + ".txt") } 
+0

我在XP上,我完全按照沒有問題的方式運行。 – EBGreen 2008-10-30 21:51:01

3

考慮標準shell中的DOS命令FOR。

C:\Documents and Settings\Kenny>help for 
Runs a specified command for each file in a set of files. 

FOR %variable IN (set) DO command [command-parameters] 

    %variable Specifies a single letter replaceable parameter. 
    (set)  Specifies a set of one or more files. Wildcards may be used. 
    command Specifies the command to carry out for each file. 
    command-parameters 
      Specifies parameters or switches for the specified command. 

... 

In addition, substitution of FOR variable references has been enhanced. 
You can now use the following optional syntax: 

    %~I   - expands %I removing any surrounding quotes (") 
    %~fI  - expands %I to a fully qualified path name 
    %~dI  - expands %I to a drive letter only 
    %~pI  - expands %I to a path only 
    %~nI  - expands %I to a file name only 
    %~xI  - expands %I to a file extension only 
    %~sI  - expanded path contains short names only 
    %~aI  - expands %I to file attributes of file 
    %~tI  - expands %I to date/time of file 
    %~zI  - expands %I to size of file 
    %~$PATH:I - searches the directories listed in the PATH 
        environment variable and expands %I to the 
        fully qualified name of the first one found. 
        If the environment variable name is not 
        defined or the file is not found by the 
        search, then this modifier expands to the 
        empty string 
18

下面是PowerShell方法:

gci -ex "*.xyz" | ?{!$_.PsIsContainer} | ren -new {$_.name + ".txt"} 

還是做了一點更詳細的,更容易理解:

Get-ChildItem -exclude "*.xyz" 
    | WHere-Object{!$_.PsIsContainer} 
    | Rename-Item -newname {$_.name + ".txt"} 

編輯:有,當然是沒有錯DOS方式。 :)

EDIT2:Powershell確實支持隱式(和明確的)線延續,正如馬特漢密爾頓的帖子顯示它確實讓事情更容易閱讀。

1

同時使用PowerShell的V4發現這很有幫助。

Get-ChildItem -Path "C:\temp" -Filter "*.config" -File | 
    Rename-Item -NewName { $PSItem.Name + ".disabled" } 
相關問題