MS-DOS FTP command in a batch file: raise error - windows

In MS-DOS (Windows 2003 R2 Server), I have a batchfile which has the FTP command in it, eg:-
FTP.CMD
-------
cd d:\extracts\scripts
ftp -i -s:ftp_getfile.ftp
exit
I would like the batch file to raise and return an error level 1 for failure instead of 0,
so that the calling batchfile can deal with it.
The error could be caused by the FTP server being down. Right now, nothing is returned to indicate
an error condition occured.
Please can someone advise?
Thanks! :)

Maybe too late, but it is possible. I'm running the following script to check for errors in the text that's returned by the FTP script. If you know the error text that's returned by FTP, then that's what you look for with the 'find' command.
The ftp commands are in a file called ftp.inp, just check out the help of FTP on how to use '-s'.
ftp -s:ftp.inp > ftp.log
find /I /C "not connected" ftp.log
IF NOT ERRORLEVEL 1 GOTO FTPERROR
find /I /C "not found" ftp.log
IF NOT ERRORLEVEL 1 GOTO FTPERROR
find /I /C "failed" ftp.log
IF NOT ERRORLEVEL 1 GOTO FTPERROR
REM --- no errors found
GOTO :END
:FTPERROR
REM --- error found
:END

As per this question:
How to capture the ftp error code in batch scripts?
The windows FTP command doesn't support this behaviour (or PASV mode) and is basically next to useless.
You might want to try NcFtp instead. It's free, small, portable, and has decent error codes.

Related

How to Pause Executable if an Error is Encountered?

I googled for a solution for how to pause and executable is an error is encountered. The most promising thing I found was this:
#echo off
"C:\Users\ryans\dist\AS_to_GBQ.exe" %1
if %errorlevel% neq 0 exit /b %errorlevel%
I put that in my Command Prompt, and run it. Now, the exe opens, runs, throws an error, and the exe shuts down with no indication of what actually happened. How can I see the details of the error, or somehow troubleshoot this issue? I'm on Windows 10. I can run this as a batch file, or through PowerShell, if that helps.

Batch to check if process exists

I'd like a batch that will check if the process firefox.exe exists (after it has been started by the start command).
If the process exists, it will go to the label :fullscreen,
else the batch will go the the label :timeout. Then, it will check again if the process firefox.exe exists and if not, it will go again to the label :fullscreen until the process exists.
Here is my batch:
#echo off
start "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
:timeout
timeout /t 5
:fullscreen
nircmd sendkeypress F11
exit
How can I do this check ?
You can also use QUERY PROCESS:
#Echo Off
If Not Exist "%ProgramFiles(x86)%\Mozilla Firefox\firefox.exe" Exit/B
Start "" "%ProgramFiles(x86)%\Mozilla Firefox\firefox.exe"
:Loop
Timeout 5 /NoBreak>Nul
QProcess firefox.exe>Nul 2>&1||GoTo :Loop
NirCmd SendKeyPress F11
I suggest for this task the batch file:
#echo off
start "" /max firefox.exe
if errorlevel 1 goto :EOF
set LoopCount=0
:WaitLoop
%SystemRoot%\System32\timeout.exe /T 5
%SystemRoot%\System32\tasklist.exe /FI "IMAGENAME eq firefox.exe" 2>nul | %SystemRoot%\System32\find.exe /I "firefox.exe" >nul
if not errorlevel 1 nircmd.exe sendkeypress F11 & goto :EOF
set /A LoopCount+=1
if not %LoopCount% == 6 goto WaitLoop
Let me explain the few command lines used here.
1. Starting Firefox
The command START being an internal command of cmd.exe interprets the first double quoted string as optional title for the console window. Therefore the command line
start "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
results just in opening a new console window with the window title:
C:\Program Files (x86)\Mozilla Firefox\firefox.exe
For that reason "" is specified as first START argument to define an empty title. Firefox is a GUI application. So no console window is opened which means an empty window title is really enough.
The parameter /max would not be really necessary, but the goal is to get Firefox into full screen mode after starting. So why not starting it already maximized?
32-bit version of Firefox is by default installed in directory %ProgramFiles% on 32-bit Windows and in %ProgramFiles(x86)% on 64-bit Windows. But it is possible during the installation to install Firefox into any other folder. But Firefox installer is well coded and registers firefox.exe in Windows registry under key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
This is recommended by Microsoft as written in MSDN article Application Registration.
The command START searches also in Windows registry under this key for an executable specified as argument without path as explained in answer on Where is “START” searching for executables?
This is the reason for using just firefox.exe on START command line because that starts an installed Firefox independent on installation location.
START displays an appropriate message box if firefox.exe could not be started and exits in this case with a return code greater 0 (9059 in my test on one computer).
The help output on running if /? in a command prompt window explains how to evaluate the exit code of a previous command or application without usage of immediate or delayed environment variable expansion and therefore working anywhere in a batch file from MS-DOS (really!) to currently latest Windows 10.
The command line if errorlevel 1 goto :EOF means IF start failed to start firefox.exe indicated by an exit code greater or equal 1 THEN exit execution of this batch file. For details on exiting batch file execution see answer on Where does GOTO :EOF return to?
2. Checking for running Firefox
The command TASKLIST being an external command, i.e. a console application in system directory of Windows, outputs a list of running processes. This list can be already filtered by TASKLIST itself for a specific process as done in batch file with /FI "IMAGENAME eq firefox.exe".
But TASKLIST is designed for just printing a list of processes. It is not designed for checking if a specific process is running and returning the result to the calling process via exit code. TASKLIST always exits with 0.
But an error message is output to handle STDERR on using a filter and no process can be found in process list matching this filter. For that reason 2>nul is used to suppress this error message by redirecting it to device NUL. Read the Microsoft article about Using Command Redirection Operators for more information about redirection.
A simple method to get a simple false/true respectively 0/1 result on checking for running Firefox is filtering output of TASKLIST with external command FIND which exits with 0 if the string to find was indeed found or with 1 if the searched string could not be found in the text read in this case from STDIN. The output of FIND is of no interest and therefore suppressed with redirection to device NUL using >nul.
Instead of using TASKLIST and FIND it is also possible to use QPROCESS:
%SystemRoot%\System32\qprocess.exe firefox.exe >nul 2>&1
QPROCESS exits with exit code 1 if firefox.exe could not be found in list of running processes. Otherwise the exit code is 0 on firefox.exe is running.
3. Evaluating Firefox process checking result
if not errorlevel 1 nircmd.exe sendkeypress F11 & goto :EOF
The IF command checks if exit code of FIND is NOT greater or equal 1 which means if exit code is lower than 1. Command FIND exits never with a negative value. So if this condition is true then it is time to execute nircmd.exe to send key press F11 to application in foreground hopefully being Firefox (not guaranteed by this code) and exit batch file processing.
Otherwise the batch file should wait once again 5 seconds and then do the check again. This can very easily result in an endless running batch file in case of started Firefox is immediately closed by the user before the 5 seconds wait timed out. For that reason it is counted how often the wait loop is already executed. After 6 loop runs, or 30 seconds, it is really time to no longer wait for Firefox and exit the batch file.
4. Getting more information about used commands
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
find /?
if /?
qprocess /?
set /?
start /?
tasklist /?
timeout /?
And Single line with multiple commands using Windows batch file should be also read explaining operator & in addition to all other web pages referenced already above.
You can show a list of opened programs like this:
tasklist
To check if firefox exists:
EDIT: Code edited to show a fully working example
#echo off
start "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
goto :checkloop
:checkloop
tasklist|find "firefox.exe" > NUL
if %ERRORLEVEL% == 0 (
call :fullscreen
exit
) else (
call :timeout
goto :checkloop
)
:fullscreen
nircmd sendkeypress F11
goto :EOF
:timeout
timeout /t 5
goto :EOF

Error level not changing when file is ran with psexec

I have one piece of code that is behaving differently when I ran it on server and when I ran it with psexec. I'm actually trying to determent is computer offline, but that is not question here. Problem is that when I run this command directly on server errorlevel is changing to 1, because pc is online. But when I use psexec to run file with same commands on that server errorelevel is not changing it stays 0. I cant find any explanations on internet.
echo %errorlevel%
ping -n 1 machine | findstr "not" > nul
IF %errorlevel%==0 (
echo test
)
echo %errorlevel%
pause
What you show is not a command, but a batch file, say ping_machine.cmd. To call it from psexec the command line would be something like psexec \\server cmd /c ping_machine.cmd. If I guessed wrong you may stop reading the rest of this answer now (and next time post enough relevant context so that one doesn't have to guess).
Problem is that cmd /c returns the exit code of ping_machine, but the batch file does not explicitly set an exit code, so it returns 0 by default. This can be verified at the cmd prompt with the following 2 runs - note that inside the batch file you see the same/correct errorlevels, but cmd /c returns 0.
C:\etc>ping_machine
0
1
Press any key to continue . . .
C:\etc>echo %errorlevel%
1
C:\etc>cmd /c ping_machine
0
1
Press any key to continue . . .
C:\etc>echo %errorlevel%
0
To have cmd /c behave as you expect (and in turn psexec as well), add the following line at the end of the batch file to return the respective errorlevel (this works because neither echo nor pause modify the errorlevel, otherwise you'd have to save it in a temp variable for later use).
exit /b %errorlevel%
There it is. When I run this batch file locally errorlevel is changed to 9009. When I use psexec \\computername -f -c pathToFile , error code is staying 0.
#echo off
echo %errorlevel%
pin 234343
echo %errorlevel%
pause

Windows batch file to connect to share and execute app

I have a batch file running on Windows 7 x86. I am trying to script a connection to a share and then once that is done to execute an app.
net use \\appserver001\mycompany.ptshare\OPS /user:geek xyz!## /persistent:yes
echo %errorlevel%
if errorlevel 2 goto message
if errorlevel 1 goto message
start "" "\\appserver001\mycompany.ptshare\OPS\OPSapp.exe"
echo %errorlevel%
if errorlevel 2 goto message
if errorlevel 1 goto message
goto end
:message
cls
echo ERROR IN CONNECTING TO SERVER
:end
The NET USE connection is made as the command window says "The command completed successfully". Then when it reaches the line to execute the app I get a pop-up message "Invalid location for program!".
I swear I have done his before but cant figure out why it will not run my app.
"Invalid location for program!" sounds like an error message that could come from OPSapp.exe itself. You could verify that by opening Task Manager and checking whether OPSapp is listed in the Processes tab when the error pops. If that's not the case then you may ignore the rest of this post.
Some programs require to be launched from a drive-letter based path (e.g. X:\etc\app.exe) rather than a UNC path (e.g. \\srv\etc\app.exe). Such a program can check at startup the location where it's been started from, then pop an error message and exit if it's a UNC path, instead of the drive-letter based one it expects.
The workaround in such cases is to (temporarily) map a drive letter to the network share, then use it to launch the program. For example, in your case replace
start "" "\\appserver001\mycompany.ptshare\OPS\OPSapp.exe"
with
pushd "\\appserver001\mycompany.ptshare\OPS"
start "" "OPSapp.exe"
If this still doesn't work, then it's possible that OPSapp expects more than just a drive-letter based path, perhaps a particular directory structure someplace, or registry entries etc - in other words it may not be portable enough to be simply runnable by path.

Why does batch file (.bat) converted to executable (.exe) not work?

So I have a batch file that I am trying to convert but i'm no success. The converter that I am using is
Bat To Exe Converter. The problem that I am encountering is that after converting the batch file it does not execute properly and immediately says "Press any key to continue . . ." and then closes. The batch file works fine on its own and when I converted it using the websites online converter it also worked (I would use the online but has little functions and is not exactly what I need).
Below is the batch code that I am using:
#ECHO OFF
TITLE ADB Over Network Running...
COLOR 17
CLS
IF "%ANDROID_PLATFORM_TOOLS%" == "" GOTO NOPATH
ADB tcpip 5555
IF ERRORLEVEL 1 GOTO END
IF ERRORLEVEL 0 GOTO NEXT
GOTO END
:NEXT
set /P ip=Enter Devices IP: %=%
ADB connect %ip%
GOTO END
:NOPATH
ECHO "ANDROID_PLATFORM_TOOLS" not found. Please add this environment variable
GOTO END
:END
PAUSE
EXIT
I hope that you can help me. Thank you for any help and your time :D
As all that program does is extract the batch file into a subfolder of temp and execute it, Windows has the exact same feature.
Type
iexpress
in Start - Run and follow the wizard and set your bat to run as the last step.

Resources