Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch file condition question?

Status
Not open for further replies.

BobMCT

IS-IT--Management
Sep 11, 2000
756
US
I am trying to help someone create a batch file that takes a positional parameter (which specifies a drive letter) and if present checks to see if it contains a colon character. If not it appends one.

I just cannot get this to work. Here's what I have

SET V_DRIVE=%1
IF "%V_DRIVE%" == "" SET V_DRIVE=C:

ECHO %V_DRIVE% | FIND ":" > NUL
IF NOT ERRORLEVEL 1 SET V_DRIVE=%V_DRIVE%:

ECHO V_DRIVE=[%V_DRIVE%]

I've been searching for techniques all day long and I know I must be missing something simple. Can anyone PLEASE reply with just how one can achieve this?

Thanks all - B [ponder]
 
How about something like this using a for loop and the colon as one of the delimiters (the example below also has semicolon & space just because)

Code:
set V_DRIVE=%1
IF "%V_DRIVE%" == "" SET V_DRIVE=C:
for /F "tokens=1,2* delims=:; " %%i in ("%V_DRIVE%") do (
  set V_DRIVE=%%i:
  )
echo V_DRIVE=[%V_DRIVE%]
 
Also, you might try it like this:
Code:
SET V_DRIVE=%1
IF "%V_DRIVE%" == "" SET V_DRIVE=C:

echo %v_drive%>ekko.txt
find ":" ekko.txt
if errorlevel = 1 set v_drive=%v_drive%:
del ekko.txt

ECHO V_DRIVE=[%V_DRIVE%]
since I don't hink you can pipe to find
and I think error level is 1 if string is not found.

Jock

Jock
 
I've never had a problem piping to find. Try treating the errorlevel as a string variable (see below). With that change, the orginal code worked fine for me. Remember that errorlevel itself, when used with an if, "specifies a true condition if the last program run returned an exit code equal to or greater than the number specified."

Code:
SET V_DRIVE=%1
IF "%V_DRIVE%" == "" SET V_DRIVE=C:

ECHO %V_DRIVE% | FIND ":" > NUL
IF NOT "%ERRORLEVEL%"=="0" SET V_DRIVE=%V_DRIVE%:

ECHO V_DRIVE=[%V_DRIVE%]
 
Thanks all who responded. With your suggestions along with further research into those areas I was able to discovery (at least to me, a linux guy) there's a whole world of "enhanced scripting" available with XP and above. Using a lot of parameter manipulation and quite a few of the embedded modifiers I was able to achieve what I asked. And then some.

Thanks again. B
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top