Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am new to writing batch file.I have written a .bat file which searches for the given file
FOR %%D IN (C: D: E:) DO DIR %%D"MY FILE" /B /P /S
and now i need to save the output of this command . Since the file may be present in more than one loaction.I may get many paths and i need to sore only the first path and second path
Can i know how is this done?
Thanks in advance

Hi, In DOS type Dir /?, Enter. You will get the instructions for the directory command. I use a bat file I call Whereis.Bat it contains the following line:
Dir Path/filename /o /p /b /s
Substitute the Path and Filename you are interested in. It searches the entire drive. In a bat file Path and Filename can be %1 and %2.
You could also just try the line at the command prompt.
This may not be exactly what you want, but should get you started.
Good Luck, Jim

Hi again Ashabb, My previous post is way off of what you want. I will analyze your FOR and the one in my Bat and see if I can adapt yours to work.
Jim

It's not obvious why you'd use /p which is 'pause' or 'page'.
I don't know how to get two lines.
This will save the output to a file:
for %%A in (s:) do dir /s/b %%A\ > filelist
=====================================
If at first you don't succeed, you're about average.M2

Hi again Ahsabb,
The folowing is additions I have made to your FOR statement and batch file:@echo off
clsif exist anyfile.txt del anyfile.txt
for %%D in (C: D: E:) do dir %%D\%1 /b /p /o >> anyfile.txt
The @echo off and cls is to prevent some of the screen clutter when you run the bat. The if exist is to remove the old file to make way for the new one or you will just keep filling up the file. The filename to find is contained in the %1 parameter as in Whereis.bat Finename.ext. Note the new \ to make a correct path and filename. Mech is correct you really don't need the /p, but if you have Dircmd set to always page and sort you will need /-p to prevent the bat file from hanging up after each page and forcing you to keep hitting a key to get the bat to finish. I added the /o to sort the files alphabetically.
Good Luck, Jim

Hi Ahsabb, I forgot to mention that I haven't solved the need for only the first two occurences of the file in a batch file. I think I would probable write that one in Qbasic or Visual Basic, count each time the file was found in each drive and end the drive access when both are in.
Jim

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

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