how to record results of command in batch file

Hey EE!  I'm trying to run a script that will stop NFS services, copy over two files from a network share to the server's local directory and then start the NFS services.  All of that sounds really good but as usual, my novice skills are preventing me from being able to complete it.

Here is what I have so far:

@echo off
setlocal enabledelayedexpansion
set LogFile=C:\Tools\reload-nfs.log
if not exist "%LogFile%" goto SkipRename
for %%f in ("%LogFile%") do (
	for /f "tokens=1-3 delims=/ " %%a in ("%%~tf") do (set TimeStamp=%%c%%a%%b)
	set NewName=%%~nf.old.!TimeStamp!%%~xf
	echo Renaming "%LogFile%" to "!NewName!" ...
	ren "%LogFile%" "!NewName!"
	ECHO File date: %%~tf
)

:SkipRename

Open in new window


This works just fine as does this next part of the script:
for /F "tokens=3 delims=: " %%H in ('sc query "Gateway for NFS" ^| findstr "        STATE"') do (
  if /I "%%H" EQU "Gateway for NFS" (
   net stop "nfsSvc"
  )
)

Open in new window


But when the script runs, I would like to know if the command was executed successfully.  I've tried a couple of different things but it didn't work.  I appreciate your help and will post follow-up questions about this script after this one is closed.

Thank you!
LVL 9
samiam41Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gerwin Jansen, EE MVETopic Advisor Commented:
After a command like:

net stop "nfsSvc"

you can check %ERRORLEVEL% - if that's zero (0) then the previous command was executed successfully, if not, you can echo an error.
0
samiam41Author Commented:
thanks gerwinjansen.  What would that look like in scripting language?
0
point_pleasantCommented:
how about set echo on and echo errorlevel
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

samiam41Author Commented:
@point_pleasant, I like it but I don't know how to code that into the script.  Can you provide?
0
Bill PrewCommented:
When you say "I would like to know if the command was executed successfully.", what command are you actually speaking of?  Just the NET command, or the whole script, or a part of it?

You can send the output of any command, like NET, to a file with redirection, like"

net ntop "nfsSvc" >mylog.txt 2>&1

Or if you just want a message of some kind based on success or failure you can check the ERRORVALUE after the NET command and handle any exceptions.

Tell us a little more about what you want to monitor, and exactly what type of notice or log info you want and we can give you more details.

~bp
0
Gerwin Jansen, EE MVETopic Advisor Commented:
if not %ERRORLEVEL% equ 0 echo Something went wrong
0
samiam41Author Commented:
@billprew, thank you for responding.  My apologies for not phrasing that question correctly.

I'm trying to find out if the command to stop the service was completed properly.  I want the results of the command to be record in the %logfile% identified at the beginning of the script.
0
point_pleasantCommented:
ECHO ERRORLEVEL = %ERRORLEV%


you could further process specific error with

IF ERRORLEVEL 255 GOTO Label255
IF ERRORLEVEL 254 GOTO Label254
  •
  •
  •
IF ERRORLEVEL   2 GOTO Label2
IF ERRORLEVEL   1 GOTO Label1
0
samiam41Author Commented:
for /F "tokens=3 delims=: " %%H in ('sc query "Gateway for NFS" ^| findstr "        STATE"') do (
  if /I "%%H" EQU "Gateway for NFS" (
   net stop "nfsSvc"
  )
)

ECHO ERRORLEVEL = %ERRORLEV%

My apologies.  I didn't state that I am awful at coding.  :)  I don't know where to put that line you provided in my script.
0
Bill PrewCommented:
I would probably do this:

net ntop "nfsSvc" && (
  REM Do any process here for success of the command
  ECHO NET command succeeded>>"%logfile%
) || (
  REM Do any processing here for failure of the command
  ECHO NET command failed>>"%logfile%
)

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewCommented:
These are described here:

http://www.robvanderwoude.com/condexec.php

~bp
0
samiam41Author Commented:
@billprew, checking it out now.
0
samiam41Author Commented:
With a minor correction to your script (first line, second word:  was- ntop s/b- stop), I am using your code for my script.  Thank you for your help and it was great to work with you again.

I appreciate everyone's time and assistance with this question.
0
samiam41Author Commented:
0
Bill PrewCommented:
Welcome, glad that helped, thanks for the feedback.

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.