Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Operating System - Microsoft
cancel
Showing results for 
Search instead for 
Did you mean: 

batch file for finding full path of a file

prajith
Occasional Visitor

batch file for finding full path of a file

Hi experts,

I am new to batch file scripting.can any body help me to create a batch file which accepts a file name from the user.this file name has to be searched in the computer and the full path should be returned.if the file doesnt exist it should display an error.

awaiting your reply
thanx in advance

3 REPLIES
Roger Faucher
Honored Contributor

Re: batch file for finding full path of a file

I don't know how to convert this into a batch file but this is how it's done from the Command Prompt:

CD\
DIR /s

Make a great day!

Roger
Make a great day!

Roger
Edgar Zapata
Esteemed Contributor

Re: batch file for finding full path of a file

Hi Prajith,

@echo off
cd \
set /p fileName="Insert file name to be searched.: "
title searching file
echo.
dir %filename% /s
pause


Note: if the file has blanks between words, users must use ""
Example:

Insert file name: "NCRM TaskForce Minutes 2006_11_15.doc"

hope that will do.

Tnks.
Hugo Tigre
Trusted Contributor

Re: batch file for finding full path of a file

hi prajith,

You can run this bath like this example:

FilePath.bat filename

If the file file as spaces between you have to use "" like for example:

FilePath.bat "file name.txt"

Note: All files found will be outputted in the end, and the last or only file found will be saved in the variable %filepath" so you can used it latter or even used it on another script.

***copy the following code to a text file, and change the extension to .bat or .cmd.***

::----------------------------------------
@echo off
set filename=%1
set filenamePath="%userprofile%\desktop\filenamePath.txt"

c:
cd\

dir %filename% /s/b | findstr %filename% > %filenamePath%

for /f "usebackq tokens=1 delims=" %%i in (%filenamePath%) do (
set filePath=%%i
)


type %filenamepath%
del %filenamePath%
::------------------------------------------

I don't really know why you need this, since a "dir /s/a filename" will find the file you need, but here it is anyway.

Hope this helps,
best regards,
Hugo Tigre