Get Server name of batch file from within batch file - batch-file

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

Related

Setting system variables using bat file

I wanna write a bat file to set system variables on any windows system. My software which I made needs to set a path instead of asking the user to process the method I heard we can do this task using bat files so I tried multiple ways to set a path nut most of them are duplicates of all existing paths instead of adding new ones and some times it's removing all paths and keeping only the new path
setx Path "%Path%;c:\ffmpeg"
which is duplicating all the existing path variable
I did read multiple Stackoverflow queries on this but none helped
if ur testing on your system to check
please save/make a copy of all ur paths first
Thank you
Eswar
i wouldn't recommend you to mess with system variables but if you want to store path for your program here's a script that will do the thing but before doing so i would encourage you to make make a backup for files and all the paths stored in system variable %Path% and if something happen read this but anyway here is a script that worked for me:
#echo off
echo validating Path
ping localhost -n 2 >nul
set "store_path=Path to your program "
rem /M to set the variable in system wide environment
setx Path "%path%;%store_path%"
setx Path "%path%;%store_path%" /M
simply i am storing the new path with the old one.
I don't suggest you mess with system variables but if you want to store path from cmd use
setx Path "%Path%;C:\your\path\here\" C:\ffmpeg\bin
I tested on windows 10 it worked for me
Hopefully it works for u too!!

read from a .txt file in BATCH, without using literal path

Basically, i would like to use the type command, but I can't provide the actual path.
Currently my attempt was
type "./TESTS/Test1.txt"
but I'm assuming that since it's a relative path, it can't work.
I've run into the same issue with copy and xcopy.
I have been unable to solve this issue or find anything online.
Is there way to do this?
EDIT:
To clarify, I am trying to get my .bat file, to read the contents of a .txt file located in a subfolder (meaning the subfolder and the .bat file are in the same folder), and print it to the console.
Since you've now edited your question but seemingly not provided feedback on my earlier comment, here it is as an answer.
Windows and it's command interpreter, cmd.exe uses the backslash \ as its path separator.
Although many commands seem to accept the forward slash interchangeably, Type isn't one of those.
Additionally .\ is relative only to the current working directory, and in cmd.exe is unnecessary, though valid.
The following should therefore work as you intended:
Type TESTS\Test1.txt
Type "TESTS\Test1.txt"
Type .\TESTS\Test1.txt
Type ".\TESTS\Test1.txt"
If the location you are using is being received in the batch file with the forward slashes, you could set it to a variable, then expand that variable substituting the forward slashes for backward slashes:
Set "Variable=./TESTS/Test1.txt"
Type "%Variable:/=\%"
It may be necessary, depending upon the code we cannot see, to navigate to the batch file directory first, since it may not necessarily be the current working directory at the time of the invokation of those commands.
To do that use:
CD /D "%~dp0"
%~dp0 provides the folder, where your batchfile resides (including the last \) (does of course only work inside a batch file). So:
type "%~dp0Test\test1.txt"
is exactly what you want: <folder_where_my_batchfile_is\><subfolder_Test>\<File_test1.txt>
independent of your "working folder" (where the batchfile might have cd or pushd to).
Wouldn't it basically work by using %CD%? Like, TYPE "%CD%/Subfolder/Test1.txt"? %CD% is the windows variable for "Current Directory" and should be set to whatever directory the batch file is working in and since you're trying to access a folder within the same directory this should work. You're question is quite unclear, however, and I hope I'm not misinterpreting.

Calling batch file reports: "The system cannot find the path specified"

I have one batch file that suppose to call another. I read that call command is used in this case. Although error message appears:
The system cannot find the path specified.
The path is not wrong 100% sure. This is the caller run.bat
#echo off
call xslt\projects\asp-bus\implementation\batch\ant-start.bat
pause
and this is the called ant-start.bat
set ant="../../../../infrastucture/apache-ant-1.10.0/bin/ant.bat"
call %ant%
pause
In each batch file, the paths are relative to the working folder you are running the batch file from, not the folder that contains the batch file itself. You either need to use absolute paths (e.g. starting with C:\), or to make sure that when each batch file is run from a working folder where the relative paths make sense.
If you're launching run.bat from a Windows shortcut, you can set the "Start In" folder from the shortcut's Properties dialog. When ant-start.bat is called, it will run from the same folder as run.bat.
To fix the problem, you might need to change ant-start.bat to
set ant="infrastucture/apache-ant-1.10.0/bin/ant.bat"
call %ant%
pause
Alternatively, you could put a cd command in one of the batch files, to force it to use an appropriate working folder.
Bear in mind that if you set the %ant% variable to a relative path as above, using the variable will only work from a folder where that relative path makes sense.

Could someone help to understand this batch script?

I'm reading a batch file, but I do not understand it, can someone help to explain?
As I understand %0 is is the name of batch file, can we iterate on it? or is it a convenient way to represent a folder?
I can not find the variable %BatchPath% in the file, where do you think it's defined?
And it seems APATH is defined in the two loops?
for %%x in (%0) do set APATH=%%~dpsx
for %%x in (%BatchPath%) do set APATH=%%~dpsx
pushd %APATH%
You can iterate over a single value. It just means the set statement is executed once. The ~dps then strips the file name, so that only the directory remains.
The first line performs this action on %0, indeed the path and name of the current script.
The second line performs the same action on a given variable, Now that is the fun part, because if %BatchPath% is empty, nothing gets iterated, so the set statement on that line is not executed at all.
So effectively, it stores a directory, which is the directory of the script by default, but can be overridden by explicitly assigning a path to %BatchPath% before calling this script.
pushd allows you to save a directory, so you can return to it later using popd. It allows the script to jump to another directory an be able to restore the shell to the original directory before it terminates.
%0 is the current batch file.
%%~dpsx gives the current batch file's
short path here its giving the Drive name for eg "D:\"
Pushd Stores the name of the current directory for use by the popd command before changing the current directory to the specified
directory.
APATH is some variable used to store the path.
so basically the script is fetching details about the script file name , its drive location and storing it to be used as location from which last batch file ran or something like that.

use batch file to call another batch file from different comp

Is it possible to use batch file (for XP) to call another batch file from different computer? if yes, how identify the target computer? Is it by IP comp ID?
You can do this using psexec from Sysinternals/Microsoft.
The computer name can be specified either by name (assuming name resolution works on the network) or IP address.
Why not use UNC naming? As in
ThisComputer.bat
#echo on
dir *.whatever
call \\ServerName\C$\YourFolder\OtherComputer.bat

Resources