How to include spaces after a token in batch scrip

2019-08-28 07:47发布

I'm using a repl.bat to find and replace files. Here's my code

for /f "tokens=1,2" %%s in (FindNReplace.txt) do (

    Type C:\user\linefeed\%%a  | C:\comp\bat\repl.bat "\%%s" "%%t" X > C:\user\out\%%a 
  )

Now in my FindNReplace.txt, I have the sample contents:

AAAAA\d{3}      AAAAA  

Basically all string containing AAAAA+3 bytes will be replaced by AAAAA. Now what if I want to include 3 bytes of spaces for my string to use as a replacement? "AAAAA+3 spaces" Can I include special character in the second column of FindNReplace.txt?

1条回答
叛逆
2楼-- · 2019-08-28 07:48

If you want to include the spaces in the second column in your .txt file, you need to indicate to the for command that these spaces should not be considered a delimiter. Simply indicate that the tokens to retrieve are the first element in the line and that the second token will be all the remaining data in the line.

for /f "tokens=1,*" %%s in (FindNReplace.txt) do (
    Type C:\user\linefeed\%%a  | C:\comp\bat\repl.bat "\%%s" "%%t" X > C:\user\out\%%a 
)

And, of course, include the three spaces after the value in the second colum.

edited to adapt to comments

copy "C:\user\linefeed\%%a" "C:\user\linefeed\%%a.in.tmp"
for /f "tokens=1,*" %%s in (FindNReplace.txt) do (
    Type "C:\user\linefeed\%%a.in.tmp"  | C:\comp\bat\repl.bat "\%%s" "%%t" X > "C:\user\linefeed\%%a.out.tmp"
    move /y "C:\user\linefeed\%%a.out.tmp" "C:\user\linefeed\%%a.in.tmp"
)
move "C:\user\linefeed\%%a.in.tmp" "C:\user\out\%%a"
查看更多
登录 后发表回答