locked
Powershell list files and remove everything after "\" character RRS feed

  • Question

  • ** Final Script at the bottom of this post, enjoy, and big thanks to Mike for the help**

    I'm writing an interactive script that:

    a. Searches for all .vmx files

    b. Displays all found items in a list 

    c. Prompts for which VM I want to backup and then holds that path/filename in a variable to feed to ovatool (no where near this good yet...obviously)

    This is what I have so far. 

    # 	Variables
    $path = "C:\Users\Me\Documents\VMs\"
    
    
    #	Alias
    $ovat = "C:\Program Files (x86)\VMware\VMware Player\OVFTool\ovftool.exe"
    
    #	Begin script
    Get-ChildItem $path -Recurse |Where-Object {$_.Extension -eq '.vmx' -and $_.extension -ne '.vmxf'} |Select-Object -ExpandProperty FullName

    And it correctly displays the following:

    PS C:\WINDOWS\system32> C:\Users\Me\Dropbox\Scripts\Windows\ps\vm\gatherList_0.ps1 C:\Users\Me\Documents\VMs\FreeBSD_s_x64_10.3\FreeBSD_s_x64_10.3.vmx C:\Users\Me\Documents\VMs\Ubuntu_d_x64_16.04\Ubuntu_d_x64_16.04.vmx C:\Users\Me\Documents\VMs\Ubuntu_s_x64_16.04\Ubuntu_s_x64_16.04.vmx C:\Users\Me\Documents\VMs\UbuntuG_d_x64_16.04\UbuntuG_d_x64_16.04.vmx C:\Users\Me\Documents\VMs\Windows10_1\Windows10_1.vmx

    Please could someone show me the syntax or an example (I'm a visual learner, not a reader) as to how I might remove (cut?) everything before the last "\" including that slash. My next step is to get the found file names into a numbered list. I'm trying to move to Powershell and no longer use batch files. The only way I learn and retain what I'm doing is to actually piece things together for small projects. Never been very good at reading manuals or syntax references. Very exciting to get into Powershell and use Cmdlets!! Thank you in advance for any help you offer.

    ------------------------------------------------------------------------------------------------------------------------

                   Completed script below. Change the "$path" variable to the directory containing your VM's.

    ------------------------------------------------------------------------------------------------------------------------

    #	ScriptName	:   vmArchive.ps1
    #	Author		:   me
    #	Date		:   20160922
    #	Purpose		:   Choose from a list of Virtual Machines to backup on C:\.
    #	Reference	:   https://social.technet.microsoft.com/Forums/windows/en-US/9979e1cd-5d13-48ca-aeb1-d98984cd731d/powershell-list-files-and-remove-everything-after-quotquot-character?forum=winserverpowershell
    
    #   Variable
    $path = 'C:\Users\Speedy\Documents\VMs'
    $files = @()
    
    #   Begin script
    cls
    Write-Host ''
    Write-Host ''
    Write-Host 'Enter the number of a VM you want to backup.'
    Write-Host ''
    Write-Host 'It will be located at C:\'
    Write-Host ''
    Write-Host ''
    
    $files += Get-ChildItem $path -Recurse -Include *.vmx
    
    for ($i = 0 ; $i -lt $files.Count ; $i++) {
    
        Write-Host "$($i+1) " -ForegroundColor Yellow -NoNewline
        Write-Host "$($files[$i].FullName)" -ForegroundColor Green
    
    }
    
    Write-Host ''
    Write-Host ''
    
    $choice = Read-Host 'VM choice number?'
    
    $selectedFile = $files[$choice-1]
    
    & ovf $selectedFile.FullName $("C:\" + $selectedFile.Name + ".ova")


    • Edited by smithna Friday, September 23, 2016 12:19 AM
    Wednesday, September 21, 2016 8:41 PM

Answers

  • Hi,

    You're looking for Split-Path:

    http://ss64.com/ps/split-path.html

    $files = @(
        'C:\Users\Me\Documents\VMs\FreeBSD_s_x64_10.3\FreeBSD_s_x64_10.3.vmx',
        'C:\Users\Me\Documents\VMs\Ubuntu_d_x64_16.04\Ubuntu_d_x64_16.04.vmx',
        'C:\Users\Me\Documents\VMs\Ubuntu_s_x64_16.04\Ubuntu_s_x64_16.04.vmx',
        'C:\Users\Me\Documents\VMs\UbuntuG_d_x64_16.04\UbuntuG_d_x64_16.04.vmx',
        'C:\Users\Me\Documents\VMs\Windows10_1\Windows10_1.vmx'
    )
    
    $files | ForEach-Object {
    
        Split-Path -Path $_ -Leaf
    
    }



    EDIT: Now that I go back and read this again, you can just change the property you're expanding if you only want the filename instead of the full path.

    Get-ChildItem $path -Recurse -Include *.vmx,*.vmxf | 
        Select-Object -ExpandProperty Name


    • Proposed as answer by jrv Wednesday, September 21, 2016 9:37 PM
    • Edited by Mike Laughlin Wednesday, September 21, 2016 10:53 PM
    • Marked as answer by smithna Wednesday, September 21, 2016 11:28 PM
    Wednesday, September 21, 2016 8:48 PM

All replies

  • Hi,

    You're looking for Split-Path:

    http://ss64.com/ps/split-path.html

    $files = @(
        'C:\Users\Me\Documents\VMs\FreeBSD_s_x64_10.3\FreeBSD_s_x64_10.3.vmx',
        'C:\Users\Me\Documents\VMs\Ubuntu_d_x64_16.04\Ubuntu_d_x64_16.04.vmx',
        'C:\Users\Me\Documents\VMs\Ubuntu_s_x64_16.04\Ubuntu_s_x64_16.04.vmx',
        'C:\Users\Me\Documents\VMs\UbuntuG_d_x64_16.04\UbuntuG_d_x64_16.04.vmx',
        'C:\Users\Me\Documents\VMs\Windows10_1\Windows10_1.vmx'
    )
    
    $files | ForEach-Object {
    
        Split-Path -Path $_ -Leaf
    
    }



    EDIT: Now that I go back and read this again, you can just change the property you're expanding if you only want the filename instead of the full path.

    Get-ChildItem $path -Recurse -Include *.vmx,*.vmxf | 
        Select-Object -ExpandProperty Name


    • Proposed as answer by jrv Wednesday, September 21, 2016 9:37 PM
    • Edited by Mike Laughlin Wednesday, September 21, 2016 10:53 PM
    • Marked as answer by smithna Wednesday, September 21, 2016 11:28 PM
    Wednesday, September 21, 2016 8:48 PM
  • First off Mike I knew we would be e-friends when your dancing bears linked to the GD's Archive.org page. I'm currently streaming Winterland '73 from my media server.

    Moving on..

    So with what you gave me and what I've found since, I have put together the following:

    # Variables $path = "C:\Users\Me\Documents\VMs\" $longList = Get-ChildItem $path -Recurse |Where-Object {$_.Extension -eq '.vmx' -and $_.extension -ne '.vmxf'} |Select-Object -ExpandProperty FullName $backupDest = "C:\"

    # Alias $ovat = "C:\Program Files (x86)\VMware\VMware Player\OVFTool\ovftool.exe" # Begin script $menu = @{} for ($i=1;$i -le $longList.count; $i++) { Write-Host "$i. $($longList[$i-1])" $menu.Add($i,($longList[$i-1])) } [int]$ans = Read-Host 'Enter selection' $selection = $menu.Item($ans) $selection | % { Write-Host $_.FullName }

    I decided that since I will eventually have to hand off the full path to ovftool I might as well stick with displaying the full path until I gain enough knowledge to come back to this project and make it better by showing the file name yet being able to reference the full path later in the script.

    Would you be so kind as to help me return the full path so I can send it along to play with the ovftool command? The last three lines is where I'm getting stuck and no matter how I word it I'm just not getting anywhere with internet searches or the cmdlet help.

    --------------------------------------------------------------------------------------------------------------

    EDIT 1 - Did some rework of the script and came up with this error as it can't figure out how to get past the "x86" in the directory path.

    PS C:\WINDOWS\system32> C:\Users\Me\Dropbox\Scripts\Windows\ps\vm\gatherList_1.ps1
    1. C:\Users\Me\Documents\VMs\FreeBSD\FreeBSD.vmx
    2. C:\Users\Me\Documents\VMs\Ubuntu 64-bit\Ubuntu 64-bit.vmx
    3. C:\Users\Me\Documents\VMs\Ubuntu16-desktop\Ubuntu16-desktop.vmx
    4. C:\Users\Me\Documents\VMs\UbuntuGnome\UbuntuGnome.vmx
    5. C:\Users\Me\Documents\VMs\Windows10\Windows10.vmx
    Enter selection: 1
    C:\Users\Me\Documents\VMs\FreeBSD\FreeBSD.vmx

    x86 : The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the 
    name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:19
    + C:\Program Files (x86)\VMware\VMware Player\OVFTool\ovftool.exe '' C: ...
    +                   ~~~
        + CategoryInfo          : ObjectNotFound: (x86:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    I'm currently looking in how the directory path is getting jacked up beyond repair but if you or anyone that comes across this could shed some light..I'll take it and move on. I don't want to hi-jack my own thread but I would like to edit my first post with the final script so others that may find it can use it or see how all the suggestions/help has been implemented. 


    • Edited by smithna Wednesday, September 21, 2016 11:53 PM
    Wednesday, September 21, 2016 11:36 PM
  • Here's something you can play with:

    $path = 'C:\Users\Me\Documents\VMs'
    
    $files = @()
    
    $files += Get-ChildItem $path -Recurse -Include *.vmx,*.vmxf
    
    for ($i = 0 ; $i -lt $files.Count ; $i++) {
    
        Write-Host "$($i+1). " -ForegroundColor Yellow -NoNewline
        Write-Host "$($files[$i].FullName)" -ForegroundColor Green
    
    }
    
    $choice = Read-Host 'Which file?'
    
    $selectedFile = $files[$choice-1]
    
    $selectedFile.FullName

    As for Winterland '73, always a good choice.


    Thursday, September 22, 2016 6:41 PM
  • GENIUS!!!!! Thank you so much Mike. Editing bottom of initial post with final script.
    Friday, September 23, 2016 12:15 AM
  • Cheers, you're very welcome. Glad you got it working.

    Friday, September 23, 2016 1:27 AM