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
How to assign group values from regexp to multiple external DOS batch variables?
Moderator: DosItHelp
Re: How to assign group values from regexp to multiple external DOS batch variables?
This may help you (but it only works for the $1 to $9):
Note the slightly different regular expression.
Source of "dpsm.bat":
penpen
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
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
Re: How to assign group values from regexp to multiple external DOS batch variables?
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.
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.
Dave Benham
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
Re: How to assign group values from regexp to multiple external DOS batch variables?
Did you blokes see any code where it says there should be some
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 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.
Re: How to assign group values from regexp to multiple external DOS batch variables?
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
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