user2278681
user2278681

Reputation: 3

Find a substring (filepath) from a string containing variable order arguments

%params% contains a variable set of arguments:

/tidy /log /truncate /convert D:\libdir or maybe

/log /tidy D:\cyclea\libfolder /test /convert /truncate

for everything but the (currently single) filepath element I use it such:

 if "%params%"=="%params:log=%" goto :DontLogit

   if NOT "%params%"=="%params:/tidy=%" (call tidysub: & do something else )

Now I want to extract the filepath element and use it as an argument to a command eg chdir

I've played with, but I'm weak with CMD string manipulation and for loops.

I'd like to keep the order of params variable. For info it comes from here:

   FOR %%s IN (%*) DO (set params=!params! %%s)

Upvotes: 0

Views: 300

Answers (2)

Aacini
Aacini

Reputation: 67236

@echo off
setlocal EnableDelayedExpansion

rem Get the single filepath element (with colon in second character):
set params=/tidy /log /truncate /convert D:\libdir
set filepath=
for %%a in (%params%) do (
   set par=%%a
   if "!par:~1,1!" == ":" (
      set filepath=%%a
   )
)
if defined filepath (
   echo Filepath = %filepath%
) else (
   echo Filepath not given
)
echo/

rem Get multiple filepath elements in an *array*:
set params=/log /tidy D:\cyclea\libfolder /test /convert D:\libdir /truncate
set i=0
for %%a in (%params%) do (
   set par=%%a
   if "!par:~1,1!" == ":" (
      set /A i+=1
      set filepath[!i!]=%%a
   )
)
echo There are %i% filepath elements:
for /L %%i in (1,1,%i%) do (
   echo %%i- !filepath[%%i]!
)

You may review a further description on array management at this post: Arrays, linked lists and other data structures in cmd.exe (batch) script

Upvotes: 0

Magoo
Magoo

Reputation: 80123

@ECHO OFF
SETLOCAL
SET swparams=log tidy test convert truncate
FOR %%i IN (%swparams% other) DO SET "%%i="
FOR %%i IN (%*) DO (
  SET "used="
  FOR %%p IN (%swparams%) DO (IF /i "/%%p"=="%%~i" SET %%p=Y&SET used=Y)
  IF NOT DEFINED used CALL SET other=%%other%% "%%~i"
  )

ECHO =============paramsreport===========
FOR %%i IN (%swparams%) DO IF DEFINED %%i (ECHO %%i:set) ELSE (ECHO %%i:clear)
ECHO other=%other%
FOR %%i IN (%other%) DO ECHO %%i or %%~i
GOTO :EOF 

Here's a way that should be extensible for you.

Simply set you switch-parameters into the list in swparams.

the parameter-names and OTHER are set to [nothing] to ensure they're not already set in the environment. Ech supplied parameter is applied to %%i in turn, and matched against each defined swparam in turn. the variable USED is cleared before the match and if the match (of /switchparametername is found, the switch parameter is set and the USED flag is set. if the used flag is not set gainst any of the switch parameters, then a parsing trick is used to accumulate any unrecognised strings into OTHER

The "%%~i" mechanism first dequotes the item in %%i, then quotes it. In this way, it ends up quoted, regardless of whether it originally has quotes or not.

The /i on the if performs a case-insensitive match.

hence running this batch

thisbatch /tidy "C:\some filename with spaces.txt"

will yield TIDY set to Y, LOG,test, convert, truncate not set and other set to "C:\some filename with spaces.txt"

Upvotes: 1

Related Questions