2008-10-30 18 views

Respuesta

16

+1 a EBGreen, excepto que (al menos en XP) el parámetro "-excluir" para obtener-childitem no parece funcionar. El texto de ayuda (gci -?) En realidad dice "¡este parámetro no funciona correctamente en este cmdlet"!

Así puede filtrar de forma manual como esto:

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

Estoy en xp y ejecuté eso exactamente igual sin ningún problema. – EBGreen

+0

Admitiré que el -excluido no funciona como se espera si lo combina con el indicador -recurse. En ese caso, debe dar una ruta de inicio explícita. Déjame asegurarme de que no estoy en el v2 CTP también. – EBGreen

+0

Oops ... bueno, ahí estás. Estoy en el v2 CTP. Parece que se está arreglando. :) – EBGreen

3

Considere el comando DOS FOR en un shell estándar.

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

Aquí es la forma Powershell:

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

O para que sea un poco más detallado y más fácil de entender:

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

EDIT: Hay, por supuesto nada malo con el DOS manera tampoco. :)

EDIT2: Powershell soporta la continuación de línea implícita (y explícita para el caso) y como la publicación de Matt Hamilton muestra que hace que sea más fácil de leer.

1

encontraron útil esta información durante el uso de PowerShell v4.

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