Little bird
Little bird

Reputation: 1088

how to get the path of a file and set into the variable

As with the following code I can be able to search and get the path of a folder in any of the C drive or D drive.Suppose i'm having a file i.e psqlwithdata.dump at unknown location in any of the drive of C or D drive then how can i get the path of i.e psqlwithdata.dump in the C or D drive ?

@echo off
for /f "delims=" %%a in (' dir "c:\demo22112" /b /s /ad ') do if /i "%%~nxa"=="demo22112" set "folder=%%a"
if not defined folder for /f "delims=" %%a in (' dir "d:\demo22112" /b /s /ad ') do if /i "%%~nxa"=="demo22112" set "folder=%%a"
echo "%folder%"
pause

Upvotes: 0

Views: 54

Answers (1)

Magoo
Magoo

Reputation: 79982

@echo OFF
SETLOCAL
SET "folder="
FOR /r "C:\" %%a IN (psqlwithdata.dump) do IF EXIST "%%a" SET "folder=%%~dpa"&GOTO got1
FOR /r "D:\" %%a IN (psqlwithdata.dump) do IF EXIST "%%a" SET "folder=%%~dpa"&GOTO got1
:got1
echo "%folder%"
GOTO :EOF

This should fild your file for you.

Upvotes: 2

Related Questions