Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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...

@echo off
setLocal EnableDelayedExpansionfor /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

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?

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

:: find abc.dat in a tree, one level down
@echo off
setLocal EnableDelayedExpansionpushd 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

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

In this case, not much diff.
=====================================
If at first you don't succeed, you're about average.M2

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

![]() |
![]() |
![]() |

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