2008-10-30 12 views

Antwort

16

+1 zu EBGreen, außer dass (zumindest auf XP) der "-exclude" -Parameter zu get-childitem nicht zu funktionieren scheint. Der Hilfetext (gci -?) Sagt eigentlich "dieser Parameter funktioniert nicht richtig in diesem Cmdlet"!

So können Sie filtern manuell wie folgt aus:

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

Ich bin auf XP und ich lief das genau so, wie es kein Problem ist. – EBGreen

+0

Ich gebe zu, dass das -exclude nicht wie erwartet funktioniert, wenn Sie es mit dem -recurse-Flag kombinieren. In diesem Fall müssen Sie einen expliziten Startpfad angeben. Lass mich sicher gehen, dass ich auch nicht auf der v2 CTP bin. – EBGreen

+0

Ups ... nun da bist du. Ich bin auf der v2 CTP. Sieht so aus, als ob es repariert wird. :) – EBGreen

3

Betrachten Sie den DOS-Befehl FOR in einer Standard-Shell.

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

Hier ist der Weg, Powershell:

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

Oder es etwas ausführlicher und leichter zu verstehen:

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

EDIT: Es gibt natürlich nichts falsch mit der DOS-Weg entweder. :)

EDIT2: Powershell unterstützt implizite (und explizit für diese Angelegenheit) Zeilenfortsetzung und wie Matt Hamiltons Beitrag zeigt, macht es Dinge leichter zu lesen.

1

fanden diese Informationen hilfreich, während Powershell v4 verwenden.

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