Solved

Batch Script Tweak Mk III

Posted on 2012-03-30
11
439 Views
Last Modified: 2012-06-21
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
Comment
Question by:Delerium1978
  • 5
  • 4
  • 2
11 Comments
 
LVL 11

Expert Comment

by:paultomasi
ID: 37789433
Blimey! I remember that code...
0
 
LVL 26

Expert Comment

by:pony10us
ID: 37789472
@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
 

Author Comment

by:Delerium1978
ID: 37789476
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 11

Accepted Solution

by:
paultomasi earned 500 total points
ID: 37789554
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
 
LVL 26

Expert Comment

by:pony10us
ID: 37789559
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
 
LVL 26

Expert Comment

by:pony10us
ID: 37789605
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 37789606
No. CTRL-C is not the way to go. He did say loop while there are still files to process.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 37789610
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
 

Author Closing Comment

by:Delerium1978
ID: 37790642
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 37790787
WOW! That was luky!

Thank you.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 37796497
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question