Batch Script - Extract EVERYTHING after the LAST Back Slash in a File


  1. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #1

    Batch Script - Extract EVERYTHING after the LAST Back Slash in a File


    I am sure that I am overthinking this as far as putting a Script together.

    I have a Script that extracts EVERYTHING from a Folder [ ALL Folders & Sub-Folder Names, ALL Filenames within them including the Suffix and Size ] and creates a .log file. The .log file includes the FULL Path name. So far so good.

    I have a .log file with Entries: 87,434.

    Obviously, with such a big file, there are different amounts of \ [ Back Slashes ] within the Path name. How can I extract just the data AFTER the LAST \ please ?

    Here is a small example . . .

    Code:
    
    G:\300GB_From_Internal_HDD\Kingston - Old Stick\KINGSTON\Tiny Hard Disk Backup\Favorites\Current Project\z1.url [Size: 260 Bytes]
    G:\300GB_From_Internal_HDD\Paul Black - FULL Backup 29-Oct-2018\Documents\Paul Black - Personal\eBay\Bought.docx [Size: 210777 Bytes]
    G:\300GB_From_Internal_HDD\Paul Black - FULL Backup 29-Oct-2018\Documents\Paul Black - Personal\eBay\Tables.docx [Size: 180517 Bytes]
    G:\300GB_From_Internal_HDD\System Image-Backups\System Image-Backup Created 23-May-2017\Favorites\Music - YouTube\Steve Roach & Others\Dream Circle Cobalt Sun & Steve Roach - YouTube.url [Size: 355 Bytes]
    G:\300GB_From_Internal_HDD\[00] Paul Black - FULL Backup 31-May-2019\Documents\Paul Black - Movies\Super Mario Bros\vlcsnap-2016-12-16-12h58m45s697.png [Size: 50385 Bytes]
    G:\300GB_From_Internal_HDD\[00] Paul Black - FULL Backup 31-May-2019\Documents\Paul Black - Personal\Miscellaneous\Master - House Specifications.xlsm [Size: 17555 Bytes]
    G:\300GB_From_Internal_HDD\[00] Paul Black - FULL Backup 31-May-2019\Documents\Paul Black - Personal\Miscellaneous\Master - Shopping List.xlsm [Size: 14567 Bytes]
    G:\300GB_From_Internal_HDD\[00] Paul Black - FULL Backup 31-May-2019\Documents\Paul Black - Personal\Miscellaneous\Videos.txt [Size: 458 Bytes]
    G:\300GB_From_Internal_HDD\[00] Paul Black - FULL Backup 31-May-2019\Favorites\General\List.log file.url [Size: 713 Bytes]
    G:\300GB_From_Internal_HDD\Kingston - 32 GB - Red USB\Photos\Sally\Sally 2.jpg [Size: 76108 Bytes]
    
    
    

    I have tried to do this in OpenOffice, but to NO avail.

    Thanks in advance.
      My Computer


  2. Posts : 3,275
    Win10
       #2

    Paul; are you eg. looking to get just this kind of list ?
    Code:
    z1.url [Size: 260 Bytes]
    Bought.docx [Size: 210777 Bytes]
    Tables.docx [Size: 180517 Bytes]
    Dream Circle Cobalt Sun & Steve Roach - YouTube.url [Size: 355 Bytes]
    vlcsnap-2016-12-16-12h58m45s697.png [Size: 50385 Bytes]
    Master - House Specifications.xlsm [Size: 17555 Bytes]
    Master - Shopping List.xlsm [Size: 14567 Bytes]
    Videos.txt [Size: 458 Bytes]
    List.log file.url [Size: 713 Bytes]
    Sally 2.jpg [Size: 76108 Bytes]
    Then, one way is to use Notepad++ and do a search and replace:
    Search for:
    Code:
    .*\\
    Replace with: Nothing
    Search Mode: Regular Expression.

    (Test on a copy of the original).
      My Computers


  3. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #3

    BRILLIANT @das10. Thanks, I can work with that.
      My Computer


  4. Posts : 3,275
    Win10
       #4

    You're welcome.

    pn: just testing using bat/powershell.
    BAT file to do same search/replace:
    Code:
    <# ::
    setlocal
    powershell -noprofile -ExecutionPolicy ByPass "[ScriptBlock]::Create((${%~f0} | out-string)) | Invoke-Expression" & Exit
    goto :EOF
    #>
    ############################     powershell block    ############################    
    $OurFile = Get-Content -Path 'C:\custom\input.txt' -Raw     # adding -Raw reduces processing time considerably.
    foreach ($line in $OurFile) {
    $line -replace '.*\\','' | Out-File 'C:\custom\output.txt' -Append -Encoding Ascii
    } 
    exit
    Last edited by das10; 26 Aug 2022 at 15:05.
      My Computers


  5. Posts : 456
    Windows 10
       #5

    If you drag & drop a text file like in the example to this batch file it should save what you want in a file called result:

    Code:
    @echo off
    for /f "usebackq delims=" %%a in ("%~1") do echo %%~nxa>>result.txt
      My Computer


  6. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #6

    Thanks @das10,

    das10 said:
    pn: just testing using bat/powershell.
    BAT file to do same search/replace:
    Code:
    <# ::
    setlocal
    powershell -noprofile -ExecutionPolicy ByPass "[ScriptBlock]::Create((${%~f0} | out-string)) | Invoke-Expression" & Exit
    goto :EOF
    #>
    ############################     powershell block    ############################    
    $OurFile = Get-Content -Path 'C:\custom\input.txt' -Raw     # adding -Raw reduces processing time considerably.
    foreach ($line in $OurFile) {
    $line -replace '.*\\','' | Out-File 'C:\custom\output.txt' -Append -Encoding Ascii
    } 
    exit

    WOW, that runs quick and produces the required output.

    Just one question. I have a header that I want to keep, so the data actually starts at Line 6. Is there anyway to include that in the Script please ?

    Thanks.
      My Computer


  7. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #7

    Thanks @ricardobohner,

    ricardobohner said:
    If you drag & drop a text file like in the example to this batch file it should save what you want in a file called result:

    Code:
    @echo off
    for /f "usebackq delims=" %%a in ("%~1") do echo %%~nxa>>result.txt
    That does indeed output the correct results, but it took about 15 minutes to process 87,434 lines.
      My Computer


  8. Posts : 3,275
    Win10
       #8

    Paul:
    We can get specific lines in Get-Content, using commands such as Select First/Skip/TotalCount etc. However, then, the -Raw parameter doesn't seem to work when trying to use the 'Select' lines command.

    This W.I.P. works in principle, although, not being able to use the -Raw parameter with Get-Content slows down the script slightly. (I have been searching for an easy way to get the Raw parameter with Get-Content working in such situations which is easier for me to understand since yesterday.)

    Code:
    <# ::
    setlocal
    powershell -noprofile -ExecutionPolicy ByPass "[ScriptBlock]::Create((${%~f0} | out-string)) | Invoke-Expression" & Exit
    goto :EOF
    #>
    ############################     powershell block   ############################    
    
    $List_IN  = 'C:\custom\input.txt'
    $List_OUT = 'C:\custom\output.txt'
    get-content $List_IN | Select -First 1  | Out-File $List_OUT -Encoding Ascii
    get-content $List_IN | Select -Skip  5  | foreach-object {$_ -replace '.*\\',''} | Out-File $List_OUT -Append -Encoding Ascii
    
    exit
      My Computers


  9. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #9

    Thanks @das10,



    das10 said:
    Code:
    Get-Content $List_IN | Select -First 1  | Out-File $List_OUT -Encoding Ascii
    I changed the above line to . . .

    Code:
    
    Get-Content $List_IN | Select -First 5 | Out-File $List_OUT -Encoding Ascii



    das10 said:
    Code:
    $List_IN  = 'C:\custom\input.txt'
    $List_OUT = 'C:\custom\output.txt'
    I changed the above lines so as to use the 'Master Script' Variables to . . .

    Code:
    
    $List_IN  = $Env:OFN
    $List_OUT = $Env:RED



    I also added the line . . .

    Code:
    
    if (Test-Path $List_OUT) {Remove-Item $List_OUT}

    . . . and it works perfectly.



    The extra time taken for 87,434 lines is ONLY about 5 Seconds longer [ 16 Seconds as oppsed to 11 Seconds ]. I can certainly live with that.

    I have set it up so I just Call the Script from my 'Master Script'.

    Thanks again.

    Last edited by Paul Black; 27 Aug 2022 at 15:19.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 17:47.
Find Us




Windows 10 Forums