1. You are viewing our forum as a guest. For full access please Register. WindowsBBS.com is completely free, paid for by advertisers and donations.

Resolved Using FOR command to list filenames in order

Discussion in 'Windows 10' started by Ludwig, 2018/09/05.

  1. 2018/09/05
    Ludwig

    Ludwig Well-Known Member Thread Starter

    Joined:
    2002/01/07
    Messages:
    96
    Likes Received:
    0
    Trophy Points:
    206
    Location:
    New Zealand
    Computer Experience:
    Been round # of yrs
    Using a simple batch file I am trying to keep my backup files for a system under control e.g. keep the most recent 5, then delete any more provided they are at least 10 days old.

    To achieve this, I am trying to use the FOR command to list files in a directory in reverse name order (which, by convention is the same as saying reverse order of when they were created).

    I can achieve a list of the matching files using the FOR syntax -
    set srch=pal *.xlsm
    for /R %%a in ("%srch%") do echo file %%~fa ..​
    and, in my case, I get all 18 matching filenames listed. Trouble is, the list of files is from oldest to youngest. I need it to be youngest to oldest. That way I can skip the first 5. With the resultant list I can then check if the file is older than say 10 days and if so delete it.

    Ignoring the wrong order for now, I tried to skip the first 5 files in the list, so I added the SKIP option into the command -
    for /R "SKIP=5" %%a in ("%srch%") do @echo %%~fa ..​
    but now I get absolutely no files listed!!
    What an I coding wrong?

    Ultimately, I thought something like the following would do it, but it obviously is flawed ;-{
    i.e. files are listed in the wrong order, and listing of files not working at all with SKIP coded.
    set myNum=5
    echo list with skip the first %myNum% ...
    for /R "SKIP=%myNum%" %%a in ("%srch%") do (
    echo encountered file %%~fa .. datetime %%~ta.
    forfiles /M "%%a" /D -10 /C "cmd /c echo issue a delete for file %%a ..
    )​

    Thanks for any help anyone has.
     
  2. 2018/09/06
    Bill

    Bill SuperGeek WindowsBBS Team Member

    Joined:
    2002/01/11
    Messages:
    2,847
    Likes Received:
    270
    Trophy Points:
    1,093
    Location:
    Nebraska, USA
    Computer Experience:
    Built. Broke. Fixed.
    I am not a programmer so I cannot answer this. Until a programmer comes along, I have to ask, can't you tell your backup program to keep only the last 5 backups?
     
    Bill,
    #2

  3. to hide this advert.

  4. 2018/09/07
    TonyT

    TonyT SuperGeek Staff

    Joined:
    2002/01/18
    Messages:
    8,796
    Likes Received:
    365
    Trophy Points:
    1,093
    Location:
    Fairfax, VA
    Computer Experience:
    echo $experienced;
  5. 2018/09/07
    Ludwig

    Ludwig Well-Known Member Thread Starter

    Joined:
    2002/01/07
    Messages:
    96
    Likes Received:
    0
    Trophy Points:
    206
    Location:
    New Zealand
    Computer Experience:
    Been round # of yrs
    I figured my code ... combination of FOR and FORFILES will do it. Posted here in case someone else wants to use it, or a variation of it.

    set keepcnt=5
    REM
    REM other code in here
    REM working directory is where the files are so no need for "path" option in FOR
    REM
    FOR /f "skip=%keepcnt% eol=: delims=" %%f in ('dir "* FNpattern *.xlsx" /b /o-n') do (
    echo Checking if file "%%f" is old enough to delete ..
    FORFILES /M "%%f" /D -10 /C "cmd /c echo ..time to delete file @file" 2>nul
    FORFILES /M "%%f" /D -10 /C "cmd /c del @file"2>nul​
    )​

    As with the "echo" immediately before it, the first FORFILES is optional. I'm using it to give me a 'report' on what will actually be done. It does result in two "ERROR.." lines if the files are not 10+ days old, but the "2>nul" on the end eliminates those msgs.
     
    Last edited: 2018/09/07
  6. 2018/09/07
    Ludwig

    Ludwig Well-Known Member Thread Starter

    Joined:
    2002/01/07
    Messages:
    96
    Likes Received:
    0
    Trophy Points:
    206
    Location:
    New Zealand
    Computer Experience:
    Been round # of yrs
    My "backup pgm" is what I write. App is written in Excel (with VBA) so a batch file (in addition to any system level backup "pgm" - which is a drive cloning each night .. don't ask me why that's what they do) does the backups periodically during the day using Task Scheduler in case a user does the accidental "hit the DELETE key" on their keyboard when explore-ing files on the network drive.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.