[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 460
  • Last Modified:

Batch Script Tweak Mk III

Hi Guys and Gals

Following on from this thread:

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_27626476.html

Im looking to alter my batch script so that it keeps going to the start of the script if there are still files to process. The current version of the script is:

@echo off
setlocal enabledelayedexpansion

set "BaseDir=D:\Videos\DVD Transfer"
set "Pass[1]=-an"
set "Pass[2]=-acodec libmp3lame -ac 2 -ab 128kb -ar 44100"

path %BaseDir%\Encoding Software;%path%

for %%S in (Comedy Documentaries Films "Music Videos" Musicals TV) do (
  echo Processing %%~S folder
  set "Source=%BaseDir%\To Encode\%%~S"
  for %%F in ("!Source!\*.mkv") do (
    echo File: %%~nF
    set Filename=%%~nF

    set "Destination=%BaseDir%\Completed Encodes\%%~S\!Filename!"
    echo TV Documentaries | findstr "%%~S" >nul && (
      if "!Filename:~-7,1!"==" " (
        if "!Filename:~-6,1!"=="S" (
          if "!Filename:~-3,1!"=="E" (
            set "Destination=%BaseDir%\Completed Encodes\%%~S\!Filename:~0,-7!\Season !Filename:~-5,2!"
    ) ) ) )

		if "%%~S" == "Films" (
  		set "Destination=%BaseDir%\Completed Encodes\%%~S\YZ0\!Filename!"
  		for %%L in (ABC DEF GHI JKL MNO PQR STU VWX YZ0) do echo %%L | find /i "!Filename:~0,1!" >null && (
    		set "Destination=%BaseDir%\Completed Encodes\%%~S\%%L\!Filename!"
  		)
		)



    md "!Destination!" 2>nul

    for /l %%P in (1,1,2) do (
      echo Pass: %%P
      ffmpeg.exe -y -i "!Source!\%%~nF.mkv" -pass %%P -passlogfile "%BaseDir%\To Encode\%%~nF.log" -threads 8 -vcodec libxvid -vtag XVID -f avi -aspect 16:9 -r 25 -s 720x576 -b 1250kb -minrate 1250kb -maxrate 1250kb -bufsize 2048k !Pass[%%P]! "!Source!\!Filename!.avi"
    )
		del *.log
    
		move "!Source!\%%~nF.avi" "!Destination!\%%~nF.avi" >nul
    
    del "%BaseDir%\To Encode\%%~S\%%~nF.mkv" >nul
    
    start "" "D:\Program Files\FlashFXP\FlashFXP.exe" -tray -c2 -go FATNAS.fqf
    echo.
  )
)

Open in new window


It now processes the file, then moves it to the right place and deletes the original source file. Once thats done, it ftp's it to my nas. The issue i've got is that im constantly adding files to the TV, Films etc folders whilst this batch script is running so its only picking up the files it found when it originally set off and not the new ones. Is it possible for it to loop back around until it finds nothing to process?

James
0
Delerium1978
Asked:
Delerium1978
  • 5
  • 4
  • 2
1 Solution
 
paultomasiCommented:
Blimey! I remember that code...
0
 
pony10usCommented:
@paultomasi  

So do I   :)

It sounds like the goal now is to make it a continous loop.  It seems that we have spent our whole lives trying to prevent continous loops, also know as runaway programs years ago.  Funny how times change.
0
 
Delerium1978Author Commented:
Well i've made it half way through my girlfriends collection but its taking so long so anything i can do to automate it the better :)

So yeah, continuous loop until there are no files found to process. It possible?
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
paultomasiCommented:
Would this suffice?
@echo off
setlocal enabledelayedexpansion

set "BaseDir=D:\Videos\DVD Transfer"
set "Pass[1]=-an"
set "Pass[2]=-acodec libmp3lame -ac 2 -ab 128kb -ar 44100"

path %BaseDir%\Encoding Software;%path%

:loop
for %%S in (Comedy Documentaries Films "Music Videos" Musicals TV) do (
  echo Processing %%~S folder
  set "Source=%BaseDir%\To Encode\%%~S"
  for %%F in ("!Source!\*.mkv") do (
    echo File: %%~nF
    set Filename=%%~nF

    set "Destination=%BaseDir%\Completed Encodes\%%~S\!Filename!"
    echo TV Documentaries | findstr "%%~S" >nul && (
      if "!Filename:~-7,1!"==" " (
        if "!Filename:~-6,1!"=="S" (
          if "!Filename:~-3,1!"=="E" (
            set "Destination=%BaseDir%\Completed Encodes\%%~S\!Filename:~0,-7!\Season !Filename:~-5,2!"
    ) ) ) )

    if "%%~S" == "Films" (
      set "Destination=%BaseDir%\Completed Encodes\%%~S\YZ0\!Filename!"
      for %%L in (ABC DEF GHI JKL MNO PQR STU VWX YZ0) do echo %%L | find /i "!Filename:~0,1!" >null && (
        set "Destination=%BaseDir%\Completed Encodes\%%~S\%%L\!Filename!"
      )
    )

    md "!Destination!" 2>nul

    for /l %%P in (1,1,2) do (
      echo Pass: %%P
      ffmpeg.exe -y -i "!Source!\%%~nF.mkv" -pass %%P -passlogfile "%BaseDir%\To Encode\%%~nF.log" -threads 8 -vcodec libxvid -vtag XVID -f avi -aspect 16:9 -r 25 -s 720x576 -b 1250kb -minrate 1250kb -maxrate 1250kb -bufsize 2048k !Pass[%%P]! "!Source!\!Filename!.avi"
    )
		del *.log
    
		move "!Source!\%%~nF.avi" "!Destination!\%%~nF.avi" >nul
    
    del "%BaseDir%\To Encode\%%~S\%%~nF.mkv" >nul
    
    start "" "D:\Program Files\FlashFXP\FlashFXP.exe" -tray -c2 -go FATNAS.fqf
    echo.
  )
)

for %%S in (Comedy Documentaries Films "Music Videos" Musicals TV) do (
  if exist "%BaseDir%\To Encode\%%~S\*.mkv" goto loop
)

Open in new window

I have added the label :loop in line 10, and code at lines 50..52.
0
 
pony10usCommented:
EDIT:  paul - you beat me to it.   :)

ORIGINAL:


Okay, I had to review the code to remember what was going on.

Here is what is happening:

1. Look in %basedir%\toencode\comedy and process all files
2. Look in %basedir%\toencode\documentaries and process all files
.
.
.
x. Look in %basedir%\toencode\tv and process all files
x+1. Exit

You want it to loop back to 1. after completing x instead of moving to x+1.

The simple answer is to put a dreaded "goto" logic in the program and just let it run continously period.  This would not be the cleanest method but would be the shortest.

Put a label between these two lines:

    path %BaseDir%\Encoding Software;%path%

    for %%S in (Comedy Documentaries Films "Music Videos" Musicals TV) do (

Something like :_CLOOP_

Then at the very end of the code (after the last ")" put the following:

goto _CLOOP_


This will put it into a continous loop that you will have to break out of when you are ready.

There are more options in various degrees of complexity as well.
0
 
pony10usCommented:
Here is another option - actually just adding to what paul and I already gave you by giving you an option to restart or not after each full loop through the program as long as your system will support the "choice" command (the last 4 lines of the code)

@echo off
setlocal enabledelayedexpansion

set "BaseDir=D:\Videos\DVD Transfer"
set "Pass[1]=-an"
set "Pass[2]=-acodec libmp3lame -ac 2 -ab 128kb -ar 44100"

path %BaseDir%\Encoding Software;%path%

:CLOOP
for %%S in (Comedy Documentaries Films "Music Videos" Musicals TV) do (
  echo Processing %%~S folder
  set "Source=%BaseDir%\To Encode\%%~S"
  for %%F in ("!Source!\*.mkv") do (
    echo File: %%~nF
    set Filename=%%~nF

    set "Destination=%BaseDir%\Completed Encodes\%%~S\!Filename!"
    echo TV Documentaries | findstr "%%~S" >nul && (
      if "!Filename:~-7,1!"==" " (
        if "!Filename:~-6,1!"=="S" (
          if "!Filename:~-3,1!"=="E" (
            set "Destination=%BaseDir%\Completed Encodes\%%~S\!Filename:~0,-7!\Season !Filename:~-5,2!"
    ) ) ) )

            if "%%~S" == "Films" (
              set "Destination=%BaseDir%\Completed Encodes\%%~S\YZ0\!Filename!"
              for %%L in (ABC DEF GHI JKL MNO PQR STU VWX YZ0) do echo %%L | find /i "!Filename:~0,1!" >null && (
                set "Destination=%BaseDir%\Completed Encodes\%%~S\%%L\!Filename!"
              )
            )



    md "!Destination!" 2>nul

    for /l %%P in (1,1,2) do (
      echo Pass: %%P
      ffmpeg.exe -y -i "!Source!\%%~nF.mkv" -pass %%P -passlogfile "%BaseDir%\To Encode\%%~nF.log" -threads 8 -vcodec libxvid -vtag XVID -f avi -aspect 16:9 -r 25 -s 720x576 -b 1250kb -minrate 1250kb -maxrate 1250kb -bufsize 2048k !Pass[%%P]! "!Source!\!Filename!.avi"
    )
            del *.log
   
            move "!Source!\%%~nF.avi" "!Destination!\%%~nF.avi" >nul
   
    del "%BaseDir%\To Encode\%%~S\%%~nF.mkv" >nul
   
    start "" "D:\Program Files\FlashFXP\FlashFXP.exe" -tray -c2 -go FATNAS.fqf
    echo.
  )
set choice=
set /p choice="Do you wish to restart? Press 'y' and enter for Yes: "
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='y' goto CLOOP
)
0
 
paultomasiCommented:
No. CTRL-C is not the way to go. He did say loop while there are still files to process.
0
 
pony10usCommented:
Your right.  I missed part of your code.  That would make it a continous loop ONLY until nothing was found and would not require any input like I did in my last code.  Very nice.  

I don't like the CTRL-C option anyway however it is the simpilist solution as I originally stated in my first reply with code.
0
 
Delerium1978Author Commented:
Thanks both for your input. Paul won this round i think as he got the nail on the head 1st with loop until no more files to process.

Thanks again

James
0
 
paultomasiCommented:
WOW! That was luky!

Thank you.
0
 
pony10usCommented:
grats and good job Paul.  :)

Yes James, Paul's code is the best choice.  His will do the job without ANY interaction from the user. Both of my options require the user to do something.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now