use batch file to call another batch file from different comp - batch-file

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

Related

How to copy from a constantly changing path in a batch file?

How would I get a batch file to copy from a certain directory that contains a file with a name that would be different every time the batch file is run.
Ex.:
copy "C:\Users\%USER%\AppData\Local\Microsoft\Windows\INetCookies"
%USER% is the changing file name.
%username%
is the environment variable for the current user's username. and
%userprofile%
is the path to their profile.
In addition to the already provided answer, you can get even further down the path using another of the built-in environment variables, %LOCALAPPDATA%.
"%LocalAppData%\Microsoft\Windows\INetCookies"
Type SET followed by [Enter] into a cmd.exe window to see all of the machine's variables listed.

Backslash in the directory path, what is the difference?

I have a batch script that checks to see if a directory exists then maps it, if it does exist.
if exist \\server\folder1\%username% net use g: \\server\folder1\%username%
Lately the script hasn't been mapping correctly with some computers, on those computers in needs to be change to
if exist \\server\folder1\%username%\ net use g: \\server\folder1\%username%
Why would that backslash make a difference? Isn't it pointing to the same directory? Why would "if exist" need it and "net use" not need it? Users do NOT have access to folder1.
Now, I came across an older version of the same file written by a previous employee and he wrote it as
net use g: \\\\server\folder1\%USERNAME%
Why would he put four slashes?
I'm not sure if this is your problem, but if exist \\server\folder1\%username% is TRUE if %username% is a valid file or folder within folder1.
Adding a backslash at the end forces the condition to only be true if a folder exists.

Get Server name of batch file from within 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

Make a batch that detects its own filepath

I need to make a batch file that detects what drive and directory it is in. When I run the the file normally, it is in the correct directory/drive already. But when it is run as admin, it starts in system32. Is there a command that goes to the directory or drive the batch came from?
You could use
Pushd "%~dp0"
This changes the current directory to the path of the batch file.
Quoting the argument makes it safe against special characters in the pathname like "C:\Documents & Settings"
well a workaround is to use \ before the path to give absolute path.
So, if you need to run a file c:\temp\xyz.exe and even if you are in directory c:\winodws\system32 still when you do cd \temp\xyz.exe still the file will run properly

Windows BAT how to substitute drive letter?

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%

Resources