g.bat to jump between folders - need help to make it pure batch!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Eureka!
Posts: 136
Joined: 25 Jul 2019 18:25

Re: g.bat to jump between folders - need help to make it pure batch!

#16 Post by Eureka! » 31 Aug 2020 08:20

misol101 wrote:
31 Aug 2020 07:50
Your description of how g.bat works is basically correct, except the third line with "my" would also be found since somewhere in the database of folders, that "my" folder is also going to be at the end of the line, like "one/more/my". You can also use / or \, so if you were looking to go to "project/folder" you could try "g ct/fo" or similar.
But it does exclude all the other unwanted subfolders of one\more\my

The most interesting (/least uninteresting :)) thing about the batch-only [1] version I posted was this:

Code: Select all

findstr /i /r /c:"^.*%*[^\\]*$" ALLFOLDERS.db
That would do a regex search for the parameter you entered if it is not followed by a \, meaning that it would have to be an "endpoint" to match.
That was just a concept; if you would actually use it, special regex characters in your parameter - like \ ^ ( ) { } [ ] + . and $ - need to be escaped before being fed to findstr (code available if needed).

Don't know if it is fast enough to your liking. I thought it was too slow, but maybe it will run smoother on more modern hardware.


[1] Depending on your definition; is using findstr still batch-only?

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

Re: g.bat to jump between folders - need help to make it pure batch!

#17 Post by siberia-man » 31 Aug 2020 13:48

I started review the code deeper and found some places for improvement. Later I found that better to rewrite it from the scratch. Perhaps i will try to do it (partially). From my perspective, there are few possible ways for evolution:

1. avoid \ with / replacement - in this case awk/find/findstr usage lowers
2. migrate heavy logic (\ with / replacement, string comparison and list rotation) to jscript/vbscript and use batch as wrapper around it

To this moment there are some small improvement.

This simplification has to do with the following example from the g.bat. This one

Code: Select all

echo "%CD%">%GTMP%\tmp-cd.dat
...
for /F %%a in (%GTMP%\tmp-cd.dat) do cd /D %%a
can be replaced bravely with:

Code: Select all

set "OLDCD=%CD%"
...
cd /d "%OLDCD%"
or this one:

Code: Select all

pushd "%CD%"
...
popd
There are other recommendations regarding awk/gawk usage. For example this one

Code: Select all

gawk "{gsub(/\\/,\""/\""); print}" | find /V "%" | find /V "$"
can be replaced with

explanation: if the input line $0 doesn't match % or $, replace \ with /

Code: Select all

gawk "$0 !~ /[%%$]/ { gsub(/\\/, \""/\""); print }"
explanation: if the input line $0 doesn't match % or $, replace \ with / passed as the variable s. It looks better because we avoid ugly attempts to escape double quotes within double quotes.

Code: Select all

gawk -v s="/" "$0 !~ /[%%$]/ { gsub(/\\/, s); print }"
The same result with sed in the case if you decide use it some time.

explanation: if input matches one of % or $, delete it; replace \ with / globally in other lines.

Code: Select all

sed "/[%$]/d; s#\\#/#g"
One more stuff.

Code: Select all

echo "%CD%"| gawk "{ $0=substr($0,2,length($0)-2); gsub(/\\/,\""/\"",$0); print}" >%GTMP%\go-temp1.dat
set ARG="%~1"
set ARG=%ARG:\=/%
In my opinion it could be better written as below (two last lines can be combined):

Code: Select all

cd | gawk -v s="/" "{ gsub(/\\/, s); print}" >%GTMP%\go-temp1.dat
set "ARG=%~1"
set "ARG=%ARG:\=/%"
type %GTMP%\go.dat | findstr /I /E "%ARG%[^/]*$">>%GTMP%\go-temp1.dat
I quickly reviewed g.awk script. It has many ways to improve but I am not sure that I fully understand how to modify it -- there is some specialty related to the situation:

Code: Select all

g one
g two
I seem written a lot of words. Hope they were useful.

Post Reply