List all files which are recursively found in dir tree?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

List all files which are recursively found in dir tree?

#1 Post by pstein » 14 Nov 2015 05:50

In a batch file I want to execute some commands for all files of a dir tree which match the pattern *.mkv or *.mp4

The following seems not to work:

set topnode=D:\videoarchive
for /r %a in (dir "%%topnode%%\*.mkv" dir "%%topnode%%\*.mp4") do @echo %%a

How else can I setup the command?

Keep in mind that only files should be fetched. Possible folders which may match e.g. *.mkv as well should NOT be considered.
The path of the files should be passed (prepended) as well.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: List all files which are recursively found in dir tree?

#2 Post by ShadowThief » 14 Nov 2015 06:13

Personally, I'm a sucker for using the /F and /L flags exclusively, but the vast majority of people tend to disagree with me about that.

Code: Select all

set topnode=D:\videoarchive
pushd %topnode%
for /F %%A in ('dir /a-d /s *.mkv *.mp4') do echo %%A
popd

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: List all files which are recursively found in dir tree?

#3 Post by Aacini » 14 Nov 2015 06:20

Code: Select all

for /R "%topnode%" %%a in (*.mkv *.mp4) do echo %%a

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: List all files which are recursively found in dir tree?

#4 Post by pstein » 14 Nov 2015 08:17

It works
Thank you

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: List all files which are recursively found in dir tree?

#5 Post by pstein » 14 Nov 2015 10:28

Sorry, one more related question:

The full task which is performed with each file is rather long.
It lasts approx 5-10 minutes per video file and there are some hundred files.

So after a while I would like to (hard) stop the batch and resume it later (in a couple of days).

Whats the best way to do this?

Assume the last file that was executed at interruption time is

D:\videoarchive\myvideo123.mp4

How can I (when re-starting the batch script again) skip (alphabetically) all previous files and resume with the file above?

Thank you

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: List all files which are recursively found in dir tree?

#6 Post by foxidrive » 14 Nov 2015 13:36

pstein wrote:So after a while I would like to (hard) stop the batch and resume it later (in a couple of days).


It would be straight forward but stopping an encoding process will usually leave a half finished file.
You can't just check for the existence of the new filetypes because of that.

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: List all files which are recursively found in dir tree?

#7 Post by pstein » 14 Nov 2015 22:38

Hmm, I thought about a code like:

Code: Select all

for /R "%basedir%" %%a  in (......) do (
   if %%a is alphabetically greater equal to the file in currentfile.txt OR currentfile.txt does not exist (
      echo %%a >currentfile.txt
      executeprgm.exe ......%%a ........)
  )

This way I could interrupt long processing with Ctrl-C for file number 433. Then later restart batch script, skip the first 432 files and start
with processing of file 433.
Ok the possibly already done work for file 433 is lost. But at least 432 files are skipped.

How do I code this pseudo-code above into real DOS batch code?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: List all files which are recursively found in dir tree?

#8 Post by penpen » 15 Nov 2015 09:26

You should not kill the task, to avoid unwanted side effects, such as:
- partially processed files
- zombie processes
- ...

If you can wait until the actual file processing is done, then this "interruptible.bat" might help you:

Code: Select all

@echo off
:: Author: Ulf Schneider (aka penpen: www.dostips.com)
:: Topic : "List all files which are recursively found in dir tree?"
:: Source: "http://www.dostips.com/forum/viewtopic.php?p=43972#p43972"
setlocal enableExtensions disableDelayedExpansion
set "terminate=terminate.txt"
set "running=running.txt"

call :/%~1 || call ://help

endlocal
goto :eof


://?
:/-?
://help
:/-help
   echo Usage: "%~nx0" {main^|forcemain^|terminate^|-?^|/?^|-help^|/help^}
   goto :eof


:/terminate
   if not exist "%terminate%" >"%terminate%" echo(request
   goto :eof


:/forcemain
   for %%a in ("%running%" "%terminate%") do if exist "%%~a" del "%%~a"
:/main
   set "error="
   if exist "%running%" set "error=true"
   if exist "%terminate%" set "error=true"
   if defined error >&2 (
      echo(Another instance of test.bat is detected.
      echo(If autodetection failed, use the forcemain command
      goto :eof
   )

   set ^"received[terminate]=exist "%terminate%"^"
   >"%running%" echo(true
   call :main
   del "%running%"

   if %received[terminate]% (
      echo(Aborted processing ^(termination signal found^).
      del "%terminate%"
   )
   goto :eof


:: your program to execute
:main
   set "lognode=log"
   if not exist "%lognode%" md "%lognode%"

   set "topnode=."
   for /R "%topnode%" %%a in (*.bat *.mkv *.mp4) do if %received[terminate]% (
      echo(Processing file "%%~nxa": Aborted ^(termination signal found^).
   ) else if exist "%lognode%\%%~nxa.txt" (
      echo(Processing file "%%~nxa": Finished ^(earlier^).
   ) else (
      <nul set /P ^"=Processing file "%%~nxa": ^"

      (
         REM simulated processing:
         >nul timeout 4
      ) && (
         >"%lognode%\%%~nxa.txt" echo(true
         echo(Finished.
      ) || (
         echo( Aborted.
      )
   )
   goto :eof
Note: You shouldn't delete the log files (or the log directory) until the full task is done, because this is way to save the progress.


Just start the program using:

Code: Select all

interruptible.bat main

If you want to terminate this program, execute:

Code: Select all

interruptible.bat terminate


penpen

Edit1: Corrected this flaw: On Ctrl-C the aborted file is handled as finished (creates log file and displays "Finished"). Now the log file is not created and the message on using ctrl-c is "Aborted". The executable must support throwing an error in the appropriate way, so it could be catched using "||" in batch.
Edit2: The subcommand "main" now ends, when detecting another instance, after displaying the error message.
Edit3: Added a missing "do" in a for command.
Last edited by penpen on 16 Nov 2015 08:59, edited 3 times in total.

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: List all files which are recursively found in dir tree?

#9 Post by pstein » 16 Nov 2015 02:30

Thank you for your comment.

However interrupting is NOT a problem.
I need some advice for resuming

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: List all files which are recursively found in dir tree?

#10 Post by penpen » 16 Nov 2015 08:28

Resuming is also implemented in my above code.
If you don't want to use the terminate subcommand
(and use ctrl-c or similar), you only have to restart using:

Code: Select all

interruptible.bat forcemain
(Corrected some flaws in the code above.)

penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: List all files which are recursively found in dir tree?

#11 Post by foxidrive » 16 Nov 2015 17:29

pstein wrote:Ok the possibly already done work for file 433 is lost. But at least 432 files are skipped.


Your encoding script can echo the source-filename to a log file AFTER the encoding has finished.

That same script can have a filter in the loop, to skip the filenames in that log file.

Post Reply