Kevin Choi
Kevin Choi

Reputation: 21

Path Batch File Windows Command Line

I'm trying to create a batch file that sets the path of a directory in front or adds it to the back of the path from the default. To put the directory in the front, you put a /f or /F, and to add it to the back, you don't include the /f or /F. Every time I use sample directories it just puts it in the front when I use either /f in the front, or without it. Please help me with corrections. Thanks.

@ECHO OFF
ECHO Press Y or y for an explanation on how to use this file.
ECHO Press N or n to continue without help.
REPLY64
:TOP
IF "%1"=="" GOTO DEFAULT
IF NOT "%1"=="" GOTO ADD
IF NOT "%1"=="" GOTO ADD
IF "%1"=="/f" GOTO FRONT
IF "%1"=="/F" GOTO FRONT
IF ERRORLEVEL 121 GOTO F1
IF ERRORLEVEL 110 GOTO TOP
IF ERRORLEVEL 89 GOTO F1
IF ERRORLEVEL 78 GOTO TOP
:F1
ECHO SETPATH /F subdirectory1 subdirectory 2 or SETPATH /f subdirectory1   subdirectory 2.
ECHO The /F or /f tells the batch file to add the subdirectory name in front of the existing path.
ECHO If the command is keyed in without /F or /f, each subdirectory will be added to the end.
GOTO END
:ADD
IF "%1"=="" GOTO END
PATH= %PATH%;%1
SHIFT
GOTO ADD
:FRONT
SHIFT
IF "%1"=="" GOTO END
PATH=%1;%PATH%
GOTO FRONT
:DEFAULT
CALL \MYPATH.BAT
:END

Upvotes: 0

Views: 429

Answers (3)

foxidrive
foxidrive

Reputation: 41224

This will do what you describe. If /f is used it will add %2 to the front of the path otherwise it will add %1 to the end of the path.

@echo off
if /i "%~1"=="/f" (
set path=%~2;%path%
) else (
set path=%path%;%~1
)

Upvotes: 0

Magoo
Magoo

Reputation: 79982

I can make neither head not tail of this question.

Waht is reply64? No indication. Perhaps it sets errorlevel to something.

Then let's look at these three lines:

IF "%1"=="" GOTO DEFAULT
IF NOT "%1"=="" GOTO ADD
IF NOT "%1"=="" GOTO ADD

Now - if the first parameter doesn't exist, go to :default. Fine. But what does that do? CALL c:\mypath.bat - which is what? This file? Assuming it is, then that will re-execute the batch with no parameter.

And suppose the first parameter exists? The next two lines are identical and should therefore go to ADD - which should add the parameter at the BACK (end) of the line, not the FRONT (beginning of the line - as implied by the coding.) Yet the report is that the parameter is being added at the front. No way that it can reach that code - given what we have posted here.

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881103

IF NOT "%1"=="" GOTO ADD
IF NOT "%1"=="" GOTO ADD
IF "%1"=="/f" GOTO FRONT
IF "%1"=="/F" GOTO FRONT

Those first two should also have /F and /f in them. Otherwise it makes no sense.

Additionally, you should move the errorlevel checks to before the argument checks.

Upvotes: 0

Related Questions