Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello,
basically i have written a search batch that indexes all files & folders on chosen HDD's.
This is done with dir %drive% /s /b >>index.tmp
Then searching is done with findstr /c:"%search%" "%CurDir%\Index.tmp" >>Result.html, which outputs the result to a html file (with clickable links to the matching files later on).
Now, i would also like the possibility to open the folder containing the file(s).Example. The index file contains a string like this:
c:\windows\tmp\somefile.txt
If i search for "somefile" it'll output the link to the result html file:
* C:\windows\tmp\somefile.txt
Then, under here i also would like a link to: c:\windows\tmp (the root folder of the file)
So, anyone know how to do this? Strip off everything after the last slash or something like that?
Batch solution preferred, but perl will also do.Letme know if anything is unclear, or if you need a copy of the script to fully understand my mess... :P

Here's a sample set of commands you could use.
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('findstr /c:"%search%" index.tmp') do (
set fullpathname=%%a
rem Open an Explorer window on the folder and select the file
explorer /select,!fullpathname!
rem The following gets just the folder
for %%f in ("!fullpathname!") do set folderpath=%%~dpf
)

Change the bold from ref to href.
@echo off
if exist index.htm (
del index.htm
)
if exist index.tmp (
del index.tmp
)
echo ^<head^> > index.htm
echo ^<body^> >> index.htm
set drive=h:
dir %drive% /a-d /b /s >> index.tmp
for /f "tokens=*" %%f in (index.tmp) do (
echo ^<br^> >> index.htm
echo ^<a ref="%%f"^>%%~nxf^</a^> .......... ^<a ref="%%~dpf"^>Open Containing folder^</a^> >> index.htm
)echo ^</head^> >> index.htm
echo ^</body^> >> index.htm
"Computer security." — Oxymoron

Tony's solution has a couple issues, one being that it puts the entire body into the head section.
Are you running a web server? If not, what's the point of doing this? Without a web server, this will only work for the local station and only works in IE (or at least Tony's solution doesn't work in Firefox). This functionality is already built-in to all web browsers by using the file:// protocol instead of http://
You can test this by putting this into your location/address bar.
file:///C:/

Thanx for the replies everybody:)
I'll have a look at it as soon i get some free time at work.
FishMonger: I'm not running a webserver no. It's meant to be a local search script which i use for myself at work. Searching for files on our fileserver in explorer takes forever. Having it all "indexed" lets me find things very quickly. Also, i can search with up to 3 keywords like: "budget,2008,.doc".
Using "file:///X:/" aint a bad idea tho. Just didnt thought about it.

Here's a copy of the source if anybody is interested. (Sorry, its a little messy)
http://pastebin.com/m2a3deca3

tonysathre: I couldn't get your solution to work properly.
klint: I kinda see how yours work, but there is a slight problem. If i get lets say 50 hits on a search, i dont want to open the root folders of all those hits (i accidently tried that, hehe). I tried to mod it a little (removing the part where you launch explorer), but the link to the "containing" folder always end up pointing to where the script is runned from. I guess its required for explorer to open the full path before this FOR loop works: for %%f in ("!fullpathname!") do set folderpath=%%~dpf ?..
How does "%%~dpf" work btw?
Here's what i got so far:for /f "delims=" %%A in (tmp.txt) do (
set fullpathname=%%A
for %%f in ("!fullpathname!") do set folderpath=%%~dpf
echo ^<b^>* ^</b^> ^%%A ^</a^> ^ Open folder ^</a^>^<br^> >>Result.htm)Any ideas?

Some of the html source disapiered of course.. Giving it a new go
echo ^<b^>* ^</b^> ^%%A
^</a^> ^<a hr:)ef="%folderpath%"^> Open folder ^</a^>^<br^> >>Result.htm)

The %%~dpf gives you just the drive letter and path of a for loop variable %%f. For more information, type "FOR /?"
In the following command
for %%f in ("!fullpathname!") do set folderpath=%%~dpf
this is not really a loop, as it only loops once (just for the one file specified) but the for command is still needed for %%~dpf to work.
As to why %%~dpf expands to the current directory in your case, you should find out by inserting a debug trace in your loop:
echo "!fullpathname!"
just before the for %%f in ("!fullpathname!") ... line.
If that produces "!fullpathname!", that means you've forgotten to add the line
setlocal EnableDelayedExpansion
at the begginning of the file. You need this line, otherwise %%~dpf is asking for the directory of a fictitious file named "!fullpathname!" which is the current directory.

Hi Klint,
EnableDelayedExp... is already enabled.
I did some tests with the following code:for /f "delims=" %%A in (tmp.txt) do (
set fullpathname=%%A
echo "!fullpathname!" & pause
for %%f in ("!fullpathname!") do set folderpath=%%~dpf
echo %folderpath% & pause)The result is:
echo "!fullpathname!" gives the full path, so it works.
echo %folderpath% on the other hand just gives ECHO is off.For some reason "do set folderpath=%%~dpf" doesn't work for me.. Any ideas?

Aah, dang..
That solved it right. I'm not very familiar with that types of variables (!something!).
Is there an explanation on this matter somewhere?
Anyway, thanx a million klint :)

The explanation of delayed variable expansion is in the online help obtained by the SET /? command. Here it is:
Delayed environment variable expansion is useful for getting around
the limitations of the current expansion which happens when a line
of text is read, not when it is executed. The following example
demonstrates the problem with immediate variable expansion:set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "%VAR%" == "after" @echo If you see this, it worked
)would never display the message, since the %VAR% in BOTH IF statements
is substituted when the first IF statement is read, since it logically
includes the body of the IF, which is a compound statement. So the
IF inside the compound statement is really comparing "before" with
"after" which will never be equal. Similarly, the following example
will not work as expected:set LIST=
for %i in (*) do set LIST=%LIST% %i
echo %LIST%in that it will NOT build up a list of files in the current directory,
but instead will just set the LIST variable to the last file found.
Again, this is because the %LIST% is expanded just once when the
FOR statement is read, and at that time the LIST variable is empty.
So the actual FOR loop we are executing is:for %i in (*) do set LIST= %i
which just keeps setting LIST to the last file found.
Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time. If delayed variable expansion is enabled, the above
examples could be written as follows to work as intended:set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "!VAR!" == "after" @echo If you see this, it worked
)set LIST=
for %i in (*) do set LIST=!LIST! %i
echo %LIST%

![]() |
Opened folder (batch)
|
Toggle Button Help
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |