Operating System - Microsoft
1751701 Members
4900 Online
108781 Solutions
New Discussion юеВ

batch file for finding full path of a file

 
prajith
New Member

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 3
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