I want to be able to check whether or not Tomcat is currently already running as a service. The problem is that I'm not sure what the name of the service will be.
I plan to look in another batch file from where Tomcat is started.
In that file there is the line:
#echo off
rem START or STOP Tomcat
....
sc start "TomcatService5"
Is there an easy way to read that file and parse out the service name argument passed into the command "sc start"?
The following code searches for the line sc start in the file start.cmd and gets the 3rd token from that line:
for /F "tokens=3" %%a in ('findstr /L "sc start" start.cmd') do echo %%a
Related
This is My first post in here
I need a help for your side I have made a batch file for run autocad exe and load a script file but give error when I run the batch file
#echo off
set KEY_NAME=HKCU\Software\Laxman Enterprises\Xpresslisp Tools
set VALUE_NAME=installpath
set FN=loadload
set FE=scr
FOR /F "tokens=2*" %%A IN ('REG.exe query "%KEY_NAME%" /v "%VALUE_NAME%"') DO (set pInstallDir=%%B)
set approot=%pInstallDir:~0,-1%
echo %approot%\%FN%.%FE%
"C:\Program Files (x86)\AutoCAD 2002\acad.exe" /b %approot%\%FN%.%FE%
pause
Error: while running batch file autocad opens and in commandline the script file not loading "Xpresslisp.scr": Can't find file."
and bellow one is working
script file loading without getting error
#echo off
set path=%USERPROFILE%
set fol=Documents
set NAME=1
set SUFFIX=scr
"C:\Program Files (x86)\AutoCAD 2002\acad.exe" /b %path%\%fol%\%NAME%.%SUFFIX%
pause
Regarding your second question in the comments...
Bellow command will create the text file and write the first line to it e.g. "some text" like in the command below.
Echo some text > full_path_to_txt_file
Command below will append new text to same file.
Echo some text >> full_path_to_txt_file
'>' char creates file and writes firs line
'>>' char append text
check that %path%\%fol%\%NAME%.%SUFFIX% returns the Full Path to the "Xpresslisp.scr" file !
if it does, inspect the Full Path and see if it contains any white spaces.
if it does, enclose the %path%\%fol%\%NAME%.%SUFFIX% with apostrophes
"%path%\%fol%\%NAME%.%SUFFIX%"
It may be something as simple as blindly removing the last character of the installpath without knowing for sure what it is, (doublequote or backslash?).
As there is unlikely to be multiple copies of any filename in the Xpresslisp Tools tree, I would suggest something like this:
#Echo Off
Set "KEY_NAME=HKCU\Software\Laxman Enterprises\Xpresslisp Tools"
Set "VALUE_NAME=installpath"
Set "FN=loadload"
Set "FE=scr"
(Echo=FILEDIA 0
Echo=(LOAD "C:\\loadmyfile.lsp"^)
Echo=FILEDIA 1)>%FN%.%FE%
For /F "Tokens=2*" %%A In ('Reg Query "%KEY_NAME%" /v "%VALUE_NAME%"') Do (
For /F "Delims=" %%C In ('Dir/B/S/A-D "%%~B"\"%FN%.%FE%" 2^>Nul') Do (
Start "" "%ProgramFiles(x86)%\AutoCAD 2002\acad.exe" /b "%%~C"))
This doesn't care if there is a trailing backslash or not and will only run the AutoCAD command if the file is there.
I have two parameters that are passed bye a service monitoring system. They are the Service Display Name and Machine name. I found the SC command can retrieve the service name via the GetKeyName. I have searched this site and others for a week and I'm stuck with how to extract output from that data to convert to a Service Start Command when the system is notified that a service is down.
I am needing to create the batch/script so that is can receive the Service Display Name as a parameter, determine the Service Name then issue the start command. This seems simple enough but when looking at the FOR command and taking into account the unknown number of tokens possible when looking at the FOR option because some service display names have multiple space breaks.
I'm looking for a solution that crosses multiple Window 2008/2003 systems without having to code for each specific set of services on that server or building a script that searcher all services when just one is stopper because there are services that are stopped for a reason and should not be restarted. This is why we have a central monitoring system that is using the display name/server name.
Thank you,
#echo off
setlocal enableextensions enabledelayedexpansion
rem Assume that everything will be fine
set "_exitCode=0"
rem Retrieve the service display name from parameters to batch file
rem If it contains spaces, "quote the service name"
set "_serviceDN=%~1"
rem Check parameters
if "%_serviceDN%"=="" (
echo ERROR: Usage is %~n0 "service display name"
set "_exitCode=1"
goto endProcess
)
rem Retrieve service name
set "_serviceName="
for /F "tokens=1,* delims== " %%a in (' sc GetKeyName "%_serviceDN%" ^| find "=" ') do (
set "_serviceName=%%b"
)
rem Check if we have retrieved the service name.
if "%_serviceName%"=="" (
echo ERROR: Service "%_serviceDN%" not found
set "_exitCode=2"
goto endProcess
)
rem Get service state
set "_serviceState="
for /f "skip=2 tokens=3 delims=: " %%f in ('sc query "%_serviceName%" ^| find ":" ') do (
if not defined _serviceState set "_serviceState=%%f"
)
rem Check if service is stopped and if necessary, start it
if "%_serviceState%"=="STOPPED" (
sc start "%_serviceName%" > nul 2>nul
set "_exitCode=!errorlevel!"
)
:endProcess
rem Clean and return exit code
endlocal & exit /b %_exitCode%
sc //machinename query | findstr DISPLAY_NAME | findstr service.name
Run from PowerShell
I am trying to call x.cmd from many servers in a list and basically x.cmd will return a log file so I will copy the output log files into a new folder.
the structure in that list is:
server name&folder path of the x.cmd on thet server&output folder name that I will create
when I run the script below, it doesn't return any error and return value is 0. but x.cmd just doesn't do anything when I check on the actual server. x.cmd working fine once I run it manually.
Please if possible point me out the mistake of my script. my other concern is the folder path being too long(but it is within the limits of Microsoft if I am right). :) Thanks for your time.
#echo on
cd /d %~dp0
setlocal EnableDelayedExpansion
set serverList=List.txt
for /f "usebackq tokens=1,2,3* delims=&" %%A in ("%serverList%") DO (
set remoteCmd=x.cmd
wmic /node:%%A process call create "%%B\remoteCmd"
pause
robocopy %%B\logs Output\%%C /e
)
endlocal
Pause
JS
The problem seems to be that you're not actually calling x.cmd on the remote servers, since the variable isn't substituted in the wmic call. Move the definition of remoteCmd out of the loop and enclose remoteCmd in the wmic call in percent signs:
set serverList=List.txt
set remoteCmd=x.cmd
for /f "usebackq tokens=1,2,3* delims=&" %%A in ("%serverList%") DO (
wmic /node:%%A process call create "%%B\%remoteCmd%"
pause
robocopy %%B\logs Output\%%C /e
)
Update 1: Since you're getting a response on the shell with a processID and Error level=0, it's fairly safe to say that x.cmd was in fact started on the remote server. The fact that the logfile does not turn up in the same directory where x.cmd resides in, is very probably because the logfile is not given an absolute path in x.cmd. That means it will be created in the directory on the remote machine where the cmd.exe is located, which is executing x.cmd. In most systems this will be %WINDIR%\system32, i.e. c:\windows\system32.
Update 2: If modifying x.cmd is no option, then you need to change the path to it's directory before calling it. You can do it like this:
wmic /node:%%A process call create "cmd /c cd /d %%B & x.cmd"
Note: The cd /d allows you to change directories even across different drives.
Update 3: Since there are spaces in the directory names you'll need to enclose them with quotes - which will have to be escaped, since there already outer quotes enclosing the whole remote command. So that means:
wmic /node:%%A process call create "cmd /c \"cd /d %%B\" & x.cmd"
Note: A possible alternative to the inner quotes would be to use 8.3-filenames of the involved directories. You can view those with dir /x on the command line.
So I've been trying to create a batch file for a piece of software called DiscEX the software requires command line use from cmd.exe windows xp or higher the way it's initiated is like this discex (any arguments needed) location of iso file.
Now I can get the software to run using the batch file but I can't seem to figure out how to copy the target location of a file that was dragged onto it to open the batch file up
Here is what the batch file in notepad looks like.
#echo off
echo Welcome to AutoDiscEx
pause
C:\windows\system32\discex
pause
also I need to be able to start in the working directory of a portable hard drive.
All you need to do is
C:\windows\system32\discex "%1"
to get a file path argument passed into the batch
If the batch file is in the working directory already, put
cd /d %~dp0 in the batch after #echo off
If you want to determine what drive is the external usb drive, use
#echo off
setlocal
set wmi='wmic logicaldisk where "volumeserialnumber='32A78F3B'" get caption'
for /f "skip=1 delims=" %%A in (%wmi%) do (
for /f "tokens=1 delims=:" %%B in ("%%A") do (set drive=%%B)
)
echo %drive%
where volumeserialnumber is the output from vol [driveletter of USB drive:] with the - removed.
When you drag a file on a batch file, the full file path is available in the first argument (%1) of the batch file. If you need this argument to be fed to the discex application as its first argument, you can do:
#echo off
echo Welcome to AutoDiscEx
pause
C:\windows\system32\discex %1
pause
Im using the sc command to query the services running on machines and was using code similar to:
sc \\"server_name" query | find "SERVICE_NAME" > servicelist2.txt
so basically all the server names are listed in this one notepad file. there are 100+ of them. it is too hard to manually put each name in and write 100+ lines of code. is there a way i can use batch itself to direct to the notepad file and iterate through all the server names written in it and then put them back into the "sc" command?
EDIT: Am on windows btw if it wasnt obvious. Using batch scripts for the above.
Assuming you have a file named servers.list in the same directory, you can use this:
for /f %%a in (%~d0%~p0servers.list) do (
ECHO %%a > servicelist2.txt
sc \\%%a query | find "SERVICE_NAME" > servicelist2.txt
)
you can do this with for /f :
OR /F "usebackq tokens=*" %%G IN ("C:\My Documents\servers.txt") DO (
sc \\%%G query | find "SERVICE_NAME" > servicelist2.txt
)
#echo off
echo Starting to list query services...
for /f %%i in (servieslist2.txt) do sc query %%i > output.txt
echo Finished query services...
The above will loop through each of the 100 service names specified in serviceslist2.txt and output it to a file called output.txt in the same directory as the batch file