Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - File name prefix?

Reply
 
Old 07-13-2008   #11 (permalink)
Megan


 
 

Re: File name prefix?

that works a treat! thanks very much for your help everyone

"Kirk Munro [MVP]" wrote:
Quote:

> Hi Megan,
>
> Here's a slight variation on Shay's script that adds support for renaming
> hidden files as well, plus it detects if some files already have the prefix
> you want and skips them.
>
> Get-ChildItem C:\Scripts -Force | Where-Object {(-not $_.PSIsContainer) -and
> ($_.name -notmatch "^$($_.directory
> .name)_")} | Rename-Item -NewName {$_.directory.name + '_' +
> $_.name} -Whatif
>
> Be careful with the line wrap. This is a one-liner, so make sure you put it
> on one line when you paste it into PowerShell.
>
> This will allow you to run the script on the same folder over time as you
> add files to it without worrying about it re-prefixing all of your files
> that already have the prefix.
>
> --
> Kirk Munro [MVP]
> Poshoholic
> http://poshoholic.com
>
> "Shay Levy [MVP]" <no@xxxxxx> a ?crit dans le message de groupe de
> discussion : 95d808933532d8cab1731886fa4b@xxxxxx...
Quote:

> > 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
> > e ---- ------------- ------ ----
> > -a--- 7/11/2008 13:33:10 0.0 B
> > -a--- 7/11/2008 13:33:10 0.0 B
> > -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:

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

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
C# Regular expression to find all instance and add a prefix .NET General
Shortcut Prefix Tutorials
How do I tell Microsoft Fax to automatically add a 1 prefix? .NET General
fax loses 9 as a prefix Vista print fax & scan
Binary prefix PowerShell


Vista Forums 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 Ltd

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