@echo off
color 06
title created by AAIE
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "[System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
choco install youtube-dl
set /p input="Enter Link For Playlist:"
set /p index="Enter Index For videos Seprated by ',':"
mkdir playlist_videos
cd playlist_videos
youtube-dl --playlist-items %index% %input%
how can I check if choco or youtube_dl is installed in windows or not or the main question what is the conditions i need to check if it true then use commands directly if not installed then it will install them and use the same commands
CodePudding user response:
Here are some examples using where
to locate and perform actions based on the result:
@echo off
(where choco.exe)>nul 2>&1
if errorlevel 1 (
echo Choco not installed. Installing now..
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "[System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
)
choco install youtube-dl
set /p input="Enter Link For Playlist:"
set /p index="Enter Index For videos Seprated by ',':"
mkdir playlist_videos
cd playlist_videos
youtube-dl --playlist-items %index% %input%
or by using conditional operators:
@echo off
(where choco.exe)>nul 2>&1 && goto :installed || goto :install
:install
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "[System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
:installed
choco install youtube-dl
set /p input="Enter Link For Playlist:"
set /p index="Enter Index For videos Seprated by ',':"
mkdir playlist_videos
cd playlist_videos
youtube-dl --playlist-items %index% %input%
Note these are straight forward examples, there are no error handling built in, like what happens if coco install fails, etc.
As per your question on 2>&1 >nul
stdout
(seen as 1>
when redirecting) is the output stream of a command where stderr
(seen as 2>
when redirecting.)
When doing >nul
or > file.txt
we are effectively redirecting the output of a command to nul
(not seen on console) or to a file (logging purpose) but not everything goes to the stdout
stream by default. so we need to redirect stderr stream to stdout, then redirect stdout
to nul
or a file.
You can obviously also redirect independently.
commandname 1>out.log 2>error.log