Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Batch, get folder of a found file

Original Message
Name: Shr0Om
Date: January 11, 2008 at 05:29:31 Pacific
Subject: Batch, get folder of a found file
OS: XP
CPU/Ram: amd 64 3200
Model/Manufacturer: custom
Comment:
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


Report Offensive Message For Removal


Response Number 1
Name: klint
Date: January 11, 2008 at 06:55:58 Pacific
Subject: Batch, get folder of a found file
Reply: (edit)
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
)


Report Offensive Follow Up For Removal

Response Number 2
Name: tonysathre
Date: January 11, 2008 at 09:07:41 Pacific
Subject: Batch, get folder of a found file
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 3
Name: FishMonger
Date: January 11, 2008 at 15:55:45 Pacific
Subject: Batch, get folder of a found file
Reply: (edit)
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:/


Report Offensive Follow Up For Removal

Response Number 4
Name: Shr0Om
Date: January 14, 2008 at 00:57:01 Pacific
Subject: Batch, get folder of a found file
Reply: (edit)
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.



Report Offensive Follow Up For Removal

Response Number 5
Name: Shr0Om
Date: January 14, 2008 at 01:46:52 Pacific
Subject: Batch, get folder of a found file
Reply: (edit)
Here's a copy of the source if anybody is interested. (Sorry, its a little messy)
http://pastebin.com/m2a3deca3

Report Offensive Follow Up For Removal


Response Number 6
Name: FishMonger
Date: January 14, 2008 at 11:08:43 Pacific
Subject: Batch, get folder of a found file
Reply: (edit)
Try this:
http://www.locate32.net/

Report Offensive Follow Up For Removal

Response Number 7
Name: Shr0Om
Date: January 15, 2008 at 04:22:52 Pacific
Subject: Batch, get folder of a found file
Reply: (edit)
It's more fun with DIY:)

Report Offensive Follow Up For Removal

Response Number 8
Name: Shr0Om
Date: January 15, 2008 at 07:45:41 Pacific
Subject: Batch, get folder of a found file
Reply: (edit)
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?



Report Offensive Follow Up For Removal

Response Number 9
Name: Shr0Om
Date: January 15, 2008 at 07:51:47 Pacific
Subject: Batch, get folder of a found file
Reply: (edit)
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)


Report Offensive Follow Up For Removal

Response Number 10
Name: klint
Date: January 15, 2008 at 09:07:09 Pacific
Subject: Batch, get folder of a found file
Reply: (edit)
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.


Report Offensive Follow Up For Removal

Response Number 11
Name: Shr0Om
Date: January 16, 2008 at 02:22:04 Pacific
Subject: Batch, get folder of a found file
Reply: (edit)
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?


Report Offensive Follow Up For Removal

Response Number 12
Name: klint
Date: January 16, 2008 at 06:11:16 Pacific
Subject: Batch, get folder of a found file
Reply: (edit)
Aha, you're using %folderpath% when you should be using !folderpath!.

Report Offensive Follow Up For Removal

Response Number 13
Name: Shr0Om
Date: January 16, 2008 at 06:43:57 Pacific
Subject: Batch, get folder of a found file
Reply: (edit)
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 :)

Report Offensive Follow Up For Removal

Response Number 14
Name: klint
Date: January 16, 2008 at 07:30:51 Pacific
Subject: Batch, get folder of a found file
Reply: (edit)
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%



Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch, get folder of a found file

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




how to setup call of duty to joytok

WindowsME / HotMail Problem

Corrupt memory

Convert fat32 to Ntfs

Best WinMo phone of 2008


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC