Script to find path of named folders

November 19, 2009 at 14:12:12
Specs: Windows XP
Hello Experts

I am not a script-writer, by any means, so I am in need of some assistance please !

I am looking for a simple DOS batch file that, when executed, asks the user to enter 1 or more names. (Say, up to 10 names).
These names are actually the names of directories, e.g. ABC123 and XYZ456.
The directory names will be unique, but they may have different absolute paths.

So, once the names have been entered, the script then writes out a new file showing the full path to the specified directories.
Continuing my example above, the new file would contain entries similar to

C:\Documents and Settings\XYZ456

That's it !

It's probably really straight forward for anyone with a scripting background, but that's not me !

Hope someone can help



See More: Script to find path of named folders

Report •

November 19, 2009 at 16:52:11
Try the following and see if it works the way you would like?

@echo off
pushd C:\
echo Enter 'Exit' to quit
set /p strDir=What is the name of the directory?
If /i "%strDir%"=="Exit" goto EOF
dir /s /ad /b %strDir%
goto Begin

Report •

November 20, 2009 at 01:00:06
Thanks gtaion.

I've amended it slightly, so that it echo's the directory name into a named file.
So, there are now 2 things left for me to achieve.

1) If the user enters ABC and the directory is called ABC XYX, then the script doesn't find it. Presumably because of the space between words in the directory name ? How can this be corrected ?

2) If the user enters 3 directories consecutively, how can I capture all 3 in an output file ? In other words, how can I write [append] a new line into the file, for each discovered directory ?

Your help is appreciated.


Report •

November 20, 2009 at 02:59:55

1. gtaion did what you asked. You didn't originally specify that you wanted to search for substrings i.e. part of a directory's name. You just said you wanted to look for the name, that implies the full name.

2. You can append to a file by adding >>filename.txt at the end of a command. But try the following alternative script:

@echo off
setlocal enabledelayedexpansion
set findstrArgs=
echo Enter a blank line (just press Enter) to exit.
set dir=
set /p dir=Name (or partial name) of directory:
if not defined dir goto endloop
set findstrArgs=!findstrArgs! /c:"!dir![^\\]*$"
goto loop
echo Searching...
dir /s /ad /b \ | findstr /r !findstrArgs! >output.txt

Report •

Related Solutions

November 20, 2009 at 03:09:45
By the way, gtaion's solution is good but has a security hole. A malicious user of that script could enter this:

Enter 'Exit' to quit
What is the name of the directory?x" =="x" rd /s /q *

(P.S. Do not try it!)

The moral: never use %var% syntax for any variable obtained from a user at run-time. Use delayed expansion instead, i.e. !var!

Report •

November 20, 2009 at 04:14:13
Thanks Klint

Security is always a plus !

I tried your script and it does not seem to find the directory name at all.
For example, if I enter ABC and the actual directory is called ABC123, it doesn't find it.
If I enter ABC123 then it still doesn't find it.

I guess it's because of the findstr parameters, but I don't know what options to specify.

Any ideas ?

Report •

November 20, 2009 at 09:05:48
set /p var=What? 
dir /b /s /ad "C:\*%var%*" >>out.txt

Report •

November 20, 2009 at 09:40:57
Tomka999, sorry to hear it didn't work. It should do a dir of the
whole of the current drive, which perhaps is not C:. You can
use C:\ instead of just \ if that's what you want. If that doesn't
fix it, try this temporary change:

echo Searching...
echo on
dir /s /ad /b \ >unfiltered.txt
findstr /r %findstrArgs% <unfiltered.txt >output.txt

Check the findstr command is as expected, and look at
unfiltered.txt with notepad, and check that the folders you
want are actually there.

Report •

November 20, 2009 at 09:44:02
Razor, your solution is fine too (though I would use !var! instead
of %var%) - presumably those two lines would be inside the loop
previously given by gtaion. The problem with that is that the OP
wants to search for up to 10 folders, and this solution searches
the whole drive up to 10 times, instead of just once for all 10

Report •

November 20, 2009 at 10:11:02
Meh, my version is case insensitive, and doesn't create a temp file. Besides, the file system structure should be cached after the first scan. (At least is is with WinXP and Win2K.)

%var% vs !var!:
There are advantages to using delayed expansion; security is not one of them.

Report •

November 20, 2009 at 13:14:50
Razor: "There are advantages to using delayed expansion; security is not one of them."

Sure, that's not one of the reasons for delayed expansion stated in the documentation. But from experimenting I found that a user can do nasty things if the batch file uses %var%, which he can't do if !var! is used instead.

Of course, it could be said that a user can simply press ctrl-break and get back to the command line, but there are ways to prevent that too.

PS good point about caching.

Report •

November 20, 2009 at 14:10:34
Or they could just open another CMD window. But we digress.

Report •

November 20, 2009 at 15:33:14
tomka999: did you get an answer that will work for you?

Report •

December 1, 2009 at 05:21:44
Thank you to everyone who has helped me.
I am using Razor's directory search within Gtaion's loop.



Report •

December 1, 2009 at 06:06:13
I know I'm weighing into this late, but I think both Klint and Razor have very valid points.

There is a very good reason not to use delayed expansion because file + directory names can contain !'s which generally disappear with delayed expansion.

On the flip side && or ||(as well as other devious tricks) can be a problem with set /p and conventional variables, especially if the script is run with elevated privileges and only has read and execute file permissions.

My guess on a half way would be be "%var:"=%" as paths can be enclosed in quotes for all newer programs and the outer quotes would render && and || useless.

Batch Variable how to

Report •

December 1, 2009 at 10:08:54
Judago, can you explain what you mean by this:

"There is a very good reason not to use delayed expansion
because file + directory names can contain !'s which generally
disappear with delayed expansion."

I thought that delayed expansion meant you were shielded
even from variables that internally contain !s. For example, try the following script. Try it with an input like a-!username!-z.

@echo off
setlocal enabledelayedexpansion
set/p x=Please type something with lots of exclamation marks: 
echo You have typed !x!
echo Note that your exclamation marks have no effect on this script.
echo If they did, it would look like this:
echo %x%

Report •

December 1, 2009 at 11:01:35
Normally !'s are just another harmless character and have no meaning to the system without delayed expansion enabled, unlike |<>&^"*?=,;. as well as tab and space, of which the problematic chars tend to be |><^"& and in some cases =*.

set /p is a special case, as is setting the variable with exclamation marks before enabling delayed expansion.

The real problem tends to come from for loops where there is no choice. Also some times it's also desirable to return a variable from a script where standard %variables% must be used(i.e endlocal & set var=%var%).

Now lets say you want to run a for /f loop over a file to retrieve the data. Here is a simulation:

Notice just how "uncool" the problem is ;)

setlocal disabledelayedexpansion
>> somefile echo c:\yeah!\cool!\exclamation!.mark
setlocal enabledelayedexpansion
for /f "delims=" %%a in (somefile) do set line=%%a
echo !line!

The link in my sig touches on the subject a little if your interested.

Edit: Removed a confusing/wrongly tested bit....

Batch Variable how to

Report •

December 1, 2009 at 11:32:03
Hi Klint,

I must admit I haven't slept in a while and aimed that at the op(somehow I read the post but somehow missed your user name).

You are right about the internal protection working with set /p. Though I think the problem I pointed out can be a real concern in a lot of situations.............

Batch Variable how to

Report •

December 2, 2009 at 03:26:51
Thanks for that Judago, that's something new I've learned. Typical, isn't it, yet another arbitrary lack of orthogonality.

set/p line=<somefile works fine in the presence of !s.

But set line=%%a doesn't.

Makes me almost give up and view delayed expansion with mistrust, and instead of

echo !line!

you can do

call echo %%line%%

and still get the same delayed expansion behaviour, without actually enabling delayed expansion.

Report •

December 2, 2009 at 08:20:33
No worries Klint!

Typical, isn't it, yet another arbitrary lack of orthogonality.

I think this is the "fun factor" of batch scripts. As far as text parsing goes in batch script, I would say it is near impossible to close down every point of failure that could arise with arbitrary data.

To deal with the exclamation marks it is possible to use this trick:

rem just to ensure delayed expansion isn't enabled
setlocal disabledelayedexpansion

for /f "delims=" %%a in (somefile) do (
    set line=%%a
    echo %%a : Before delayed expansion
    setlocal enabledelayedexpansion
    echo %%a : After delayed expansion
    echo !line! : Contents of line

But you will loose all of the vars set/changed each iteration with endlocal. It's also possible to use "setlocal disab..." but it can easily hit a recursion limit.

"you can do

call echo %%line%%"

Yep, you can also nest variables that way, I've got a whole section on it in my how to.

Batch Variable how to

Report •

Ask Question