Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

rename files snippet

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 09-04-2007   #1 (permalink)
Hal Rottenberg
Guest


 

rename files snippet

I've got a group of people I work with who distributes executable files with _'s
appended to the filenames. This little snippet made short work of that!

$path = "\\server\share\folder"
dir $path -Recurse -Include *_ | % {
Rename-Item $_ $_.name.Substring(0,$_.name.length-1)
}

Line 1: Set the path to traverse. It can be local or a network share or any
other Powershell provider (like a Onenote notebook or Sharepoint site).

Line 2: Does a dir (aka Get-ChildItem) of the path, recursing into subfolders,
and only includes those files which end in a "_" character. Pass the results
into a foreach-object loop, abbreviated here by the % symbol.

Line 3: Inside the loop, rename each item (as indicated by the $_ automatic
variable). On each item, look at the Name property, and use the Substring
method on that name. We want the substring beginning at the first character
(first element in any array in Powershell is indicated by 0), and ending at the
Length of the Name minus one.


--

Hal Rottenberg
blog: http://halr9000.com
powershell category:
http://halr9000.com/article/category...ng/powershell/

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch rename of files robm_jnb PowerShell 6 06-13-2008 05:23 AM
can you rename all files in folder at once? Buggs1a Vista General 8 02-11-2008 09:31 AM
rename all files from a directory to a list of files ... show3r@gmail.com PowerShell 4 08-25-2007 10:43 PM
rename files (lots of files for newbie) light_wt PowerShell 3 06-08-2007 12:36 PM
cant delete files or rename files nurselisa Vista security 2 05-03-2007 01:40 PM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51