Run youtube-dl periodically - batch-file

Say I have some problems with my connection to youtube, and keep getting WinError 10060 when downloading a full channel from time to time - and most importantly got desperate from trying to fix that (The problem is not with a specific video, as when I start over it downloads normally).
My aim is to bypass this by creating a batch file with an infinite loop which will run youtube-dl again with the same parameters after this error occurs.
I've been searching for a solution and my current loop bat contains this:
:LOOP
timeout /T 1 /NOBREAK
call MainDownload.bat
echo "Stop Recorded">> log.txt
if not ErrorLevel 1 goto :LOOP
After trying this I can see a "stop recorded" on my log when it happens, but the MainDownload.bat won't run again.
Can anyone offer a solution which will cause this to run infinitely until I manually close it?
Thanks in advance.

If you run youtube-dl.exe without parameters you'll get an error.
if you check the errorlevel after this you will get 2. So youtube-dl set an errorlevel when an error occur !
#echo off
youtube-dl.exe 2>nul
echo %errorlevel%
Run youtube-dldirectly in the bat to get the good errorlevel.
Something like this :
#echo off
:LOOP
timeout /T 1 /NOBREAK
youtube-dl -v -f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best" --yes-playlist --download-archive DownloadedVideosArchive -o "%%(uploader)s/%%(playlist)s/%%(playlist_index)s - %%(title)s [%%(upload_date)s].%%(ext)s" --batch-file Playlists.txt
if %errorlevel% GTR 0 (
echo [%time%] "Stop Recorded" >> log.txt
goto :LOOP
)

Related

Make a batch-file loop when there is internet or launch a program and exit when there is NO internet

The idea is to have a batch file ping the internet every 5 minutes to see if there is a connection. If there is an internet connection the batch file will loop every 5 minutes to ping again. If there is NO internet connection then the batch file will launch a program and exit. I created a batch file that does everything but exit when the internet is not detected.
What I have so far loops just the way it should but I'm stuck at making the file exit if the internet connection is NOT connected and notepad.exe is launched. I don't know much about batch-files and am trying to piece things together from searches, I need help.
#echo off
setlocal
cls
:loop
#ping 209.222.18.218 -n 1 -w 1000> nul
if %ERRORLEVEL% EQU 1 start C:\windows\notepad.exe
timeout /t 60 >null
goto loop
Expected result: the batch file pings the internet every 5 minutes to detected either there is or isn't an internet connection. If there is an internet connection the batch file will loop every 5 minutes. If there isn't an internet connection the batch file will launch notepad.exe and then exit itself.
Actual result, I get the batch file to loop when it detects internet but I can't get it to exit itself when there is NO internet.
Rearrange your lines a bit:
:loop
timeout 300 >nul
ping 209.222.18.218 -n 1 -w 1000 |find "TTL=" >nul
if %errorlevel%==0 goto :loop
REM if not errorlevel 1 goto :loop
start C:\windows\notepad.exe
Use one of the if lines - whichever you are more comfortable with.

Batch File "if" and "goto" problems

Whenever i run the .bat file it all goes well i choose my time it says it in minutes and all until it comes to confirming your decision and i really have no clue how to fix it for some reason it just goes to :shutdown
Intention: I am using this as a simple little tool when installing games for example if i have to leave steam to download and install a large game overnight i just choose a time and leave it :)
Note: I have no experience with coding ( except changing values on LUA scripts in games ) so an explanation would be
Greatly Appreciated and yes sure i can just use the shutdown command but i wanted to create something a little more fancy
Also if someone could explain to me what the
/s in shutdown
and /a in set timedelay commands do because as i stated i have little to no experience
.
If you can't answer or can't be bothered thank you for your time either way!
Thank you in advance! :)
.
#echo off
cls
title Delayed Shutdown
echo Delayed Shutdown by Martin Angelov
echo Press Any Key To Choose Delay
Pause>NUL
:choosedelay
cls
echo Type in the desired ammount of delay
set /p timedelaysec=
set /a timedelaymin=%timedelaysec% / 60
:confirmation
echo Your current desired shutdown time is:
echo %timedelaysec% Seconds
echo ( %timedelaymin% Minutes )
echo Press 1 to Confirm Shutdown
echo Press 2 to Change Delay
echo press 3 to Exit Program
set /p confirmaation =
if "%confirmaation%" == "1" goto shutdown
if "%confirmaation%" == "2" goto choosedelay
if "%confirmaation%" == "3" goto exit
:shutdown
cls
echo Shutting Down!
echo Delay Chosen:
echo %timedelaysec% Seconds
echo ( %timedelaymin% Minutes )
pause
exit
:exit
cls
echo Exiting Program...
ping 0.0.0.0 -n 2
exit
:Temporary so it doesn't actually enable the shutdown
shutdown /s /t %timedelaysec%
In batch files, it's usually something simple and frustrating.
Try
set /p confirmaation=
(Note that I removed a space before the equals.)
If that fixes your problems, your environment variable wasn't set to any of the three values you checked for and the logic very naturally went line-by-line to :shutdown.
Good luck!

Batch : Use the FOR command to check the files in the folder

I have made ​​a batch script to perform the following tasks :
if the file abc.laccdb still in the update folder , show a message : wait a minute.... if abc.laccdb file is not in the 'update' folder , show a message : updating data successfully..
My Batch script is :
#ECHO OFF
start update_data.vbs
:check
FOR %%F IN (update\abc.laccdb) DO (
echo wait a minute...
goto :check
)
echo updating data successfully
pause
with the script above , the message "wait a minute ... " continuously displayed in command prompt window, even though abc.laccdb file has not in the Update folder. properly if the abc.laccdb file is not there in the Update folder, the bacth application run the next line (echo updating data successfully). please correct my script. thank you :)
A for command with a wildcard will enumerate the files matching the wildcard, but without a wildcard, it will not ensure that the file exists, so the code in the do clause will always be executed whether the file exists or not.
Use
#ECHO OFF
start update_data.vbs
:check
ping -n 3 localhost >nul 2>nul
if exist "update\abc.laccdb" (
echo wait a minute...
goto :check
)
echo updating data successfully
pause
It is not always a good idea to have a waiting loop without some kind of wait. The ping has been included to generate a 2 seconds pause between loops to reduce the cpu usage.
Using MC ND's answer:
#ECHO OFF
echo wait a minute...
start update_data.vbs
ping -n 5 localhost >nul 2>nul
:check
if exist "update\abc.laccdb" (
ping -n 3 localhost >nul 2>nul
goto :check
)
echo updating data successfully
pause
Display echo wait a minute... message in advance...

Bat file stopping and i'm not sure why

So, I have a bat file I've been building to do a simple profile backup/restore. I'm running Windows 7 64bit.
I get through 99% of the script until it gets to this point and then dies. To be clear, in the environment I'm working in, I HAVE to turn off UAC for certain things. Someone much higher up than me made that call.
However, this needs to do this but it dies after the "[ COMPLETE ]" is echoed. It doesn't close the window, it just goes back to the C prompt.
The only other thing it's going (trying) to do after that is log the user off after a 15 second timer.
Any help would be much appreciated.
echo [ Turning off UAC... ]
C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
ping -n 2 -w 1000 127.0.0.1 > nul
echo [ COMPLETE ]
ping -n 2 -w 1000 127.0.0.1 > nul
echo.
ECHO ==========================================================
ECHO = Restore Complete =
ECHO = Computer will logoff in 15 seconds to apply changes. =
ECHO ==========================================================
TIMEOUT 16
shutdown /l
#pause
:EOF
I made a test batch file that does what you're trying:
#echo off
echo before
cmd /k echo I'm doing it!
echo after
And here was the result:
C:\files\j>test
before
I'm doing it!
So I changed it to call:
#echo off
echo before
call echo I'm doing it!
echo after
And got this:
C:\files\j>test
before
I'm doing it!
after
So clearly, /k isn't for you. But CALL will put it in a new shell... So my recommendation is to just change that line to be :
%windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
No cmd, no \k, no anything - just run the REG.exe.
If you have to, use the CALL since you're adding a registry key and it should stay there.

How to start a batch file when a text file is created?

I am trying to work out how to run a batch file when a text file is created. I have tried to research but am unable to find anything that helps.
The Pseudocode of what I want is something like this:
When "Response.txt" is created run "2 Week.bat".
Thanks.
#echo off
:start_loop
if exist "c:\Response.txt" (
start cmd /c "c:\2weeks.bat"
goto :exodus
)
rem :: wait for 1 minute
w32tm /stripchart /computer:localhost /period:1 /dataonly /samples:60 >nul 2>&1
goto :start_loop
:exodus
This will work on Vista and later:
#echo off
:loop
if exist "c:\folder\Response.txt" "c:\folder\2 week.bat"
rem - reduce CPU usage by waiting for 15 seconds
timeout /t 15 /nobreak
goto :loop

Resources