I am trying to work from my thumbdrive, therefore i am setting the java path the java installion inside of my own thumbdrive. However, i am not always on a PC that is always a "G:\", sometimes it may be H or F depending on how many devices are currently connected.
How may i rewrite this bat so that it is more dynamic?
set Path=G:\dev\04 jdk\jdk1.6.0_29\bin;%Path%
".\dev\01 ide\eclipse-jee-indigo-SR1-win32\eclipse.exe"
pause
The drive the file was started from is in %~d0 (%0 being the files full start path).
If the drive was G:\ then after double clicking the file in explorer, or running C:\>G:\the.bat then %~d0 === G:
So you can;
set Path=%~d0\dev\04 jdk\jdk1.6.0_29\bin;%Path%
Related
I have the following problem: We send customers a USB-stick with content that they need to import onto a Linux-computer via a import-function (no influence on the Linux-part, only on the USB-stick part). The Linux import-function cannot read files with spaces, i.e. the file "system volume information" which is created on every drive of a Windows computer disrupts the import function.
With a google research I found out, how I can delete this file on the drive of the USB-Stick. If I do that before disconnecting the USB-stick from my Windows computer, I can import the content on the Linux computer. I do this with a batch-file containing the following command:
rd /s /q D:\"system volume information"
However, those USB-Sticks are sent out to customers that will want to view the content on a Windows-computer as well, before importing it, i.e. the file "system volume information" is created anew. I would like to make it as simple as possible for the customer and put a batch-file on the stick where they simply have to double-click on it in order to delete the file. This is where my problem lies: I cannot be sure, that it will be drive D: where the stick will lie. This is why I am looking for a function or command that reads the drive of the batch-file (USB-Stick) and does the command of delete "system volume information". I read some things about %~d0 but I could not find out how I specifically have to write the command to make it work.
Would you be able to help me? I have very limited experience with batches, so I apologize if this question seems very easy for you.
Thanks
Milak
I have not tested the code:
#echo off
set P=%~d0
set "P=%P%\system volume information"
rd /s /q "%P%"
I am writing a batch file on my Windows 8.1 machine. In one section of my batch file I need to start a command prompt in the "current working directory".
So far, this is what my batch file looks like:
#echo OFF
set WORKING=%cwd%
start cmd.exe /K pushd %WORKING%
exit
Let's say the batch file is located in the folder C:\Temp\Utilities. If I open an explorer window and double click the batch file to run it everything works great. A new command prompt is created in the directory C:\Temp\Utilities. However, if I right-click the batch file and select Run as administrator the working directory is no longer the location of the batch file, it's C:\Windows\System32.
Similarly, if I create a shortcut to the batch file in a different folder (for example. C:\Temp) and repeat the two steps above the results are the same. If I double click the shortcut and run it as a normal user the working directory is what I would expect. (Note, the working directory for the shortcut it's whatever is set for "Start in" of the shortcut properties, not the location of the batch file.) If I right click the shortcut and run it as administrator I again get a command prompt opened to the folder C:\Windows\System32.
I assume this is a "bug" or "feature" (if you want to call it that) in Windows 8.1 and it probably happens because execution environments for programs run as administrator are forced to run in the System32 folder? (I remember with Windows 7 this did not happen so it must be a new feature to Windows 8.)
I found one way to fix the issue and stop the command prompt from starting in C:\Windows\System32. I did this by modifying the following line in the batch file:
set WORKING=%~dp0
Doing it this way sets the working directory to the location of the batch file. With this change, no matter how I run the batch file or the shortcut (administrator or normal) the working directory ends up being the same, C:\Temp\Utilities.
The problem with this solution is I don't want the working directory to always be the location of the batch file. If the batch file is run directly then it's okay but if I run it from a shortcut I need the working directory to be whatever is set in the "Start in" property of that shortcut. For example, if the batch file is located in the folder D:\Temp\Utilities this is what I need to happen regardless of whether I run as administrator or not:
Shortcut Location Start In Property Command Prompt Working Directory
-------------------- ------------------- ------------------------------------------
C:\Temp <undefined> D:\Temp\Utilities
C:\Data\bin C:\Data\bin C:\Data\bin
C:\Data\bin D:\Temp\Utilities D:\Temp\Utilities
What this means is I can't always use %~dp0 to set the working directory in my batch file. What I need is some way for the batch file to know if it was run either directly or by a shortcut. If the batch file is run directly then the working directory is easy to get, it's just the value of %cwd%. If the batch file is run by using a shortcut, I don't know how to get the "Start in" property inside the batch file.
Does anyone know how I can do these two things inside my batch file:
1. Check whether it was run directly or by a shortcut.
2. If run by a shortcut, get the "Start in" property of the shortcut that started it.
Thank you,
Orangu
UPDATE
I found sort-of a "hackish" way to fix the issue. For the shortcut I edited the "Target" field and changed it to the following:
cmd.exe /k pushd "C:\Temp" && "D:\Temp\Utilities\batchfile.bat"
Now the working directory can be obtained by calling %CD% in the batch file and this works for both administrator and normal users. It does not, however, work for the case when I run the batch file directly. I still need to use %~dp0 in that case.
I don't like this solution, however, because it requires me to manually change all shortcuts I make and it also makes the icon look like a cmd prompt icon rather than a batch file.
Have you already considered to not use shortcuts at all?
You could e.g. create a batchfile_exec.bat containing your call
REM optionally do
REM cd /D working_directory
REM if you want to force a special working directory
D:\Temp\Utilities\batchfile.bat
and replace all the shortcuts with batchfile_exec.bat. If you double-click batchfile_exec.bat, the working directory will be the one containing batchfile_exec.bat.
I personally don't like Windows shortcuts that much, because they are hard to handle within a revision control system. As you also noticed, it is very time consuming if you want to modify a lot of them.
By the way: If batchfile.bat was designed/written to be always run from the direcory where it is located, you might also consider to modify batchfile.bat to force that behaviour:
setlocal
cd /D %0\..
REM your original content
endlocal
In %0 the path to the batchfile is stored.
The trick is to assume that %0 is a directory and then to change one level lower based on that directory. With /D also the drive letter is changed correctly.
The cd command doesn't care if %0 is really a directory. In fact %d doesn't even have to exist (%0\dummy\..\.. would also work).
The setlocal command is to have the working directory being restored when batchfile.bat has finished (this would be good if batchfile.bat was called form another batch file).
I noticed that the endlocal command is not really necessary in this context since it is applied implicitly when batchfile.bat finishes.
I am putting Kingsoft office on my flash drive, and I want to use a batch file to start the applications because the paths are not easily accessible, I cannon create a .lnk file because the path varies by computer because it may be plugged into a different port. Here is my batch file code, could somebody give some suggestions on how to make this work. Thanks in advance...
set "path=%~dp0"
start %path%office6\wpp.exe
The second line is the problem, the program won't start the program. Thanks!
cd /d "%~dp0"
start "" /b wpp.exe
I think some of the directory names in %path% contain spaces and since %path% is not enclosed within ""( double quotes), the script is unable to find the exe .
You may also want to include a log file so that it becomes easier to debug in case of any errors.
Try this:
set baseFolder=%~dp0
start "%baseFolder%office6\wpp.exe" > "%baseFolder%batchRunLog.log"
MachineX is calling a batch file through UNC path to either MachineA or MachineB, depending on server failover status:
If everything is good, the batch is called via path \\MachineA\files\Batch1.bat.
If there is a failover, MachineX knows to call the batch via path \\MachineB\files\Batch1.bat.
Without using an additional parameter, or tapping into how MachineX knows which machine to call, I need to know within the batch which machine it's running on, so it can access other files on the machine. To do this, I want to store the machine name in a variable.
Because the executing machine is MachineX, I can't use a hostname variable to pull the batch, but since the batch is always called with a fully-qualified UNC path, argument %0 will have the machine name in the path. Since the system is part of a mirroring setup, the batch files are always kept identical, so hard-coding is not possible.
What is the simplest method of getting the machine name from within the batch file, without having to go external? Thanks in advance.
EDIT:
Alright, I figured it out:
for /f "tokens=1,2,3,4,5 delims=\" %%a in ('echo %~p0') do set svrpth=%%a
This works fine so long as there are no more than 5 folders in the path trail, which for this purpose is acceptable.
Thanks for all your help.
if path is the same on every machine it looks as you can simply remove that from %0 variable (path to called script) - what is left will be machine name
My husband and I use Quicken on two separate computers on the same home network. We have two installations of the application, but only one data file that resides on my husband's machine.
Quicken doesn't work well when I try to open the data file across the network from my machine. It runs very slow and has unexplainable errors.
So, instead, I need a batch file to copy the data file to my machine so I can use it, then put it back when I'm done.
Any suggestions where to start?
I do something very similar.
REM Networked Drive isnt always recognized from DOS unless its mapped in explorer first
explorer /n,q:\
call wait 5
copy "q:\Quicken.QDF" C:\
REM Backup the Quicken copy from your Q drive
move "q:\Quicken.QDF" "Q:\QuickenBackup%date:~4,2%%date:~7,2%%date:~10,4%.QDF"
REM Now run Quicken
"C:\Quicken.QDF"
REM After youre done with the data file, put it back
copy "C:\Quicken.QDF" Q:\Temp.QDF
IF ERRORLEVEL 0 GOTO NOPROBLEM
ECHO A Problem with "COPY" Has Occurred
ECHO Please Correct It and Try Again
GOTO END
:NOPROBLEM
move Q:\Temp.QDF "q:\Quicken.QDF"
move "C:\Quicken.QDF" "C:\QuickenBackup%date:~4,2%%date:~7,2%%date:~10,4%.QDF"
:END
pause