17

指定された既存の拡張子がない限り、ファイル拡張子を追加したいファイルのディレクトリがあります。したがって、.xyzで終わらないすべてのファイル名に.txtを追加します。PowerShellはこれに適した候補のようですが、私はそれについて何も知りません。どうすればいいですか?

4

4 に答える 4

27

Powershell の方法は次のとおりです。

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

または、もう少し冗長で理解しやすくするには、次のようにします。

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

編集: もちろん、DOS の方法にも問題はありません。:)

EDIT2: Powershell は、暗黙的 (およびそのことについては明示的) の行継続をサポートしており、Matt Hamilton の投稿が示すように、読みやすくなっています。

于 2008-10-30T21:45:40.920 に答える
16

EBGreenに+1します。ただし、(少なくともXPでは)get-childitemの「-exclude」パラメーターが機能していないようです。ヘルプテキスト(gci-?)には、実際には「このパラメーターはこのコマンドレットでは正しく機能しません」と記載されています。

したがって、次のように手動でフィルタリングできます。

gci 
  | ?{ !$_.PSIsContainer -and !$_.Name.EndsWith(".xyz") } 
  | %{ ren -new ($_.Name + ".txt") }
于 2008-10-30T21:48:32.710 に答える
3

標準シェルでの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
于 2008-10-30T21:32:28.280 に答える