Problems working with different drive units in a batch file - windows

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.

Related

Change Directory of Batch File to the batch file's location

Weekly, I have to update our computers with publications for our aircraft. I have streamlined the process by creating a batch file that deletes the old publications automatically and opens the folder where they are stored so I don't have to dig for it. The location where the publications are stored each week is the same. My batch file looks like below...
del /s /f "C:\Users\Public\Desktop\PubFolder\Pub.pdf"
So basically all I have to do is copy the PubFolder from my USB drive to the Public Desktop after I have ran the batch file to delete all of the publications.
If all of our computers were exactly the same, this would be an easy fix because I could write in the batch file
xcopy /y D:\PubFolder" "C:\Users\Public\Desktop\PubFolder"
and the script would do all of the work for me. My issue is, a lot of the computers have different software installed on them which require partitions in the HDD. Some of them have 3, some 2, some 1.
Basically what I'm needing is when I run the batch file from the USB, it uses the location of the batch file as the directory so I can copy from there.
A cmd script (.bat file) can reference its own directory with %~dp0
See the details using FOR /?
While you are at it, some additional error checking might make some situations easier to recover from.
SETLOCAL
SET "EXITCODE=0"
SET "USERDIR=%PUBLIC%\Desktop\PubFolder"
IF NOT EXIST "%USERDIR%" (
ECHO ERROR: The user publication directory "%USERDIR%" does not exist.
SET EXITCODE=4
GOTO TheEnd
)
IF EXIST "%USERDIR%\Pub.pdf" (DEL "%USERDIR%\Pub.pdf")
xcopy /y %~dp0PubFolder" "%USERDIR%"
SET "EXITCODE=%ERRORLEVEL%"
IF %EXITCODE% NEQ 0 (
ECHO ERROR: Failed to copy from "%~dp0" to "%USERDIR%"
SET EXITCODE=5
GOTO TheEnd
)
:TheEnd
EXIT /B %EXITCODE%
#setlocal
REM If you have pre-Vista machines, uncomment the folowing line:
REM #if not defined PUBLIC set PUBLIC=%SYSTEMDRIVE%\Users\Public%
#xcopy /y %~d0\PubFolder %PUBLIC%\Desktop\PubFolder

How do I make a batch file delete it's own directory?

Okay, I apologize that I am very new at this, but I am trying to make my batch file delete it's own directory after it has been launched. This is how my folders are arranged:
Folder1
delete.bat
My goal is to make "delete.bat" delete "Folder1" after "delete.bat" has been launched. So here's my code:
rd /s /q %~dp0..\Folder1
This seems like it would work but it only deletes the contents of "Folder1" rather than the whole directory itself. What am I doing wrong?
Some thoughts...
%~dp0 gets the drive and path of the batch file, so you don't need to include ..\Folder1.
What you have should work. If it's not removing the folder itself, it means that it's locked, probably because cmd's current folder is Folder1. (That's a likely guess, but it's not the only reason it might be locked.) If it is cmd, you'll have to call the batch file from another folder, outside of Folder1.
While what you have will work, it will result in a funny error when resuming the non-existent batch file: The system cannot find the path specified. You can avoid that in the solution below.
One good solution: start /b "" cmd /c rd /s /q "%~dp0"
This creates a new process to remove the folder (and everything in it, including the batch file itself). Be careful. =)
From the corresponding MSDN link for rd:
You cannot use rmdir to delete the current directory. You must first change to a different directory (not a subdirectory of the current directory) and then use rmdir with a path.
I guess this is what's going wrong in your case since the batch file is located within the directory that you're trying to delete.
My implementation is effectively the same as Soja's, plus the info from dbenham's comment. I have added a 2-sec delay, to ensure there are no timing issues, even though I believe the error when the .bat file is deleting itself is harmless.
#echo off
:: Do the work
...your command here...
:: In order to delete the current dir we are running from (and all subdirs), none of them can be the
:: current working directory of any running process. Therefore, we are setting our own CWD to something
:: else, so it will be inherited by the below cmd.exe.
cd /d %temp%
:: The countdown is there to allow this batch file to exit, so it can then be deleted safely.
set DelayedSelfDeleteCommand="timeout /t 2 >NUL && rmdir /s /q "%~dp0""
:: Start a separate process (without waiting for it), to execute the command
start "" /b cmd.exe /C %DelayedSelfDeleteCommand%
Well I think it cannot be done (at least as normal user)
start /b "" cmd /c rd /s "%~dp0"
deletes folder but only with right permissions I think
start /b "" cmd /c rmdir /s "C:/folder"
This has the same result
del /s /q "C:\Temp\folder\*"
rmdir /s /q "C:\Temp\folder"
del %0
only way as for batch files is to use vbs script or autohotkey (send !{Space} // send e // send p formula) or hack it, you can delete only file used and content of folder but not working directory due to specification of cmd. Any other language would have no problem with it cuz it in RAM memory.
I recommend this way to do it (as for normal users):
in your bat file add
copy C:\urpath\deleteafter.bat C:\Temp\deleteafter.bat
start "" autohotkey.exe "X:\patchto\deletebatchfile.ahk"
deletebatchfile.ahk
sleep 2000
Run, C:\Temp\deleteafter.bat, C:\Temp\
deleteafter.bat
rmdir /s /q "C:\Temp\batfileworkingpath"
sleep 3
del %0

“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).

How place file with bat command when ran from currenty directory

So I am trying to create a bat/exe installer for regjump. I am using a bat compiler so I can attach the "regjump.exe" to the installer.exe; so when the installer is ran, it will put regjump in the current directory of the installer.exe. I know I can use xcopy to place the file but I need help with the first half of the command. What is "local directory\regjump.exe" syntax?
cd C:\
xcopy "WHEREVER THE INSTALLER IS RAN FROM\regjump.exe" "C:\Windows\System32" /q /y /r
I want the syntax to be able to be ran from anywhere i.e. removable F:
Try this. You will need to execute as admin to write to "C:\Windows\System32", so this checks for that.
openfiles >nul 2>&1 || echo(You must execute this command as Administrator. & pause & goto :eof
set "SourceDir=%CD%
cd C:\
xcopy "%SourceDir%\regjump.exe" "C:\Windows\System32" /q /y /r

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

Resources