How to assign group values from regexp to multiple external DOS batch variables?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

How to assign group values from regexp to multiple external DOS batch variables?

#1 Post by pstein » 16 Oct 2016 03:11

Assume I want to loop over a large directory tree with thousands of files.
Only those files should be picked up for further processing which match a regular expression like

(.*)\((.*).(.*)\).log
logfile for variant 34 (223.101).log
So a file like



should be matched.

Furthermore an advanced difficulty comes into play. As you noticed the regular expression contains groups
which are normally internally assigned to (temporary) variables $1, $2, $3,...
In the above example they contain:

$1 = logfile for variant 34
$2 = 223
$3 = 101

I need to access these values for further processing in a DOS batch script.

How can I access them?

Finally the DOS batch script should look like similar to:

for %I in (::(.*)\((.*).(.*)\).log) do (
set firstgroup=%I.getvar($1)
set secondgroup=%I.getvar($2)
set thirdgroup=%I.getvar($3)
)

Obviously there is no such function like .getvar().

I guess I have to use some thrid party program.

Everything is acceptable as long as I can access the interal group variables later.

Maybe the whole script could then look like

for %I in (::(.*)\((.*).(.*)\).log) do (
myextregexprgm %I "(.*)\((.*).(.*)\).log" firstgroup secondgroup thirdgroup
)

Any idea how to achieve this?

Peter

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to assign group values from regexp to multiple external DOS batch variables?

#2 Post by penpen » 16 Oct 2016 07:44

This may help you (but it only works for the $1 to $9):

Code: Select all

Z:\>set $
Die Umgebungsvariable "$" ist nicht definiert.

Z:\>for /F "tokens=* delims=" %a in ('dpsm "/text:logfile for variant 34 (223.101).log" "/regExp:(.*)\(([^.]*).(.*)\).log"') do @set "%~a"

Z:\>set $
$1=logfile for variant 34
$2=223
$3=101
Note the slightly different regular expression.

Source of "dpsm.bat":

Code: Select all

@if (true == false) @end /*
@echo off
setlocal enableDelayedExpansion enableExtensions

rem cscript //E:JScript //Nologo "%~f0"  "/text:logfile for variant 34 (223.101).log" "/regExp:(.*)\((.*).(.*)\).log"
rem cscript //E:JScript //Nologo "%~f0"  "/text:logfile for variant 34 (223.101).log" "/regExp:(.*)\(([^.]*).(.*)\).log"

cscript //E:JScript //Nologo "%~f0" %*

endlocal & exit /b %errorlevel%
*/

var regExp = WScript.Arguments.Named.Item("regExp");
var text = WScript.Arguments.Named.Item("text");

if (!regExp | !text) {
   WScript.Echo(
      "Usage:\r\n" +
      "dpsm[.bat] \"/text:<string>\" \"/regExp:<string>\"\r\n" +
      "dpsm[.bat] /text:<token> /regExp:<token>\r\n" +
      "\r\n" +
      "This utility displays parenthesized substring matches."
   );
} else if (text.match(regExp)) {
   for (var i = 1; i <= 9; ++i) eval("if (RegExp.$" + i + ") WScript.Echo(\"$" + i + "=\" + RegExp.$" + i + ");");
   WScript.Quit(0);
}
WScript.Quit(1);


penpen

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to assign group values from regexp to multiple external DOS batch variables?

#3 Post by dbenham » 16 Oct 2016 22:03

I don't see why you need a regular expression with your search. All you need is FOR /F with DIR.
Also, no need to use environment variables - FOR /F variables can be used for each value. But feel free to set variables if you really want them.

Code: Select all

@echo off
for /f "tokens=1-3 delims=(.)" %%A in (
  'dir /b /s /a-d "*(*.*).log"'
) do (
  echo(
  echo part1=%%~nxA
  echo part2=%%B
  echo part3=%%C
)


If you really want to use regular expressions, then you can pipe the output of DIR to JREPL.BAT, and transform the values into a pipe delimited list, or delimit using any other character that cannot appear in a file name. Note that your original regex is faulty - you must escape the dot literals, else they are interpreted as a wildcard. You also should consider how you want to handle names that have additional parentheses and/or dots, and modify the regex accordingly.

Code: Select all

for /f "tokens=1-3 delims=|" %%A in (
  'dir /b /s /a-d *.log^|jrepl "^(.*)\((.*)\.(.*)\)\.log$" "$1|$2|$3" /i /a'
) do (
  echo(
  echo part1=%%~nxA
  echo part2=%%B
  echo part3=%%C
)


Dave Benham

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to assign group values from regexp to multiple external DOS batch variables?

#4 Post by foxidrive » 20 Oct 2016 03:21

Did you blokes see any code where it says there should be some :?:

pstein wrote:Assume I want to loop over a large directory tree with thousands of files.
Only those files should be picked up for further processing which match a regular expression like

(.*)\((.*).(.*)\).log
logfile for variant 34 (223.101).log
So a file like



should be matched.

Furthermore an advanced difficulty comes into play.


It's been three days since the last reply, from Dave.
It really saddens me to see people ask questions and then get abducted by aliens.

I hope the Alien computers run Windows so pstein can keep fiddling with his batch file.

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: How to assign group values from regexp to multiple external DOS batch variables?

#5 Post by pstein » 20 Oct 2016 03:30

Yes, I saw the suggestions of solutions.

However since I wrote only a simplified scenario of my original problem I need time to adjust it accordingly.

I am pretty surprised that this should work but I am optimistic.
I don't forget ths Thread and appreciate your help.

So thank you for now.
I will revert and write a comment in a while.

Peter

Post Reply