Little bird
Little bird

Reputation: 1088

How to get the path and set into the variable

Basically i'm using an installer to form an exe file i.e retry.exe . This retry.exe file consists of a folder named demo which consists of some files. During installation or double clicking to i.e retry.exe to a machine i have an option to browse the folder in any of my desirable location i.e i can browse to C folder or D folder of windows 7 O.S system and install my demo folder. So how can i get the path of demo folder and set into a variable using a batch file script ? After googling ,i came to know that pushd %~dp0 may work but dont have much idea of it though i'm very new to batch file scripting. Any answers will be highly appriciated.

Upvotes: 0

Views: 131

Answers (2)

foxidrive
foxidrive

Reputation: 41234

If the batch file is running from the demo folder then %cd% will return the full path of the folder it is in.

With your added info, this should work but can take a long time though. This searches drive C: and if the folder is not found it searches drive D:

@echo off
set "folder="
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: 1

LS_ᴅᴇᴠ
LS_ᴅᴇᴠ

Reputation: 11151

You need to ask user for a folder. There is no standard way of doing such with batch.

Some options:

1) Ask user to type directory and check it:

SET /P "Directory=Enter desired directory: "
IF NOT EXIST "%Directory%\" GOTO Not_Directory_Error

2) Use to browse for a directory and returning it to batch.

Upvotes: 0

Related Questions