download a file from a website with batch - windows

i am attempting to create an automation script to install and update the mine craft editor MCEDIT on windows. here is what i have so far
#echo off
REM finds the architecture of the windows installation.
reg Query "HKLM\Hardware\Description\System\CentralProcessor\0" | find /i "x86" > NUL && set arc=32BIT || set arc=64BIT
if %arc%==32BIT GOTO 32
if %arc%==64BIT GOTO 64
REM placeholder for testing.
set version=1.5.3.0
:32
REM attepts to use bitsadmin to download file but fails.
bitsadmin.exe /transfer "JobName" https://github.com/Khroki/MCEdit-Unified/releases/download/^%version%/MCEdit.^%version%.Win.32bit.exe .\install.exe
:64
REM another attempt at bitsadmin that also fails
bitsadmin.exe /transfer "test" /download https://github.com/Khroki/MCEdit-Unified/releases/download/^%version%/MCEdit.^%version%.Win.64bit.exe .\install.exe
REM unzips the auto extractor to current location
install.exe /s /d %cd%
:end
echo end
pause
so the first issue i am having is that.
bitsadmin.exe /transfer "JobName" https://github.com/Khroki/MCEdit-Unified/releases/download/^%version%/MCEdit.^%version%.Win.32bit.exe .\install.exe
always fails, it is also depreciated and soon to be dropped from windows entirly. i am looking for a native solution that would not require a user to download anything in additon to this script to complete the task.
The second issue i am having is that i have no way to get the current version. the files are always stored at
https://github.com/Khroki/MCEdit-Unified/releases/download/(version_number)/mcedit.(version_number).exe
So i need a way to test for the newest version on the server or a way to print a text file containing the directories and do it locally.

Related

Access system32 from VisualStudio 2019 build script

In a post build step i want check if OpenSSH.Client and OpenSSH.Server is installed and install it if it is not there. Checking the installed features with Powershell needs administrative privileges.
Therefore i test the existence of the relevant OpenSSH commands with the following code (extract of long script)
SET /a res=0
SET /a three=3
%windir%\system32\OpenSSH\ssh-keygen.exe /?
echo Errorlevel %ERRORLEVEL%
IF %ERRORLEVEL%==%three% (SET /a res=%res%+1)
%windir%\system32\OpenSSH\ssh-keyscan.exe /?
echo Errorlevel %ERRORLEVEL%
IF %ERRORLEVEL%==%three% (SET /a res=%res%+1)
SET /a check=0
IF %res% == %check% (
echo already installed
goto skipopenSSH
)
echo installation
:skipopenSSH
By checking the existence of the commands no admin privileges are necessary for the check so a normal build will not cause a administrative popup.
On cmd.exe it works fine, but as a post build step in Visual Studio both commands in %windir%\systrem32\OpenSSH are not found, although the variable is expanded to the same c:\Windows\System32\OpenSSH\*.exe as if executed on commandline.
After trying the different find mechanisms which all fail i made a test batch file C:\Windows\System32\OpenSSH\ssh-keyscan.exe /?
which leads to a file not found error if executed as a post build step. So the real question should be: Modifies the visual studio build step commandline the path?
The directory OpenSSH exists in directory %SystemRoot%\System32 with the files ssh-keygen.exe and ssh-keyscan.exe depending on version of Windows 10. The directory %SystemRoot%\System32 is for 64-bit applications on 64-bit Windows. But Visual Studio is a 32-bit application and for that reason 32-bit Windows command processor in directory %SystemRoot%\SysWOW64 is executed to process the batch file with the commands to execute as post build step.
Microsoft documented with WOW64 Implementation Details, File System Redirector and Registry Keys Affected by WOW64 and other documentation pages how Windows on Windows 64 works.
All file system accesses to %windir%\system32\OpenSSH in the batch file processed by 32-bit %SystemRoot%\SysWOW64\cmd.exe being started by 32-bit Visual Studio results in the approach to access %SystemRoot%\SysWOW64\OpenSSH which does not exist at all. There is no subdirectory OpenSSH in Windows system directory for 32-bit applications.
One solution would be using the following code for the batch file executed as post build step.
rem This simple check is for 32-bit Windows and for 64-bit Windows with batch
rem file executed in 64-bit environment by 64-bit Windows command processor.
set FolderSSH=%SystemRoot%\System32\OpenSSH
if exist %FolderSSH%\ssh-keygen.exe if exist %FolderSSH%\ssh-keyscan.exe goto UseOpenSSH
rem This check is for 64-bit Windows with batch file executed
rem in 32-bit environment by 32-bit Windows command processor.
if exist %SystemRoot%\Sysnative\cmd.exe set FolderSSH=%SystemRoot%\Sysnative\OpenSSH
if exist %FolderSSH%\ssh-keygen.exe if exist %FolderSSH%\ssh-keyscan.exe goto UseOpenSSH
rem Search entire system drive on machines without Windows 10 or with older
rem versions of Windows 10 with OpenSSH not installed at all by default.
for /F "delims=" %%I in ('%SystemRoot%\System32\where.exe /R %SystemDrive%\ ssh-keygen.exe 2^>nul') do (
if exist "%%~dpIssh-keyscan.exe" for %%J in ("%%~dpI.") do set "FolderSSH=%%~fJ" & goto UseOpenSSH
)
echo ERROR: ssh-keygen.exe AND ssh-keyscan.exe not found.
rem More code to handle this use case.
goto :EOF
:UseOpenSSH
echo Found ssh-keygen and ssh-keyscan in: "%FolderSSH%"
rem More code to handle this use case with existing SSH tools.
The remarks explain most of the code. The inner FOR loop is used to get the full qualified name of the directory containing ssh-keygen.exe and ssh-keyscan.exe without backslash at end of the folder path to have FolderSSH defined always without a backlash at end for further usage in the batch file.
Please note that it is safe to use %FolderSSH%\ssh-keygen.exe and %FolderSSH%\ssh-keyscan.exe without surrounding " at beginning of the batch script as it is impossible that %FolderSSH% expands to a folder path containing a space or one of these characters &()[]{}^=;!'+,`~.
But "%FolderSSH%\ssh-keygen.exe" and "%FolderSSH%\ssh-keyscan.exe" must be used on the command lines below the label UseOpenSSH because it could happen that WHERE was used to find the two executables anywhere on system drive and for that reason %FolderSSH% could expand now to a folder path containing a space or a character with a special meaning for Windows command processor outside a double quoted argument string.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?
goto /?
if /?
rem /?
set /?
where /?
Read also the Microsoft article about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded where command line with using a separate command process started in background with %ComSpec% /c and the command line between ' appended as additional arguments on which 2^>nul became already 2>nul.
The following idea also uses the where.exe command, as mentioned in the comments. This one will search the system drive for ssh-keyscan.exe, and if found will ensure that ssh-keygen.exe is also located there. If both are located in the same place, then it is considered as already installed:
#Set "dirOpenSSH="
#For /F Delims^= %%G In (
'%__AppDir__%where.exe /R %SystemDrive%\ ssh-keyscan.exe 2^>NUL'
)Do #For /F Delims^= %%H In (
'%__AppDir__%where.exe "%%~dpG.":ssh-keygen.exe 2^>NUL'
)Do #Set "dirOpenSSH=%%~dpH"
#If Defined dirOpenSSH (
Echo OpenSSH is available at %dirOpenSSH%.
%__AppDir__%timeout.exe 3 /NoBreak>NUL
GoTo skipopenSSH
)
#Echo OpenSSH is not available, beginning installation.
#%__AppDir__%timeout.exe 3 >NUL
:skipopenSSH
Please note that where.exe and timeout.exe require a minimum OS version of Windows NT 6.x
As your application is currently a 32-bit one, the console session it is using is not accessing the 64-bit locations. To fix that you could replace %__AppDir__%where.exe, with %SystemRoot%\SysNative\where.exe
It is probably worth mentioning that in the versions of windows-10 with OpenSSH part of the base OS, the location, %SYSTEMROOT%\System32\OpenSSH\, should exist in %PATH%, so you should be able to find your files directly using them:
%__AppDir__%where.exe ssh-keygen.exe
%__AppDir__%where.exe ssh-keyscan.exe
For %I In (ssh-keygen.exe) Do #Echo %~$PATH:I
For %I In (ssh-keyscan.exe) Do #Echo %~$PATH:I
I have found on the systems which have upgraded from an earlier version of Windows, that many of the default locations using C:\Windows were replicated, but using %SYSTEMROOT%/%SystemRoot% instead.
In that state, the Where command and For's %~$PATH: failed to locate some items which did exist and should have been found.
Removing the duplicated items, I removed those which were carried over, (those not using variables), although removing either format seems to have worked fine.
Both where and %~$PATH: were then functioning as intended.

“Remote command failed with exit status 127”

I have a batch file as below:
#echo off
REM <--Fetching installed service pack version and storing it in var-->
FOR /f "tokens=* " %%a in ('findstr /I "install.servicepack" ^< "C:\A\B\C\D.properties" ') DO SET temp=%%a
SET var=%temp:~22%
REM <-- I tested, correct value is getting assigned to var say 1.2.3-->
REM <--Next, I am changing the directory using CD, in which X, Y and Z is a fixed directory path and after that it is variable based upon %var% value
cd c:\X\Y\Z\%var%
echo %cd%
REM <-- I tested and directory is correctly set against cd c:\X\Y\Z\1.2.3
REM <--With in c:\X\Y\Z\%var% (c:\X\Y\Z\1.2.3), there is an exe called uninstaller.exe and I am executing it is below:
dir
ECHO MESSAGE: Starting Silent Uninstallation of ABC Package
uninstaller.exe -options -silent
ECHO MESSAGE: Finished Silent Uninstallation of ABC Package
Set-up: I have Jenkins installed on windows and via sshexec task in ANT, I am calling the above batch file in a remote windows machine using cygwin openssh.
Issue: The above script when called from Jenkins job using above set-up, it is returning “Remote command failed with exit status 127”. However, if I am hard coding the value of %var% in cd as cd c:\X\Y\Z\a.b.c rather than passing as cd c:\X\Y\Z\%var%, script is executing fine, i.e.; directly changing the directory with the exact path (cd C:\X.Y.Z.\1.2.3).
I tried couple of ways to call uninstaller.exe after changing the directory but no success.
Please help.
Do NOT change value of TEMP variable: this is a special system variable holding the temporary directory env. variable.
Please choose another variable name.
FOR /f "tokens=* " %%a in ('findstr /I "install.servicepack" ^< "C:\A\B\C\D.properties" ') DO SET t=%%a
SET var=%t:~22%
If you change temporary directory, programs relying on it may crash (and there are a lot of them).

Running Multiple Installations with System-Reboot Inbetween them

Currently I have a set of software-Installations(and their paths) that i have to install on my Windows Machine.
What I do now is -- Hit RUN every time and type in the software installation path into it..
What I want is to design a Batch file which would install all the applications and REBOOT my system after every successful installation and then continue with the NEXT item in the list..
Is it possible using a .bat file ??
This really isn't something batch was designed for, so this will be a bit hacky. It's not elegant by any means but give it a shot, it might work for you.
for /f %%a in (C:\files.txt) do (
start /wait %%a
exit /b
)
for /f "skip=1" %%b in ("C:\files.txt) do (
echo %%b >>C:\newfiles.txt
)
xcopy C:\newfiles.txt C:\files.txt /y
del C:\newfiles.txt /f /q
shutdown /r /t 0 /f
The idea being that you have a text file with the paths of the executables that you want to install. It will go through and execute the first file in the list, wait for it to complete, then re-write the list without the file it just installed.
This is dependend on the setup file having no user interaction and exiting by itself, or maybe it's just to make things easier - in which case just go through each install yourself, and when it finishes the batch file will do the rest.
On the note of rebooting and continuing you will either need to run the batch file again yourself or put it in the registry to start up itself, the latter command being
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v "MyBatchInstaller" /d "C:\MyBatchFile.bat" /f
Hope this helps

Problems working with different drive units in a batch file

This is my first batch file, and also my first time with batch language (I usually use UNIX and don't know a lot about Windows commands).
I'm creating a batch file called install.bat which does all the work to install a Java application from source files. Here a snapshot of the install section:
#ECHO off
SET INSTALL_DIR=%1\
SET SRC_DIR=sources\
SET LIB_DIR=lib\
SET IMG_DIR=img\
SET BIN_DIR=bin\
SET INIT_DIR=%CD%
SET MAIN_CLASS=%SRC_DIR%\main\Main.java
SET CLASS_PATH=%LIB_DIR%log4j.jar;%LIB_DIR%jdom.jar;
SET JAR_NOM=myApp.jar
SET JAR_MF=MANIFEST.MF
:BEGIN
CLS
ECHO Checking directory...
IF EXIST %INSTALL_DIR% (
GOTO Ask_Overwrite
) ELSE (
GOTO Install
)
:Ask_overwrite
SET OVERW=Y
SET /P OVERW="The program is already installed. Overwrite? ([Y]/N): "
IF %OVERW%==N GOTO Cancel
IF %OVERW%==n GOTO Cancel
IF %OVERW%==Y (
RD /S /Q %INSTALL_DIR% <--- Here was the error
GOTO Install
)
IF %OVERW%==y (
RD /S /Q %INSTALL_DIR% <--- Here was the error
GOTO Install
)
GOTO Ask_overwrite
:Install
MD %INSTALL_DIR%
XCOPY . %INSTALL_DIR% /E
CD /D %INSTALL_DIR%
MD %BIN_DIR%
ECHO Compiling...
javac -cp %CLASS_PATH% -sourcepath %SRC_DIR% %MAIN_CLASS% -d %BIN_DIR%
ECHO Creating JAR file...
jar cfm %JAR_NOM% %JAR_MF% -C %BIN_DIR% .
ECHO Succes! The application has been installed in %INSTALL_DIR%
GOTO CleanUp
:Abort
ECHO Abort! The application has not been installed.
GOTO CleanUp
:Cancel
ECHO Canceled by user. The application has not been installed.
GOTO END
:Cleanup
REM Code for clean up
GOTO END
:END
CD /D %INIT_DIR%
PAUSE
NOTE: The javac and jar commands are correct, at least work in my machine.
Well, the script works correctly when I test it with the INSTALL_DIR belonging to the same drive where I execute it, but if I use a target directory out of the drive where I'm executing, I have problems.
Executions without problems (called in a cmd.exe session):
C:\Users\TC\testInstall> install.bat .\..\installTarget
C:\Users\TC\testInstall> install.bat C:\Users\TC\installTarget
Execution with problems (called in a cmd.exe session):
C:\Users\TC\testInstall> install.bat D:\Documents\installTarget
The problems happen when I try to copy files specially, but also making and removing directories.
I hope someone can tell to me which options I must use in order to fix the problems.
Regards!
Well, I have two mistakes that I fixed and then the script work correctly.
The first was that I didn't use the /d option in cd command in order to change also the drive unit. It means, C:Users\TC> cd D:\Documents is wrong, the correct command is the following: C:Users\TC> cd /d D:\Documents as well as the MS-DOS manual page indicates.
The second error, it wasn't actually an error, I put rm -rf %INSTALL_DIR% (like in Linux) instead of the correct Windows command rd /s/q %INSTALL_DIR%
Now all the problems have been fixed and the script works properly :)
It has to do with batch files not accessing other drives than the C:\ drive by default. Even if you open up your command line, you shouldn't be able to CD D:\. Try this (not sure if this will work as I have never tried it)
PUSHD D:\
C:\Users\TC\testInstall> install.bat D:\Documents\installTarget
Or else, use PUSHD D:\ then move the install file to D:\ temporarily and install. Only solutions I can think of.

Finding the path of the program that will execute from the command line in Windows

Say I have a program X.EXE installed in folder c:\abcd\happy\ on the system. The folder is on the system path. Now suppose there is another program on the system that's also called X.EXE but is installed in folder c:\windows\.
Is it possible to quickly find out from the command line that if I type in X.EXE which of the two X.EXE's will get launched? (but without having to dir search or look at the process details in Task Manager).
Maybe some sort of in-built command, or some program out there that can do something like this? :
detect_program_path X.EXE
Use the where command. The first result in the list is the one that will execute.
C:\> where notepad
C:\Windows\System32\notepad.exe
C:\Windows\notepad.exe
According to this blog post, where.exe is included with Windows Server 2003 and later, so this should just work with Vista, Win 7, et al.
On Linux, the equivalent is the which command, e.g. which ssh.
As the thread mentioned in the comment, get-command in powershell can also work it out. For example, you can type get-command npm and the output is as below:
Here's a little cmd script you can copy-n-paste into a file named something like where.cmd:
#echo off
rem - search for the given file in the directories specified by the path, and display the first match
rem
rem The main ideas for this script were taken from Raymond Chen's blog:
rem
rem http://blogs.msdn.com/b/oldnewthing/archive/2005/01/20/357225.asp
rem
rem
rem - it'll be nice to at some point extend this so it won't stop on the first match. That'll
rem help diagnose situations with a conflict of some sort.
rem
setlocal
rem - search the current directory as well as those in the path
set PATHLIST=.;%PATH%
set EXTLIST=%PATHEXT%
if not "%EXTLIST%" == "" goto :extlist_ok
set EXTLIST=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
:extlist_ok
rem - first look for the file as given (not adding extensions)
for %%i in (%1) do if NOT "%%~$PATHLIST:i"=="" echo %%~$PATHLIST:i
rem - now look for the file adding extensions from the EXTLIST
for %%e in (%EXTLIST%) do #for %%i in (%1%%e) do if NOT "%%~$PATHLIST:i"=="" echo %%~$PATHLIST:i

Resources