Windows command line util to CD to the location of a file found in your system path, and optionally run a command. Finds the file using "where" and then cd's to that directory where the file is located.
Last active
August 29, 2015 14:14
-
-
Save icodebot/c5e16e9233f6db4e207a to your computer and use it in GitHub Desktop.
cd to the location of a file in system path, and optionally run a command. Finds the file using "where" and then cd's to the directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
echo "%~dp1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
SET godir= | |
FOR /F "tokens=*" %%i in ('%~dp0wherep %1') do SET godir=%%~i | |
if "%godir%" == "" goto pathnotfound | |
:setdir | |
pushd %godir% | |
:: Command to run? | |
if "%2"=="" goto :eof | |
FOR /F "tokens=*" %%i in ('where %1') do SET gofile=%%~i | |
%2 %gofile% | |
goto :eof | |
:pathnotfound | |
echo %1 not found | |
goto :eof | |
:help | |
echo. | |
echo gofile <filename> [command] | |
echo. | |
echo. Executes where on the file, and sets current directory to where it is located. | |
echo. use 'popd' to return to previous directory | |
echo. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
::Input is to be piped in to this script "where blah.txt | slash" | |
:: escapes backslash character c:\xxx\yyy\zzz become c:\\xxx\\yyy\\zzz | |
more | sed s/\\/\\\\/g |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
:: returns the path of a file found on path | |
where %1 | slash | xargs -I {} getpath {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This could simplified in to fewer command line files.
Requires:
xargs - http://www.gnu.org/software/findutils/findutils.html
sed - http://gnuwin32.sourceforge.net/packages/sed.htm