Results 1 to 7 of 7
  1. #1
    WS Lounge VIP Lugh's Avatar
    Join Date
    Jun 2010
    Location
    Indy
    Posts
    1,604
    Thanks
    363
    Thanked 286 Times in 238 Posts

    Code to delete filename suffixes

    I have a recurring situation in my WIP folder—where I pull in the bits for the current job. It happens due to the requirements of the processes feeding into and being fed by this WIP work. In one step I have 3 files like this:

    banana-ABC.pdf
    banana-ABC-123.txt
    banana-XYZ-123.txt

    I need to end up with:
    banana.pdf
    banana.txt
    banana-XYZ.txt

    So I manually edit the filenames & delete the '-ABC' and '-123' bits. Job done.
    Is there a code snippet which can do this?

    Notes:

    The 'banana' part changes with each job;
    The -ABC and -123 bits do not change, they are literally the same each job;
    The job is always done in the same folder;
    There are other files in the folder at this time;
    -ABC and -123 do not appear in any other files in the folder, they are unique to these 3 files.
    Lugh.
    ~
    Dell Alienware Aurora R6; Win10 Home x64 1803; Office 365 x32
    i7-7700; GeForce GTX 1060; 16GB DDR4 2400; 2 x 256G SSD, 4TB HD

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    10,165
    Thanks
    451
    Thanked 1,656 Times in 1,488 Posts
    Lugh,

    I have a PowerShell script worked up that does the renaming just fine.

    Before:
    BeforeRename.JPG
    After:
    AfterRename.JPG

    However, I haven't tackled the problem of other files in the folder.
    • Do any of the other files have -123 or -ABC in their names?
    • Are any of the other files of type .pdf or .txt?
    • Can you describe another method of determining what files NOT to process?


    HTH
    Last edited by RetiredGeek; 2019-01-02 at 17:09.
    May the Forces of good computing be with you!

    RG
    (still RetiredGeek on AskWoody.com)

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    10,165
    Thanks
    451
    Thanked 1,656 Times in 1,488 Posts
    Lugh,

    I think this may be the solution?
    • It will only process .txt & .pdf files.
    • It will only process files that have file names with greater than 1 segment.
    • It will only process files with file name segment 1 = to ABC for .txt or .pdf files.
    • It will only process files with file name segment 1 = XYZ if name segment 2 = 123 and there are only 3 name segments.


    Before test:
    BeforeRename.JPG

    After Code run:
    AfterRename.JPG

    PowerShell:
    Code:
    #Change as appropriate or convert to Parameter!
    $MyDir = "G:\Test" 
    
    Get-ChildItem -Path "$MyDir" -File |
      ForEach-Object {
        $Parts = $_.Name.Split('.')
        If ($Parts[1] -eq "txt" -or $Parts[1] -eq "pdf") {
          $Segs  = $Parts[0].Split('-')
          If ($Segs.Count -gt 1) {      
            If ($Segs[1] -eq "ABC") {
              $RIArgs = @{Path    =  $_.FullName
                          NewName = "$($Segs[0]).$($Parts[1])"}
              Rename-Item @RIArgs 
            } #End If
            Else {
                If ($Segs.count -eq 3 -and $Segs[2] -eq "123") {
                  $RIArgs = @{Path    =  $_.FullName
                              NewName = "$($Segs[0])-$($Segs[1])." +
                                        "$($Parts[1])"}
                  Rename-Item @RIArgs
                } #End If    
            }     #End Else
          }       #End If ($Segs.Count...
        }         #End If ($Parts[1]...
      }           #End ForEach-Object
    HTH
    May the Forces of good computing be with you!

    RG
    (still RetiredGeek on AskWoody.com)

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  4. The Following User Says Thank You to RetiredGeek For This Useful Post:

    Lugh (2019-01-02)

  5. #4
    WS Lounge VIP Lugh's Avatar
    Join Date
    Jun 2010
    Location
    Indy
    Posts
    1,604
    Thanks
    363
    Thanked 286 Times in 238 Posts
    Thanks for your efforts RG, much appreciated as always

    I'll test later and report back.
    Lugh.
    ~
    Dell Alienware Aurora R6; Win10 Home x64 1803; Office 365 x32
    i7-7700; GeForce GTX 1060; 16GB DDR4 2400; 2 x 256G SSD, 4TB HD

  6. #5
    WS Lounge VIP Lugh's Avatar
    Join Date
    Jun 2010
    Location
    Indy
    Posts
    1,604
    Thanks
    363
    Thanked 286 Times in 238 Posts
    Finally got to test this RG, and nothing happens when I run the script. I tried it first via a desktop shortcut, then via the script file directly—r-click & Run in Powershell.

    The only things I changed were:
    The location of $MyDir;
    The 'txt' & 'pdf' to the actual extensions;
    The ABC & 123 to the actual suffixes.

    The only event after running the script is a windows appears briefly—less than a second—which I assume is the PS window.

    To answer your Qs in post #2:

    Do any of the other files have -123 or -ABC in their names?
    No.

    Are any of the other files of type .pdf or .txt?
    Yes.

    Can you describe another method of determining what files NOT to process?
    All I can think of is the other files won't have 123 or ABC in their names.

    This is only a minor job, so don't spend any/much time on it. I'm pretty sure I can do it with SikuliX without much effort—I just thought there might be a simple way to do it via VB or CMD or PS.
    Lugh.
    ~
    Dell Alienware Aurora R6; Win10 Home x64 1803; Office 365 x32
    i7-7700; GeForce GTX 1060; 16GB DDR4 2400; 2 x 256G SSD, 4TB HD

  7. #6
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    9,214
    Thanks
    70
    Thanked 1,170 Times in 1,087 Posts
    Lugh, open a Powershell window so you can see what is happening. You probably need to allow un-trusted scripts to run.
    https://ss64.com/ps/syntax-run.html

    cheers, Paul

  8. #7
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    10,165
    Thanks
    451
    Thanked 1,656 Times in 1,488 Posts
    Lugh,

    Have you ever set your execution-policy in powershell?

    Open PowerShell as Administrator
    Type: Set-ExecutionPolicy RemoteSigned (enter key)
    Answer Yes to All to the prompt.

    The above is a one time operation.

    HTH
    May the Forces of good computing be with you!

    RG
    (still RetiredGeek on AskWoody.com)

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •