[WIN10] Get 1st file only in each dir

Discussion in 'Windows Operating Systems' started by spotted, Dec 28, 2020.

  1. spotted

    spotted Member

    Joined:
    Nov 10, 2010
    Messages:
    414
    Location:
    Sydney
    Playing with batch files, and I need a list of files in a tree:
    • in any subfolder
    • matching a type (*.ext)
    • but only 1st file in each dir
    I got up to the last bit
    dir *.ext /s /b > out.txt

    Is there a way to remove multiple files in same dir?
     
  2. evilasdeath

    evilasdeath Member

    Joined:
    Jul 24, 2004
    Messages:
    4,994
    not what you want to know but in bash i would use grep and search for ".." and do post-lines 1 then pipe back to grep and remove the .. line

    ls -al | grep -A1 ".." | grep -v ".."

    it doesn't appear that findstr includes the ability to do post/pre-lines

    there might be powershell commands that do, or a version of grep for cmd?

    on another thought i'd need an appropriate file filter for ls to do the file type.
     
  3. OP
    OP
    spotted

    spotted Member

    Joined:
    Nov 10, 2010
    Messages:
    414
    Location:
    Sydney
    Interesting. There is cygwin, that has grep. But I got this problem
    I need ls -alR to get it to list subdir
    Otherwise I'm only in the top dir

    • I can't search *.ext - ok I can add a grep
    • But folder names are separated from filenames now
    Now if I use dir to get my filelist, it doesn't show . and .. so the rest won't work.
     
  4. philquad

    philquad Member

    Joined:
    Jan 22, 2004
    Messages:
    1,605
    Location:
    nelson bay
    im no script kiddy but maybe look at this https://www.den4b.com/download/renamer/portable & its reame or insert function, as it can pick out the 1st instance of a set of folders \ sub folders
     
  5. NSanity

    NSanity Member

    Joined:
    Mar 11, 2002
    Messages:
    18,304
    Location:
    Canberra
    you're going to have to shift to powershell

    Code:
    (get-childitem .\ | Where {$_.Name -like "**.ext"} | select-object -first 1).name
    then you're going to want to do a for-each loop to gather the names of all the directory's and loop into each of them.
     
  6. OP
    OP
    spotted

    spotted Member

    Joined:
    Nov 10, 2010
    Messages:
    414
    Location:
    Sydney
    Thanks.
     
  7. CAPT-Irrelevant

    CAPT-Irrelevant Member

    Joined:
    Sep 7, 2007
    Messages:
    7,380
    Location:
    Sydney
    More for my own education, what criteria are you looking at for the '1st' file? Any scenario examples?
     
  8. OP
    OP
    spotted

    spotted Member

    Joined:
    Nov 10, 2010
    Messages:
    414
    Location:
    Sydney
    I have a script (not written by eme) that acts on a file AND all other files of the same ext in a folder
    So I needed to run this command only once per folder, esp if a folder had 20 of the same file types, you'd run once vs 20 times.
    So it's just a search for *.ext really
     
  9. OP
    OP
    spotted

    spotted Member

    Joined:
    Nov 10, 2010
    Messages:
    414
    Location:
    Sydney
    In folders where nothing exists, how do I check the return?

    I got

    Code:
    $folders = (Get-ChildItem -Recurse -Directory -Force -ErrorAction SilentlyContinue | Select-Object FullName)
    
    foreach ($folder in $folders) {
        # Get 1 file per folder
        $filename = (Get-ChildItem -Path $folder.FullName -Filter *.ext -Name | Select-Object -first 1)
     
  10. randomman

    randomman Member

    Joined:
    Oct 21, 2007
    Messages:
    4,962
    Location:
    Vancouver, BC
    Code:
     (gci -Filter "*.ext")[0].Name
    RE where nothing exists:
    Code:
    If (!(gci -Filter "*.ext")[0].Name) {Write-Output "Nothing exists!"}
    Else {Write-Output "Do something"}

    Code:
    $folders = (Get-ChildItem -Recurse -Directory -Force -ErrorAction SilentlyContinue | Select-Object FullName)
    This is kinda pointless. If you just want the FullName property you want to use:
    Code:
    $folders = Get-ChildItem -Recurse -Directory -Force -ErrorAction SilentlyContinue | Select-Object -Expand FullName
    or
    Code:
    $folders = (Get-ChildItem -Recurse -Directory -Force -ErrorAction SilentlyContinue).FullName
     
    Last edited: Feb 11, 2021
  11. NSanity

    NSanity Member

    Joined:
    Mar 11, 2002
    Messages:
    18,304
    Location:
    Canberra
    you could just do a try...catch...finally
     

Share This Page

Advertisement: