List all files which are recursively found in dir tree?
Moderator: DosItHelp
List all files which are recursively found in dir tree?
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.
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.
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: List all files which are recursively found in dir tree?
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
Re: List all files which are recursively found in dir tree?
Code: Select all
for /R "%topnode%" %%a in (*.mkv *.mp4) do echo %%a
Re: List all files which are recursively found in dir tree?
It works
Thank you
Thank you
Re: List all files which are recursively found in dir tree?
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
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
Re: List all files which are recursively found in dir tree?
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.
Re: List all files which are recursively found in dir tree?
Hmm, I thought about a code like:
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?
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?
Re: List all files which are recursively found in dir tree?
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:
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:
If you want to terminate this program, execute:
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.
- 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
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.
Re: List all files which are recursively found in dir tree?
Thank you for your comment.
However interrupting is NOT a problem.
I need some advice for resuming
However interrupting is NOT a problem.
I need some advice for resuming
Re: List all files which are recursively found in dir tree?
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:
(Corrected some flaws in the code above.)
penpen
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
penpen
Re: List all files which are recursively found in dir tree?
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.