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?
+1
A:
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
kenny
2008-10-30 21:32:28
+7
A:
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.
EBGreen
2008-10-30 21:45:40
+7
A:
+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") }
Matt Hamilton
2008-10-30 21:48:32
I am on xp and I ran that exactly as is with no issue.
EBGreen
2008-10-30 21:51:01
I will admit that the -exclude does not work as expected if you combine it with the -recurse flag. In that case you must give an explicit start path. Let me make sure I'm not on the v2 CTP also.
EBGreen
2008-10-30 21:52:34
Oops...well there you are. I'm on the v2 CTP. Looks like it is getting fixed. :)
EBGreen
2008-10-30 21:53:18
Ah good to know that they've fixed it in v2! I started typing an answer before you posted yours with "-exclude", but tried to test it before I submitted and it didn't work! I had to rework my post! :)
Matt Hamilton
2008-10-30 21:56:24
If you place the rename-item call in the foreach block, it won't receive the current item from the pipeline: should either pass it explicitly (i.e. "| %{ ren $_ -new ($_.Name ...) }") or follow EBGreen's example and use a scriptblock for the new name (i.e. "| ren -new {$_.Name ...}").
Emperor XLII
2008-11-02 13:59:15
Also, if I just copy-and-paste the command, the output of get-childitem won't be piped to the remaining clauses without moving the | to the previous line, or escaping it with a `.
Emperor XLII
2008-11-02 14:01:26