3

I'm trying to pass through caret chars through batch. Escaping them once would be easy, but I need to do it twice. I have an executable that will back up tables based on a Regex expression (not my code). I want to back up all tables with an exclusion list. Using ^(?!tableName$).* works for a single table.

Batch File 1 (called from command line)

SET ignoreTables=tableName
:: Call the backup script
CALL SecondBatch.bat %ignoreTables%

Batch File 2

:: Passthrough ignoreTables
Executable.exe --ignoreTablesPattern="^(?!%1$).*"

But I'd like to ignore multiple tables. In Regex this means using the | (pipe) character eg; tableOne|tableTwo would require;

SET ignoreTables=tableOne^|tableTwo

Which is correct at the SET but not when passed to the CALL

The correct output that works from the command line is;

Executable.exe --ignoreTablesPattern="^(?!tableOne|tableTwo$).*"

How can I get this result out of the batch file?

Josh
  • 2,994
  • 2
  • 17
  • 22
  • Note: I would also be happy if I could use ^table to match all tables starting with "table" but then I have the same problem trying to escape the ^ caret to get it out of the first batch! – Josh Oct 17 '16 at 05:28

1 Answers1

1

In batch file 1 use:

SET "ignoreTables=tableOne|tableTwo"
:: Call the backup script
CALL SecondBatch.bat "%ignoreTables%"

And in batch file 2 use:

:: Passthrough ignoreTables
Executable.exe --ignoreTablesPattern="^(?!%~1).*$"

Run in a command prompt window cmd /? and read the output help pages, especially the last paragraph on last help page which is about when surrounding a directory/file name or parameter string with double quotes is required.

The first line of batch file 1 contains the command SET with the parameter string variable=value. By enclosing this parameter string in double quotes the pipe character is not interpreted anymore as operator. For more details about using double quotes on assigning a string to an environment variable see answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line?

The value of the environment variable is passed next expanded as first parameter to batch file 2. Again surrounding double quotes are needed to pass the string containing | as literal string to the second batch file.

In the second batch file it is necessary to reference the first argument without surrounding quotes. Therefore %~1 is used now instead of %1 as explained in help of command CALL output on running in a command prompt window call /?.

BTW: I'm quite sure $ should be at end of the regular expression and not inside the negative lookahead.

Community
  • 1
  • 1
Mofi
  • 38,783
  • 14
  • 62
  • 115
  • Thanks Mofi. I had tried quotations, but not around the whole SET. I knew it would be possible somehow. It looks easy once you know how. – Josh Oct 17 '16 at 07:13