batch search f. and go to that dir

July 14, 2007 at 16:46:17
Specs: Windows XP SP2, P4 2,8Ghz / 1Gb

hi
i'm getting crazy with this matter... i would want to make a file batch that could search for a specific file (for example a file named abc.dat) and then assign to a variable the path of that file or could only go to to the directory containing it. It should be great to restrict search to one level subdirectory only... i know that there was an utility called "berkfind" that was able to do exactly this, but it is not compatible with Windows XP.
ps: sorry if my english sucks but it's not my main language...

See More: batch search f. and go to that dir

Report •


#1
July 14, 2007 at 18:04:29

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/s/b/a-s c:\temp\abc.dat') do (
set var=%%~DPa
echo the path to abc.dat is !var!
)


=====================================
If at first you don't succeed, you're about average.

M2



Report •

#2
July 15, 2007 at 03:42:51

mitico! :-)
i had to delete the string "c:\temp\" leaving only 'dir/s/b/a-s abc.dat'
and it's ok!!
thanks!

do you have any idea on how limit the search in the subdir to only the first level?


Report •

#3
July 15, 2007 at 04:36:29

I think you mean that if the tree is:


S:\MY
├───a
│ ├───1
│ └───2
└───b
├───1
└───2

[that got trashed as usual]

I think you want to start at MY and check a and b but NOT 1 and 2. Is that right?



=====================================
If at first you don't succeed, you're about average.

M2



Report •

Related Solutions

#4
July 15, 2007 at 05:10:52

exactly
do you know how to make it?

Report •

#5
July 15, 2007 at 06:19:43

:: find abc.dat in a tree, one level down

@echo off
setLocal EnableDelayedExpansion

pushd s:\my

for /f "tokens=* delims= " %%a in ('dir/b/ad ') do (
pushd %%a
for /f "tokens=* delims= " %%a in ('if exist abc.dat dir/b abc.dat') do (
set var=%%~DPa
)
popd
)
echo the path to abc.dat is !var!



=====================================
If at first you don't succeed, you're about average.

M2



Report •

#6
July 15, 2007 at 09:19:36

thanks a lot, it's perfect!


Report •

#7
July 23, 2007 at 09:06:06

hi
i was "studying" what Mechanix2Go told me, and i was thinking if there was a particular motive to use pushd and popd in the place of cd and cd..
that is:

pushd s:\my

for /f "tokens=* delims= " %%a in ('dir/b/ad ') do (
cd %%a
for /f "tokens=* delims= " %%a in ('if exist abc.dat dir/b abc.dat') do (
set var=%%~DPa
)
cd..
)
echo the path to abc.dat is !var!


there should be any difference?
tnx


Report •

#8
July 23, 2007 at 09:16:11

In this case, not much diff.


=====================================
If at first you don't succeed, you're about average.

M2



Report •

#9
July 23, 2007 at 09:39:01

in which case you must use pushd and popd?


Report •

#10
July 23, 2007 at 09:49:10

Any time you don't want to be bothered hard coding the DIR to return to.


=====================================
If at first you don't succeed, you're about average.

M2



Report •


Ask Question