John Sheehan
John Sheehan

Reputation: 78134

Using PowerShell to add an extension to files

I have a directory of files that I'd like to append file extension to as long as they don't have an existing, specified extension. So add .txt to all file names that don't end in .xyz. PowerShell seems like a good candidate for this, but I don't know anything about it. How would I go about it?

Upvotes: 20

Views: 47280

Answers (4)

Coding101
Coding101

Reputation: 531

Found this helpful while using PowerShell v4.

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

Upvotes: 3

EBGreen
EBGreen

Reputation: 37740

Here is the Powershell way:

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

Or to make it a little more verbose and easier to understand:

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

EDIT: There is of course nothing wrong with the DOS way either. :)

EDIT2: Powershell does support implicit (and explicit for that matter) line continuation and as Matt Hamilton's post shows it does make thing's easier to read.

Upvotes: 36

Matt Hamilton
Matt Hamilton

Reputation: 204219

+1 to EBGreen, except that (at least on XP) the "-exclude" parameter to get-childitem doesn't seem to work. The help text (gci -?) actually says "this parameter does not work properly in this cmdlet"!

So you can filter manually like this:

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

Upvotes: 16

kenny
kenny

Reputation: 22344

Consider the DOS command FOR in a 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

Upvotes: 3

Related Questions