#1 03 Jun 2021 05:17

Rekrul
Member
Registered: 17 Apr 2016
Posts: 98

Simple install script doesn't work under Win7

A friend (who lives in a different country) asked me for an easy way to download MP3 files from YouTube videos. I was going to direct her to a GUI for YouTube-DL, but I couldn't figure out to get it to actually save in MP3 format without also preventing it from downloading full videos (use the extra parameters option to tell it to only download audio). So, I spent way more time that I really should have, making an easy to use, menu-driven script to serve as an interface to YouTube-DL. It lets you change the download location, saves your choice for next time, lets you update YouTube-DL and download both videos and MP3s.

To complete the package, I made a simple installer script that creates two directories in the root of the C: drive, copies the required EXE files to one, copies the script and a couple other files (icon and help file) to the other new dir, and then creates a Desktop shortcut for it.

She hasn't had the chance to sit down with me on the phone and troubleshoot it yet (this weekend, maybe), but she said she followed my instructions, double-clicked my install script and the window appeared and then instantly closed without doing anything. Since the copying portion should have taken at least long enough for her to read the text, I'm guessing that for some reason, it failed to create the two directories on C:, which caused all the copy commands to fail and there was nothing to create a Desktop shortcut to.

She has Windows 7 Pro, 64-bit. She does not use a restricted user account, she simply boots right into Windows, so I'm pretty sure she has administrator level access. She's never had any trouble running traditional software installers before.

So my question is: Will a Batch script running under Windows 7 run into problems trying to create two new directories in the root of C:?

For reference, here is the script that I sent her with my creation;

@echo off
cls
echo.
echo.
echo Creating Directories...
echo.
echo.
md c:\Batch 2>nul
md c:\Commands 2>nul
echo Copying files...
echo.
echo.
copy Simple-Media-Downloader.bat c:\Batch\ >nul
copy Simple-Media-Downloader.ico c:\Batch\ >nul
copy Simple-Media-Downloader.txt c:\Batch\ >nul
copy ffmpeg.exe c:\Commands\ >nul
copy youtube-dl.exe c:\Commands\ >nul
echo Creating Desktop shortcut...
echo.
echo.
nircmd shortcut "c:\Batch\Simple-Media-Downloader.bat" "~$folder.desktop$" "Simple-Media-Downloader" "" "c:\Batch\Simple-Media-Downloader.ico"

I sent all the output to nul and didn't include a Pause command because I didn't anticipate it running into any problems. I tested it thoroughly before sending it to her and everything worked perfectly.

I did send her a replacement that shows all output and pauses before closing, but she hasn't had time to try it yet.

For what it's worth, I sent a similar install script to a friend with Windows 10 a while back and he had no problems with it.

Offline

#2 04 Jun 2021 00:24

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: Simple install script doesn't work under Win7

You're able to make a folder in the root of C:\, but that's a stupid place for new folders to go, so you shouldn't try doing it anyway.

Just use relative paths to the script with %~dp0 instead.

Last edited by Shadow Thief (04 Jun 2021 00:29)

Offline

#3 04 Jun 2021 04:56

Rekrul
Member
Registered: 17 Apr 2016
Posts: 98

Re: Simple install script doesn't work under Win7

Shadow Thief wrote:

You're able to make a folder in the root of C:\, but that's a stupid place for new folders to go, so you shouldn't try doing it anyway.

I chose the root of C: for two reasons;

1. I've read that newer versions of Windows (I have an old system) place restrictions on where you can create directories and copy files.

2. I wanted them to be easy to find if she ever needs to go into them to update one of the EXE files or make changes to one of the scripts. Finding them in the root directory is easier than digging through Program Files (or Program FIles x86) for her.

Shadow Thief wrote:

Just use relative paths to the script with %~dp0 instead.

I don't understand what you mean.

I did send her a "portable" version that she can just copy to an external drive and run by double-clicking the BAT file, but she likes to have a Desktop icon for everything, and Desktop shortcuts will fail if the drive letter changes, as the letters for external drives sometimes do. So I made a version that could be installed.

Besides, what's the danger in creating two new directories in the root of C:? They don't interfere with anything or overwrite anything. Over the years, various programs have created the following directories on my system root;

83f8d5212c04a662d490560e\
CanoScan\
ef95af7712b807eff3c157f2b5b87e82\
NVIDIA\
swsetup\

Offline

#4 04 Jun 2021 05:20

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: Simple install script doesn't work under Win7

Rekul wrote:

Besides, what's the danger in creating two new directories in the root of C:? They don't interfere with anything or overwrite anything.

You already have folders on your computer where you can put them. There's literally no reason to put them directly in C:.

Rekrul wrote:
Shadow Thief wrote:

Just use relative paths to the script with %~dp0 instead.

I don't understand what you mean.

@echo off
set "root_directory=%~dp0"
set "bat_dir=%root_directory%\Batch"
set "cmd_dir=%root_directory%\Commands"
mkdir "%bat_dir%" 2>nul
mkdir "%cmd_dir%" 2>nul

copy Simple-Media-Downloader.* "%bat_dir%" >nul

copy ffmpeg.exe "%cmd_dir%" >nul
copy youtube-dl.exe "%cmd_dir%" >nul

nircmd shortcut "%bat_dir%\Simple-Media-Downloader.bat" "~$folder.desktop$" "Simple-Media-Downloader" "" "%bat_dir%\Simple-Media-Downloader.ico"

Last edited by Shadow Thief (04 Jun 2021 18:32)

Offline

#5 04 Jun 2021 17:21

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,118
Website

Re: Simple install script doesn't work under Win7

If the person you are sending these to is just running them directly from the email message, then the current directory may be some random temporary folder.

So you could improve this by:
- hard coding the installation folder
- checking for the existence of the dependent files and throwing a helpful error message if they are missing.

If I was writing this for myself I'd use %~dp0 but often it is easier to debug problems by reducing the number of possible configuration choices.

Offline

#6 07 Jun 2021 03:22

Rekrul
Member
Registered: 17 Apr 2016
Posts: 98

Re: Simple install script doesn't work under Win7

Shadow Thief wrote:

You already have folders on your computer where you can put them. There's literally no reason to put them directly in C:.

If the script can't make the directories in C: for whatever reason (I still haven't had a chance to troubleshoot this with her), it would probably run into the same problem creating a directory in Program Fles.

Shadow Thief wrote:
set "root_directory=%~dp0"

Thanks for this. I never knew that you could do this to reference the script file itself. I only knew how to do this in conjunction with files read during a For loop.

Installing to relative directories of wherever the script is run from would have it installed to her downloads directory. Of course she could make a directory somewhere else, copy the files there and then run the install script, but that kind of negate the convenience of having an installer in the first place. If I were going to do that, I'd have the directories/files set up ahead of time and all the install script would do is create the Desktop shortcut.

Simon Sheppard wrote:

If the person you are sending these to is just running them directly from the email message, then the current directory may be some random temporary folder.

Most likely it would be her common Downloads directory.

Simon Sheppard wrote:

So you could improve this by:
- hard coding the installation folder

That's what I tried to do. Maybe the root of C: isn't the best choice, but theoretically, it should have worked.

Simon Sheppard wrote:

- checking for the existence of the dependent files and throwing a helpful error message if they are missing.

I tried to make it as simple as possible since too much information tends to confuse her. Also, I tested it myself multiple times and didn't anticipate that it would fail like that. I still haven't had a chance to troubleshoot it with her, but the only reason I can see for the window instantly closing without creating a Desktop icon is if it failed to make the directories. If that happens, the copy commands would fail and there wouldn't be anything to create a shortcut to, which would all happen in the blink of an eye.

Simon Sheppard wrote:

If I was writing this for myself I'd use %~dp0 but often it is easier to debug problems by reducing the number of possible configuration choices.

I did send her a "portable" version that can be run from any directory just by double-clicking the main script, but she hasn't tried that yet. She's often quite timid about trying things on her own. Frankly, I'm surprised that she tried using the install script, even though I included a ReadMe.txt file that told her exactly what to do. It also covers the portable version in detail as well.

Normally in larger scripts, I do try to include as much error checking as possible. Entering numbers checks to make sure that the user actually enters numbers. All prompts for user input check for empty variables, which I sometimes use as an abort back to the menu or it keeps the previous value. Anything even semi-destructive asks for confirmation. Because it saves the user's choice of download location, it checks to make sure that the drive and path exist when the script runs and if not, it jumps right to the change destination routine. The upgrade option for YouTube-DL backs up the current version so that it can be restored if the new version has any problems. It even has a good-sized help file detailing every option.

I probably spent way more time on this than I needed to. smile

I didn't check for the existence of the EXE files though. I probably should have, but if they're not there, I don't know how to make the script download them from scratch and she probably wouldn't know how to do it manually.

For reference, here's the portable version. To make it work, make a directory called "SMD-Files\" in the same directory as the script, and put copies of YouTube-DL.exe and ffmpeg.exe in it. The "Help" option looks for a file in that directory called "Simple-Media-Downloader.txt". The script will make a directory called "Downloads\" when run. You can change the download location, but it doesn't save your choice between sessions.

@echo off
title Simple-Media-Downloader

for /f %%D in ('cd') do set CurrDir=%%D
path %path%;%CurrDir%SMD-Files

mode con: cols=60 lines=26
set Drive=%CurrDir:~0,1%
set Dir=%CurrDir:~3%\Downloads

:Main
cls
%Drive%:
cd \
md "%Dir%" 2>nul
cd "%Dir%"
echo.
echo  Simple Video Downloader
echo  -=-=-=-=-=-=-=-=-=-=-=-
echo.
echo.
echo  1) Download videos
echo  2) Download MP3s
echo.
echo.
echo  3) Change download location:
echo     %Drive%:\%Dir%\
echo.
echo  4) Update YouTube-DL.exe
echo.
echo  5) View help
echo.
echo.
echo  U) Uninstall
echo.
echo.
echo  E) Exit
echo.
echo.
choice /n /c 12345ue /m "Please choose [1-5,U,E]: "
if errorlevel 7 goto Exit
if errorlevel 6 goto Uninstall
if errorlevel 5 goto Help
if errorlevel 4 goto UpdateYouTubeDL
if errorlevel 3 goto ChangeDestination
if errorlevel 2 goto DownloadMP3
if errorlevel 1 goto DownloadVideo

:DownloadVideo
cls
echo.
echo.
echo  Download videos
echo.
echo  Enter the URL(s) of the videos you want
echo  to download. Press Enter by itself to
echo  return to the main menu.
echo.
echo.
:DV
set url=
set /p url=Url:
if "%url%"=="" goto Main
start "Download Video" youtube-dl.exe --ignore-errors --no-mtime --no-check-certificate --no-playlist %url% -o "%%(title)s.%%(ext)s"
goto DV

:DownloadMP3
cls
echo.
echo.
echo  Download MP3s
echo.
echo  Enter the URL(s) of the MP3s you want to
echo  download. Press Enter by itself to return
echo  to the main menu.
echo.
echo.
:DMP3
set url=
set /p url=Url:
if "%url%"=="" goto Main
start "Download MP3" youtube-dl.exe --extract-audio --audio-format mp3 --ignore-errors --no-mtime --no-check-certificate --no-playlist %url% -o "%%(title)s.%%(ext)s"
goto DMP3

:ChangeDestination
cls
echo.
echo.
echo  Enter the letter of the drive you want
echo  to download to. Press Enter by itself to
echo  keep the same drive.
echo.
echo.
set /p Drive=Enter new drive letter: 
if "%Drive%"=="" (
echo.
echo.
echo  ERROR! You must enter a drive!
echo.
echo.
pause
goto ChangeDestination)
if not exist %Drive%: (
echo.
echo.
echo ERROR! Drive not found!
echo.
echo.
set Drive=
pause
goto ChangeDestination)
:Directory
cls
echo.
echo.
echo  Enter the name of the directory you want
echo  to download to. To enter nested directory
echo  names, separate them with a backslash. 
echo  Enter a single period by itself to have
echo  downloads go into the root directory of
echo  the drive. Press Enter to keep the same
echo  directory.
echo.
echo  NOTE: If the directory doesn't exist, it
echo  will be created.
echo.
echo.
set /p Dir=Enter new directory: 
goto Main

:UpdateYouTubeDL
cls
echo.
echo  Just a moment, checking current version...
for /f %%V in ('youtube-dl --version') do set Version1=%%V

cls
echo.
echo  YouTube-DL Update
echo  -=-=-=-=-=-=-=-=-
echo.
echo  Current version is: %Version1%
echo.
echo.
echo  1) Download latest version of YouTube-DL
echo.
echo  2) Undo last update
echo.
echo.
echo  M) Main menu
echo.
echo  E) Exit
echo.
echo.
choice /n /c 12me  /m "Please choose [1-2,M,E]: "
if errorlevel 4 goto Exit
if errorlevel 3 goto Main
if errorlevel 2 goto UndoUpdate
if errorlevel 1 goto Update

:Update
cls
echo.
echo.
echo  Checking for latest version...
echo.
echo.
copy /y "%CurrDir%\SMD-Files\youtube-dl.bak" "%CurrDir%\SMD-Files\youtube-dl.tmp" >nul 2>&1
copy /y "%CurrDir%\SMD-Files\youtube-dl.exe" "%CurrDir%\SMD-Files\youtube-dl.bak" >nul 2>&1
youtube-dl --update
ping -n 5 127.0.0.1 >nul
for /f %%V in ('youtube-dl --version') do set Version2=%%V
if "%Version1%"=="%Version2%" copy /y "%CurrDir%\SMD-Files\youtube-dl.tmp" "%CurrDir%\SMD-Files\youtube-dl.bak" >nul 2>&1
del "%CurrDir%\SMD-Files\youtube-dl.tmp" >nul 2>&1
echo.
echo.
pause
goto UpdateYouTubeDL

:UndoUpdate
if not exist "%CurrDir%\SMD-Files\youtube-dl.bak" (
cls
echo.
echo.
echo  No previous version to restore!
echo.
echo.
pause
goto UpdateYouTubeDL
)
cls
echo.
echo.
echo  This will overwrite the current
echo  version with the previous version.
echo.
echo.
choice /n  /m "Are you sure? "
if errorlevel 2 goto UpdateYouTubeDL
del "%CurrDir%\SMD-Files\youtube-dl.exe"
ren "%CurrDir%\SMD-Files\youtube-dl.bak" youtube-dl.exe
cls
echo.
echo.
echo  Previous version restored.
echo.
echo.
pause
goto UpdateYouTubeDL

:Help
start Notepad "%CurrDir%\SMD-Files\Simple-Media-Downloader.txt"
goto Main

:Uninstall
cls
set Confirm=no
echo.
echo.
echo  This will remove YouTube-DL and
echo  this script as well as the support
echo  files.
echo.
echo  Type YES to confirm. Type anything
echo  else to abort.
echo.
echo.
set /p Confirm=Uninstall? 
If not "%Confirm%"=="YES" goto Main
cd /D "%CurrDir%\SMD-Files"
del ffmpeg.exe
del youtube-dl.exe
del youtube-dl.bak 2>nul 
del Simple-Media-Downloader.prefs
del Simple-Media-Downloader.txt
cd ..
rd SMD-Files
start /b "" cmd /c del "%~f0"^&exit

:Exit

Last edited by Rekrul (07 Jun 2021 03:31)

Offline

#7 07 Jun 2021 11:40

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Simple install script doesn't work under Win7

Personally I would side step the "am I an Admin?", "can I create at C:\?" and "this might end up somewhere weird" issues by creating the directories under "%USERPROFILE%" (or a subdir therein).  This does mean you'd have to install per user, but that's probably a small price to pay and may not even be a consideration for a non-shared computer.

Last edited by bluesxman (07 Jun 2021 12:05)


cmd | *sh | ruby | chef

Offline

#8 07 Jun 2021 20:07

Rekrul
Member
Registered: 17 Apr 2016
Posts: 98

Re: Simple install script doesn't work under Win7

bluesxman wrote:

Personally I would side step the "am I an Admin?", "can I create at C:\?" and "this might end up somewhere weird" issues by creating the directories under "%USERPROFILE%" (or a subdir therein).  This does mean you'd have to install per user, but that's probably a small price to pay and may not even be a consideration for a non-shared computer.

Installing to the profile directory always seemed even messier to me.

Offline

#9 08 Jun 2021 11:52

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Simple install script doesn't work under Win7

Rekrul wrote:

Installing to the profile directory always seemed even messier to me.

For my money it feels like exactly the right place.
* You don't need to make any assumptions about which drive to address in your install script.
* No special permission or elevation should be required.
* There are not likely to be any issues managing the directories and files (e.g. where default behaviour changes across Windows versions).
* There are not likely to be any issues with other users (should they exist) messing with your stuff.
* You're already obfuscating the location via desktop shortcuts.
* If you want to give easy user access to the containing directories for some reason, you could add further shortcuts.

But ultimately do what makes most sense for you. ¯\_(ツ)_/¯

Last edited by bluesxman (08 Jun 2021 11:53)


cmd | *sh | ruby | chef

Offline

Board footer

Powered by FluxBB