- What is the equivalent to $? in Windows?
- 5 Answers 5
- Windows Batch Files
- Windows Powershell
- Cygwin Bash Scripting
- Is there an equivalent of ‘which’ on the Windows command line?
- 26 Answers 26
- Finding executables using only part of the name
- Finding custom executables
- Windows equivalent find -delete
- 3 Answers 3
- How do I find the location of an executable in Windows?
- 14 Answers 14
- Example
- Output:
- Windows equivalent of $export
- 2 Answers 2
- Not the answer you’re looking for? Browse other questions tagged windows or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
What is the equivalent to $? in Windows?
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
5 Answers 5
You want to check the value of %ERRORLEVEL% .
Windows Batch Files
%ERRORLEVEL% Returns the error code of the most recently used command. A non zero value usually indicates an error.
Windows Powershell
$? Contains True if last operation succeeded and False otherwise. And
$LASTEXITCODE Contains the exit code of the last Win32 executable execution.
Cygwin Bash Scripting
$? Expands to the exit status code of the most recently executed foreground program.
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.
Feel free to use this. If you haven’t seen UnxUtils, check ’em out.
Is there an equivalent of ‘which’ on the Windows command line?
As I sometimes have path problems, where one of my own cmd scripts is hidden (shadowed) by another program (earlier on the path), I would like to be able to find the full path to a program on the Windows command line, given just its name.
Is there an equivalent to the UNIX command ‘which’?
On UNIX, which command prints the full path of the given command to easily find and repair these shadowing problems.
26 Answers 26
Windows Server 2003 and later (i.e. anything after Windows XP 32 bit) provide the where.exe program which does some of what which does, though it matches all types of files, not just executable commands. (It does not match built-in shell commands like cd .) It will even accept wildcards, so where nt* finds all files in your %PATH% and current directory whose names start with nt .
Try where /? for help.
Note that Windows PowerShell defines where as an alias for the Where-Object cmdlet, so if you want where.exe , you need to type the full name instead of omitting the .exe extension.
While later versions of Windows have a where command, you can also do this with Windows XP by using the environment variable modifiers, as follows:
You don’t need any extra tools and it’s not limited to PATH since you can substitute any environment variable (in the path format, of course) that you wish to use.
And, if you want one that can handle all the extensions in PATHEXT (as Windows itself does), this one does the trick:
It actually returns all possibilities but you can tweak it quite easily for specific search rules.
$PATH:%i To add it to an alias.bat script that you load everytime you run cmd.exe (put the above script in a new directory called C:\usr\aliases): DOSKEY which=C:\usr\aliases\which.bat $* Then you can make a script to launch cmd.exe with the alias.bat file: cmd.exe /K E:\usr\aliases\alias.bat – Brad T. Apr 25 ’14 at 20:42
Under PowerShell, Get-Command will find executables anywhere in $Env:PATH .
And since powershell let’s you define aliases, which can be defined like so.
PowerShell commands are not just executable files ( .exe , .ps1 , etc). They can also be cmdlets, functions, aliases, custom executable suffixes set in $Env:PATHEXT , etc. Get-Command is able to find and list all of these commands (quite akin to Bash’s type -a foo ). This alone makes it better than where.exe , which.exe , etc which are typically limited to finding just executables.
Finding executables using only part of the name
Finding custom executables
Unlike UNIX, where executables are files with the executable ( +x ) bit set, executables on windows are files present in one of the directories specified in the $PATH env. variable whose filename suffixes are named in the $PATHEXT env. variable (defaults to .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL ).
As Get-Command also honours this env. variable, it can be extended to list custom executables. e.g.
See Get-Command for more options and examples.
In Windows PowerShell:
If you have PowerShell installed (which I recommend), you can use the following command as a rough equivalent (substitute programName for your executable’s name):
The GnuWin32 tools have which , along with a whole slew of other Unix tools.
In Windows CMD which calls where :
Cygwin is a solution. If you don’t mind using a third-party solution, then Cygwin is the way to go.
Cygwin gives you the comfort of *nix in the Windows environment (and you can use it in your Windows command shell, or use a *nix shell of your choice). It gives you a whole host of *nix commands (like which ) for Windows, and you can just include that directory in your PATH .
In PowerShell, it is gcm , which gives formatted information about other commands. If you want to retrieve only path to executable, use .Source .
For instance: gcm git or (gcm git).Source
- Available for Windows XP.
- Available since PowerShell 1.0.
- gcm is an alias of Get-Command cmdlet.
- Without any parameters, it lists down all the available commands offered by the host shell.
- You can create a custom alias with Set-Alias which gcm and use it like: (which git).Source .
- Official docs: https://technet.microsoft.com/en-us/library/ee176842.aspx
I have a function in my PowerShell profile named ‘which’
Here’s what the output looks like:
gold on windows platforms, puts all the nice unix utilities on a standard windows DOS. Been using it for years.
It has a ‘which’ included. Note that it’s case sensitive though.
NB: to install it explode the zip somewhere and add . \UnxUtils\usr\local\wbin\ to your system path env variable.
If you can find a free Pascal compiler, you can compile this. At least it works and shows the algorithm necessary.
Not in stock Windows but it is provided by Services for Unix and there are several simple batch scripts floating around that accomplish the same thing such this this one.
The best version of this I’ve found on Windows is Joseph Newcomer’s «whereis» utility, which is available (with source) from his site.
The article about the development of «whereis» is worth reading.
None of the Win32 ports of Unix which that I could find on the Internet are satistactory, because they all have one or more of these shortcomings:
- No support for Windows PATHEXT variable. (Which defines the list of extensions implicitely added to each command before scanning the path, and in which order.) (I use a lot of tcl scripts, and no publicly available which tool could find them.)
- No support for cmd.exe code pages, which makes them display paths with non-ascii characters incorrectly. (I’m very sensitive to that, with the ç in my first name :-))
- No support for the distinct search rules in cmd.exe and the PowerShell command line. (No publicly available tool will find .ps1 scripts in a PowerShell window, but not in a cmd window!)
So I eventually wrote my own which, that suports all the above correctly.
Windows equivalent find -delete
What is the Windows/DOS equivalent to this Linux command?
find . -path «*/migrations/*.py» -not -name «__init__.py» -delete
I know how to delete all the files but not how to specify an exception (ie: not deleting __init__.py )
3 Answers 3
cmd.exe doesn’t support wildcards in several levels of a path (PowerShell does) so you’ve to emulate this somehow.
If the output looks OK, remove the echo
In a batch file double the percent signs %F => %%F
If the output looks OK, remove the -WhatIf
In this sample tree
the output will be
Exceptions are not possible (as far as I know) in Windows/DOS search, but they are possible in xcopy /exclude and robocopy /X. . Therefore I’d advise you to copy all but the exceptions to a kind of backup folder, remove all the original ones (including the exceptions) and put everything back.
You can probably use for /R and if , something like this:
Here I’ve prefixed the del command with echo so it doesn’t actually delete it. Once it looks like it’s doing what you want, then take away that echo.
If you do this in a batch file, you’ll need to double up the % signs.
The for /R is a recursive for, and in that format will work from the current directory.
ni says «give me the filename part only, of %i»
(I’m running Linux at the moment so I can’t verify exact behaviour, but you could start with this).
How do I find the location of an executable in Windows?
I remembered that I used a tool called as where to find locations for any executable programs like this in a console:
Now I cannot find this tool. Not sure if Windows has a build-in tool to do that search?
14 Answers 14
According to the StackOverflow answer at Is there an equivalent of ‘which’ on windows?, where.exe does this on Windows 7 and Windows Server 2003 and later:
Example
Output:
In powershell use where.exe , Get-Command (or its abbreviation gcm ), as where is the default alias for Where-Object .
EDIT: I should have added, if you can’t use the WHERE command from the command prompt, check your PATH variable. (Just use the «path» command.) Make sure C:\Windows\System32 is in your path. That’s where «where.exe» is located.
WHERE is the command you’re looking for! WHERE is like a cross between the UNIX shell built-in «which» and the «locate» command, in that it works for both command executables and regular files.
It’s also somewhat more complex than either of those two, although, in general a simple
It’s different from the «locate» command in that it’s not looking through the entire filesystem. Instead, the default behavior is to look for files in two locations:
- The current directory.
- All of the directories in the PATH variable.
So, any command that you can run directly from a command prompt without specifying the directory, will be found by the WHERE command. (Because any command like that is already in the PATH variable list.)
If you want to search only in the command path variable, you can use:
If, on the other hand, you want to find all copies of a file in a directory tree, you can use:
Finally, WHERE will find commands and any files with an extension from the PATHEXT variable without including the extension. All other files have to be specified either exactly or with wildcards.
Take for example the files «dxdiag.exe» and «dxdiagn.dll». Note the following command and its output:
It succeeds in returning all versions of «dxdiag.exe» because «.exe» is one of the extensions in the PATHEXT variable. (Note: «WHERE dxdiag» would have worked as well, because C:\Windows\System32 is in the PATH variable.)
on the other hand, fails to return any result, because «.dll» is not in PATHEXT.
In this case, look at the result that adding a wildcard gives us:
It successfully returns all versions of dxdiagn.dll.
For more information, use «WHERE /?». Hope this helps!
Windows equivalent of $export
I am trying to follow some instructions for creating a directory using the command line. The instructions are:
Are these windows commands? Are there windows equivalents?
2 Answers 2
To translate your *nix style command script to windows/command batch style it would go like this:
mkdir on windows doens’t have a -p parameter : from the MKDIR /? help:
MKDIR creates any intermediate directories in the path, if needed.
which basically is what mkdir -p (or —parents for purists) on *nix does, as taken from the man guide
/.bashrc – user137717 Feb 5 ’16 at 18:31
There is not an equivalent statement for export in Windows Command Prompt. In Windows the environment is copied so when you exit from the session (from a called command prompt or from an executable that set a variable) the variable in Windows get lost. You can set it in user registry or in machine registry via setx but you won’t see it if you not start a new command prompt.
Not the answer you’re looking for? Browse other questions tagged windows or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.