Windows Vista Forums

File name prefix?
  1. #1


    Jacques Guest

    File name prefix?

    In need to list a group of files, work on each one individually, then rename
    each one with a different suffix when I've processed it. The listing and
    looping parts I've done, but I can't find a way of (easily) just getting the
    first part of a file's name. For example, I need to find and process
    'C:\temp\firstfile.sql', then when I've finished with it, rename it
    'C:\temp\firstfile.processed'. I can get the 'C:\temp' and '.sql' into
    variables, but I really want the 'firstfile' part. Any ideas anybody?

    Thanks in advance



    Jacques



      My System SpecsSystem Spec

  2. #2


    Marco Shaw [MVP] Guest

    Re: File name prefix?

    Jacques wrote:

    > In need to list a group of files, work on each one individually, then rename
    > each one with a different suffix when I've processed it. The listing and
    > looping parts I've done, but I can't find a way of (easily) just getting the
    > first part of a file's name. For example, I need to find and process
    > 'C:\temp\firstfile.sql', then when I've finished with it, rename it
    > 'C:\temp\firstfile.processed'. I can get the 'C:\temp' and '.sql' into
    > variables, but I really want the 'firstfile' part. Any ideas anybody?
    >
    > Thanks in advance
    >
    > Jacques
    >
    >
    PS> get-childitem test.ps1|select-object basename

    BaseName
    --------
    test

    Note: This is PowerShell v2 CTP2.

    Marco

    --
    Microsoft MVP - Windows PowerShell
    http://www.microsoft.com/mvp

    PowerGadgets MVP
    http://www.powergadgets.com/mvp

    Blog:
    http://marcoshaw.blogspot.com

      My System SpecsSystem Spec

  3. #3


    Hal Rottenberg Guest

    Re: File name prefix?

    Jacques wrote:

    > first part of a file's name. For example, I need to find and process
    > 'C:\temp\firstfile.sql', then when I've finished with it, rename it
    > 'C:\temp\firstfile.processed'. I can get the 'C:\temp' and '.sql' into
    > variables, but I really want the 'firstfile' part. Any ideas anybody?
    Try the BaseName property.

    PS > ls | select -first 3 | ft basename

    BaseName
    --------
    2007-07-02@xxxxxx-07-33
    bob_
    cd-images


    --
    Author, Tech Prosaic blog (http://halr9000.com)
    Webmaster, Psi (http://psi-im.org)
    Community Director, PowerShellCommunity.org
    Co-host, PowerScripting Podcast (http://powerscripting.net)

      My System SpecsSystem Spec

  4. #4


    Jacques Guest

    Re: File name prefix?

    Phew! That was quick! Thanks Marco and Hal, that does the job, but only in
    CTP2. Is there no solution for V1.0 other than manually stripping off and
    replacing the suffix? I haven't rolled CTP2 out into my production
    environments.

    Jacques



      My System SpecsSystem Spec

  5. #5


    Jacques Guest

    Re: File name prefix?

    Shay

    That does it! Many thanks.

    Jacques



      My System SpecsSystem Spec

  6. #6


    Shay Levi Guest

    Re: File name prefix?


    Hi Jacques,

    You can rename files with move-item:


    PS 9> new-item -type file -Name firstfile.sql

    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    -a--- 6/5/2008 3:58:56 PM 0.0 B firstfile.sql


    PS 10> move-item firstfile.sql firstfile.processed
    PS 11> dir *.pro*

    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    -a--- 6/5/2008 3:58:56 PM 0.0 B firstfile.processed



    To get the first part of the filename:

    PS 12> [io.path]::GetFileNameWithoutExtension("firstfile.sql")
    firstfile


    In PowerShell CTP(2) you can get the filename using the BaseName member:
    PS 13> (dir .\firstfile.processed).BaseName
    firstfile



    ---
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > In need to list a group of files, work on each one individually, then
    > rename each one with a different suffix when I've processed it. The
    > listing and looping parts I've done, but I can't find a way of
    > (easily) just getting the first part of a file's name. For example, I
    > need to find and process 'C:\temp\firstfile.sql', then when I've
    > finished with it, rename it 'C:\temp\firstfile.processed'. I can get
    > the 'C:\temp' and '.sql' into variables, but I really want the
    > 'firstfile' part. Any ideas anybody?
    >
    > Thanks in advance
    >
    > Jacques
    >


      My System SpecsSystem Spec

  7. #7


    Megan Guest

    Re: File name prefix?

    Hi,

    I'm new to powershell and having a similar problem where I'm trying to
    rename the file with folder name as the prefix.

    eg. C:\scripts\test.txt
    becomes
    C:\scripts\scripts_test.txt

    I can get it to loop through and rename, but can't find a way to return the
    folder name to append to the existing name?

    Any advice is appreciated. Thanks

    "Jacques" wrote:

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

      My System SpecsSystem Spec

  8. #8


    Michael Gao Guest

    Re: File name prefix?

    Hi,
    I think you can get the folder name like this:
    ((gci C:\scripts\test.txt).directory).name

    "scripts " should be return.

    MG

    "Megan" <Megan@xxxxxx> wrote in message
    news:633932BE-E406-4BD9-AFAA-0669DD730F6D@xxxxxx

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

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

      My System SpecsSystem Spec

  9. #9


    Shay Levy [MVP] Guest

    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>

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


      My System SpecsSystem Spec

  10. #10


    Kirk Munro [MVP] Guest

    Re: File name prefix?

    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...

    > 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>

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

      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
File name prefix? problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
batch rename files with same prefix scottchu General Discussion 2 08 Apr 2010
Shortcut Prefix Brink Tutorials 2 19 Aug 2008
How do I tell Microsoft Fax to automatically add a 1 prefix? Dave .NET General 0 12 Aug 2008
fax loses 9 as a prefix xmlmagician Vista print fax & scan 5 22 May 2008
Binary prefix Pavel Ivashkov PowerShell 0 01 May 2007