Solved search a folder for a text file

March 8, 2012 at 14:03:18
Specs: Win 7 Pro, 4.17Ghz 8GB ram
I have a file that I need to call upon. It can be in multiple different folders

ex i need to find test.txt in a folder called batch

but text.txt may be in batch\test\hello\text.txt

So i need to search all subdirectories as well.

Any help woud be greatly appreciated.


See More: search a folder for a text file

Report •


#1
March 9, 2012 at 00:10:00
FOR /R \batch %%i IN (.) DO  IF EXIST test.txt  SET Dir_Found=%%i 

If test.txt exists in multiple subfolders and you need to do something with each one, then use a compound statement after the DO.

Report •

#2
March 9, 2012 at 13:39:49
Its not working quite right

test.txt:
@echo off
FOR /R \batch %%i IN (.) DO IF EXIST test.txt SET Dir_Found=%%i
echo %Dir_Found%
pause

i get an error saying echo is turned off

When i put it in the same place that the text file is in it will tell me the directory.

I have also tried putting "" around the path


any other suggestions?


Report •

#3
March 9, 2012 at 16:03:23
✔ Best Answer
Oops! A tweak is needed. This worked for me:

@echo off
SET Dir_Found=
FOR /R \batch %%i IN (.) DO IF EXIST %%i\test.txt SET Dir_Found=%%i 
IF "%Dir_Found%"==""  echo Not Found
IF NOT "%Dir_Found%"==""  echo The full filespec is "%Dir_Found%\test.txt"
pause

It is necessary with the (.) set to tell the IF where to look on each itteration. It was always looking in the current, and never finding it there.

Clearing Dir_Found before the FOR insures you don't get a false positive in the event it was already defined. %Dir_Found% ends in a ".", so the backslash makes the syntax correct.

Please enclose your code within the "pre" and "/pre" xml code tags. This makes it much easier to follow.


Report •

Related Solutions

#4
March 9, 2012 at 23:21:53
it is saying file is not found no matter where i put it now

Report •

#5
March 10, 2012 at 00:36:32
In our first try Dir_Found was never assigned because IF was always looking in the current directory and hence never finding test.txt. The next statement was then essentially just ECHO with no parameters which will tell you if echo is on or off.

If you didn't change the FOR statement, test.txt won't be found and you would get the more informative "Not Found" message. Did you notice that I added "%%i\" into the FOR?

QUOTE(reply #3):
"It is necessary with the (.) set to tell the IF where to look on each itteration. It was always looking in the current, and never finding it there."

Also, if the starting path "\batch" is instead something with a space in it like "C:\Documents and Settings" then you have to enclose that path in double quotes or the command interpreter will get confused.

I tested this under Win2000 and WinXP and it worked both places. I wouldn't expect WinVista or Win7 to be different, but I don't know. Have you messed with the environment with SETLOCAL or with CMD /E:OFF?


Report •

#6
March 10, 2012 at 17:45:22
There we go I had to change where the file is to a full path and not just a "\" folder name. But it worked with ".\". Is there any way to make it so the script does not have a period at the end? or an easy way to remove it. I know how to remove it by the length in characters. but its a varying about of characters so im kinda lost. I apologies about being a "Noob" im fairly new to batch scripting I have pretty much everything down except for statements. I know there is documentation within the command prompt but do you know a website that explains how to use it further.

Also I did notice that you added the slash and I thought that was a good idea. I have also become a nazi about making sure I clear my vars before I use them and after I use them so I do not get any false vars

I really appreciate your help.

Thank you


Report •

#7
March 10, 2012 at 18:00:30
Never mind I guess I should have tried first

This is how I did it

@echo off
SET Dir_Found=
SET Cut_Dir_Found=
FOR /R ".\" %%i IN (.) DO IF EXIST %%i\test.txt SET Dir_Found=%%i 
IF "%Dir_Found%"==""  echo Not Found
IF NOT "%Dir_Found%"==""  SET Cut_Dir_found=%Dir_Found:~0,-2%
ECHO %Cut_Dir_found%
SET Dir_Found=
SET Cut_Dir_Found=
Pause
exit

Any way this can be done with a folder instead of a text file?


Report •

#8
March 11, 2012 at 01:35:44
Unless you have purely aesthetic reasons for not wanting the dot at the end, you should not worry about it. "\batch\sub1" is the same as "\batch\sub1." is the same as "\batch\sub1.\." is the same as "\batch\sub1\." is the same as "\batch\sub1\.\.\.\.", etc. Try it with the DIR command!

"." is the current directory and ".." is the parent of the current. The dot ending is superior because you can always append a backslash (or ".\") and more path or a filespec. If your path ends in a backslash, you have to be aware of this, because you can't add another backslash.

A directory is a special type of file and may have an extension, but typically is not given one, so "\batch\sub1.." is also the same directory (the first dot is part of the filespecification).

To test existence of a path you look for it directly in the NT OS's:

IF EXIST %mypath%  SET Found=TRUE

To test existence of a path in MS-DOS you look for the NUL file:

IF EXIST %mypath%.\NUL  SET Found=TRUE

Rob van der Woude's Scripting Pages is one of the best websites:

QUOTE:
"I have also become a nazi about making sure I clear my vars before I use them"

It is also a good idea to make sure the var isn't already in use!

IF NOT "%My_Var%"==""  GOTO ERRMSGnn


Report •

#9
March 11, 2012 at 13:14:04
I think your one of the most help ful people I have ever talked to on this form. Its usually pretty good but you defiantly went above and beyond. I really thank you.

Also I had no idea nor have I ever seen on any forum about the .\NUL I will be using that kinda often.

That website looks like a really good one.

IF NOT "%My_Var%"=="" GOTO ERRMSGnn I also like that I will have to start using that in iffy situations.

Thank you again


Report •

Ask Question