g.bat to jump between folders - need help to make it pure batch!
Posted: 13 May 2016 06:28
Hi, some time ago I made g.bat. This is based on an old dos program I used to have which no longer works.
What is does is create a text database of all folders (on one or more drives), and then search that database to quickly jump to where you want to go.
Example:
g fold
would jump to the first folder in the database containing the string "fold". If you were already in such a folder, it wold jump to the next one instead.
If you knew that the folder you want to jump to was inside another folder called e.g. "mine", you could do:
g ne\fold or g mine/fold
to make it more likely to get to the right spot.
Often g.bat can really save time instead of using cd to get around.
To create or update database, type g * [drives(default c)] e.g.
g * cd (creates database of folders in drives C and D)
So anyway this script uses a bunch of external tools, and I was wondering if you guys could help me out to make it pure Batch? I suppose the different replace utilities I have seen here will be useful. Also, hopefully without losing speed.
Here is the script:
It uses tr.exe to replace \ with / (because I want the script to accept both / and \ as input to g)
It uses grep.exe to search for a pattern where the input given is found, and is not followed by any /, and is ending in a newline
It uses gawk.exe, first to remove beginning and trailing ", and second in this separate script (this must be copied to the TEMP folder in a file called g.awk)
This one creates a bat file based on the input, comparing the first line (which is the current path) to the ones found by grep. The resulting bat file uses CD to go to the folder.
Help, please!
What is does is create a text database of all folders (on one or more drives), and then search that database to quickly jump to where you want to go.
Example:
g fold
would jump to the first folder in the database containing the string "fold". If you were already in such a folder, it wold jump to the next one instead.
If you knew that the folder you want to jump to was inside another folder called e.g. "mine", you could do:
g ne\fold or g mine/fold
to make it more likely to get to the right spot.
Often g.bat can really save time instead of using cd to get around.
To create or update database, type g * [drives(default c)] e.g.
g * cd (creates database of folders in drives C and D)
So anyway this script uses a bunch of external tools, and I was wondering if you guys could help me out to make it pure Batch? I suppose the different replace utilities I have seen here will be useful. Also, hopefully without losing speed.
Here is the script:
Code: Select all
@echo off
if "%~1" == "" echo.&echo Usage: g [input]&echo g ^* [drives(default c)] to create/update folder database&goto :eof
set GTMP=
if not "%TMP%" == "" set GTMP=%TMP%
if not "%TEMP%"=="" set GTMP=%TEMP%
if "%GTMP%" == "" echo Set TMP environment variable to temporary folder first.&goto :eof
if not "%~1" == "*" goto GODIR
echo "%CD%">%GTMP%\tmp-cd.dat
set ARG=c&if not "%~2" == "" set ARG=%~2
del /Q %GTMP%\go.dat 2>nul
echo Updating go database...
:DBLOOP
set DRV=%ARG:~0,1%
set ARG=%ARG:~1%
cd /D %DRV%: 2>nul
if not %ERRORLEVEL% == 0 goto SKIPDRIVE
dir /s /b /-p /ad \ 2>nul|tr \\ /|find /V "%%" |find /V "$" >>%GTMP%\go.dat
:SKIPDRIVE
if not "%ARG%" == "" goto DBLOOP
for /F %%a in (%GTMP%\tmp-cd.dat) do cd /D %%a
echo Done.
set ARG=
set DRV=
goto :eof
:GODIR
if not "%~2" == "" echo.&echo Usage: g [input]&echo g ^* [drives(default c)] to create/update folder database&goto :eof
if not exist %GTMP%\go.dat echo No database, run g * first.&goto :eof
echo "%CD%"| gawk "{ print substr($0,2,length($0)-2) }" | tr \\ />%GTMP%\go-temp1.dat
set ARG="%~1"
set ARG=%ARG:\=/%
type %GTMP%\go.dat|grep -i "%ARG:~1,-1%[^/]*$">>%GTMP%\go-temp1.dat
type %GTMP%\go-temp1.dat | gawk -f %GTMP%\g.awk> %GTMP%\go-temp2.bat
call %GTMP%\go-temp2.bat
set ARG=
It uses grep.exe to search for a pattern where the input given is found, and is not followed by any /, and is ending in a newline
It uses gawk.exe, first to remove beginning and trailing ", and second in this separate script (this must be copied to the TEMP folder in a file called g.awk)
Code: Select all
{ IN[NR]=$0 }
END {
if (NR>1) {
CH=2;
for(i=2; i<NR; i++) {
if(index(IN[1],IN[i])==1 && length(IN[1])==length(IN[i]))
CH=i+1;
}
i=CH;
do {
printf("cd /D \"%s\" 2>nul\nIF \"%%ERRORLEVEL%%\" == \"0\" GOTO :EOF\n",IN[i]);
i++;
if(i>NR) i = 2;
} while(i!=CH);
}
}
Help, please!