在 for / findstr 循环中去除引号

问题描述 投票:0回答:1

我想我死了,已经进入蝙蝠文件地狱了。

我正在尝试处理如下所示的文本文件:

"N" "accounts" "General" no "0001"
"N" "accounts" "Customer" no "0002" 
and so on

批处理文件如下所示:

setlocal EnableDelayedExpansion 

set TGT=schctrl
set TMP=schctrl.tmp

copy %TGT% %TMP%

set NWOV=accounts inventory generalledger headoffice

for %%a in (%NWOV%) do (
   
   for /f "tokens=2,5 delims= " %%D in ('findstr /i %%a %TMP%') do (
      set M=%%E
      set M=%M:"=%
      echo %%D %%E %M%
   )
)

我期待这里的 set 语句: set M=%M:"=% 去掉所有引号(它没有),当我运行它时,我得到这样的输出:

set M="1148"
set M=!M:"=!
echo "accounts" "1148"

我期待最后一个像这样:“accounts”“1148”1148,但最后一个字段丢失了。

我猜罪魁祸首是 for 循环和变量扩展,但我一直在努力解决这个问题,但尚未找到解决方案。

温柔点 - Windows /DOS 不是我的强项

蒂亚

奈杰尔。

for-loop batch-file findstr
1个回答
0
投票

当您打开

cmd
并运行
for /?
时。向下滚动到
Substitution and read the first line: [![enter image description here][1]][1] Therefore 
%%E
will contain quotes and
%%~E` 部分不包含引号。因此,只需更改代码中的元变量即可:

@echo off

set "TGT=schctrl"
set "TMP=schctrl.tmp"

copy "%TGT%" "%TMP%"

set "NWOV=accounts inventory generalledger headoffice"

for %%a in (%NWOV%) do (
   for /f "tokens=2,5 delims= " %%D in ('findstr /i %%a %TMP%') do (
      echo %%D %%~E %%E
   )
)
© www.soinside.com 2019 - 2024. All rights reserved.