2008-10-30 15 views

Odpowiedz

16

+1 do EBGreen, z tym że (przynajmniej na XP) parametr "-exclude" dla get-childitem nie działa. Tekst pomocy (gci -?) Mówi, że "ten parametr nie działa poprawnie w tym cmdlecie"!

Więc można filtrować ręcznie tak:

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

Jestem na xp i uruchomiłem to dokładnie tak, jak jest bez problemu. – EBGreen

+0

Przyznam, że -exclude nie działa zgodnie z oczekiwaniami, jeśli połączysz go z flagą -recurse. W takim przypadku musisz podać wyraźną ścieżkę początkową. Pozwól mi upewnić się, że nie jestem również na CTP v2. – EBGreen

+0

Ups ... cóż, jesteś. Jestem na CTP v2. Wygląda na to, że jest naprawiony. :) – EBGreen

3

Rozważ polecenie DOS w standardowej powłoce.

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

Oto sposób PowerShell:

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

Albo zrobić to trochę bardziej gadatliwy i łatwiejsze do zrozumienia:

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

EDIT: Nie ma oczywiście nic złego w Sposób DOS albo. :)

EDIT2: Powershell wspiera niejawną (i wyraźną na ten temat) kontynuację linii, a jak pokazuje post Matt Hamiltona, ułatwia czytanie.

1

uznało tę pomocne podczas korzystania PowerShell v4.

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