Does anybody know what is the equivalent to $?
in Windows command line? Is there any?
EDIT: $?
is the UNIX variable which holds the exit code of the last process
The where command is a Windows which equivalent in a command-line prompt (CMD). In a Windows PowerShell the alternative for the which command is the Get-Command utility.
Use gcm as the Equivalent of Which Command in PowerShell You can use the gcm alias as the equivalent of which command in PowerShell.
Windows “dir” Command is “ls” Command Equivalent Windows MS-DOS and PowerShell command-line interface provide the dir command in order to list files and folders.
You want to check the value of %ERRORLEVEL%
.
%ERRORLEVEL% Returns the error code of the most recently used command. A non zero value usually indicates an error.
http://technet.microsoft.com/en-us/library/bb490954.aspx
$? Contains True if last operation succeeded and False otherwise. And
$LASTEXITCODE Contains the exit code of the last Win32 executable execution.
http://blogs.msdn.com/powershell/archive/2006/09/15/ErrorLevel-equivalent.aspx
$? Expands to the exit status code of the most recently executed foreground program.
http://unix.sjcc.edu/cis157/BashParameters.htm
Sorry to dredge up an old thread, but it's worth noting that %ERRORLEVEL% doesn't get reset with every command. You can still test "positive" for errorlevel after several lines of subsequent--and successful--batch code.
You can reliably reset errorlevel to a clean status with ver
. This example works with UnxUtils for a more Linux-ish directory listing. The reset might seem extraneous at the end, but not if I need to call this script from another.
:------------------------------------------------------------------------------
: ll.bat - batch doing its best to emulate UNIX
: Using UnxUtils when available, it's nearly unix.
:------------------------------------------------------------------------------
: ll.bat - long list: batch doing its best to emulate UNIX
: ------------------------------------
: zedmelon, designer, 2005 | freeware
:------------------------------------------------------------------------------
@echo off
setlocal
: use the UnxUtil ls.exe if it is in the path
ls.exe -laF %1 2>nul
if errorlevel 1 (
echo.
echo ----- ls, DOS-style, as no ls.exe was found in the path -----
echo.
dir /q %1
)
: reset errorlevel
ver>nul
endlocal
Feel free to use this. If you haven't seen UnxUtils, check 'em out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With