Search code examples
windowsbatch-filefor-loopfindstr

Batch Script check a file for a string in a IF DO loop


new here and hoping this is an easy question. I have very little background in programming and scripting but I've got the basics working so far and I'm stuck.

The logic of what I'm trying to accomplish with the parts marked I need help with;

  1. scan my IP cam folder for the largest file
  2. need help check that file against a list of already processed files
  3. move that file to a temp Working dir on a PC with a good CPU and SSD
  4. run HandBrake on it to compress it
  5. move the finished file back to its original location
  6. delete the original from the temp working directory
  7. write its file name to Processed.txt
  8. need help Loop the whole thing to run endlessly

Right now it's working perfectly to scan and find the largest file and send it for processing and move it back. What I want to add is a check of the Processed.txt file during the size scan as very often the largest files still end up to be the largest even after compression (so it gets stuck on the same one). Basically I want to be able to endlessly scan for the largest unprocessed file and compress them one by one. (I have THOUSANDS so typical batch processing wont really work)

I was trying to work out how to get the findstr into the for\do loop but I'm totally lost on that. My thinking is to take the current %name% variable, run it though findstr on the Processed.txt file and if it matches move on, if not make that the current largest file.

here's what I have working so far that I've managed to piece together from other posts;

SETLOCAL EnableDelayedExpansion
set tes=0
set name=
set path=

for /r D:\Cameras %%h in (*.*) do (
IF !tes! LSS %%~zh (
SET tes=%%~zh
SET name=%%~nh
SET path=%%~ph
SET ext=%%~xh
)
)

move "d:%path%%name%%ext%" "d:\Working\"

HandBrakeCLI -i "d:\Working\%name%%ext%" -o "c:\Completed\%name%.mp4" -O -e x264 -q 23 -B 128 -w 720 -l 480 --auto-anamorphic

move "c:\Completed\%name%.mp4" "d:%path%%name%.mp4"
del "d:\Working\%name%%ext%"
echo !name! >> Processed.txt

Thanks in advance, and any and all help is greatly appreciated.

*****EDIT UPDATE***** So i couldn't get the below to work but it took me a new direction. Right now the below seems to be pretty close to working but the second FOR DO IF seems to process outside of the main loop. Maybe someone could help fix that because that is the logic that i think will work for me.

i rem out the stuff i know is working just to test if itll scan, enter the file name, then skip it next time around for testing.

rem :start
SETLOCAL EnableDelayedExpansion
set tes=0
set name=
set path=
set found=false

for /r d:\Cameras %%h in (*.*) do ( 
rem CALL :checkprocessed %%h
for /f %%x in (d:\Processed.txt) do (
IF "%%~nh" NEQ "%%~x" IF !tes! LSS %%~zh (
echo !tes! %%~zh %found% >> ifcheck.txt 
SET tes=%%~zh
SET name=%%~nh
SET path=%%~ph
SET ext=%%~xh
)
)
)

rem move "d:%path%%name%%ext%" "d:\Working\"
rem HandBrakeCLI -i "d:\Working\%name%%ext%" -o "c:\Completed\%name%.mp4" -O -e x264 -q 23 -B 128 -w 720 -l 480 --auto-anamorphic
rem move "c:\Completed\%name%.mp4" "d:%path%%name%.mp4"
rem del "d:\Working\%name%%ext%"
echo !name! >> Processed.txt
rem goto :start


rem :checkprocessed
rem SET found=false
rem for /f %%x in (d:\Processed.txt) do IF "%%~nh" EQU "%%~x" (SET found=true & echo !found! >> found.txt & exit /b 0)
rem exit /b 0

Solution

  • First I want to thank @jwdonahue for all the help and time spent on this. Going back and forth with ideas, I was able to get a script to work perfectly! Below is the working script if anyone needs it. I fixed the path as well from another users comment. Thanks everyone for all the help!

    :start
    SETLOCAL EnableDelayedExpansion
    set tes=0
    set name=
    set _path=
    set ext=
    set _found=false
    
    for /r d:\Cameras %%h in (*.*) do (
    for /f %%x in (d:\Processed.txt) do ( IF "%%~nh" EQU "%%~x" ( set _found=true) )
    IF !_found! EQU false IF !tes! LSS %%~zh (
    SET tes=%%~zh
    SET name=%%~nh
    SET _path=%%~ph
    SET ext=%%~xh
    )
    set _found=false
    )
    
    
    move "d:%_path%%name%%ext%" "d:\Working\"
    HandBrakeCLI -i "d:\Working\%name%%ext%" -o "c:\Completed\%name%.mp4" -O -e x264 -q 23 -B 128 -w 720 -l 480 --auto-anamorphic
    move "c:\Completed\%name%.mp4" "d:%_path%%name%.mp4"
    echo !name! >> Processed.txt
    echo !_path!,!name!!ext!,!tes! >> OrgSize.log
    del "d:\Working\%name%%ext%"
    goto :start