View Single Post
Old 07-11-2008   #9 (permalink)
Shay Levy [MVP]


 
 

Re: File name prefix?

Hi Megan,


Here's a sample to rename all files in c:\scripts directory, remove -whatIf
when you are ready to perform the rename:


# three files in c:\scripts

PS > dir c:\scripts

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 7/11/2008 13:33:10 0.0 B file1.txt
-a--- 7/11/2008 13:33:10 0.0 B file2.txt
-a--- 7/11/2008 13:33:10 0.0 B file3.txt


# get all files in c:\scripts, exclude directories, rename each file to 'parentDirectoryName_fileName.ext'

PS> dir c:\scripts | where {!$_.PSIsContainer} | rename-item -newName {$_.directory.name+"_"+$_.name}
-whatIf

What if: Performing operation "Rename File" on Target "Item: C:\scripts\file1.txt
Destination: C:\scripts\scripts_file1.txt".
What if: Performing operation "Rename File" on Target "Item: C:\scripts\file2.txt
Destination: C:\scripts\scripts_file2.txt".
What if: Performing operation "Rename File" on Target "Item: C:\scripts\file3.txt
Destination: C:\scripts\scripts_file3.txt".




---
Shay Levy
Windows PowerShell MVP
blog: http://blogs.microsoft.co.il/blogs/ScriptFanatic



M> Hi,
M>
M> I'm new to powershell and having a similar problem where I'm trying
M> to rename the file with folder name as the prefix.
M>
M> eg. C:\scripts\test.txt
M> becomes
M> C:\scripts\scripts_test.txt
M> I can get it to loop through and rename, but can't find a way to
M> return the folder name to append to the existing name?
M>
M> Any advice is appreciated. Thanks
M>
M> "Jacques" wrote:
M>
Quote:
Quote:

>> Shay
>>
>> That does it! Many thanks.
>>
>> Jacques
>>

My System SpecsSystem Spec