批处理 - 根据内容

时间:2017-11-06 17:00:23

标签: windows batch-file

我的所有文件都位于同一个文件夹中,并且都是txt文件 我的文件看起来像这样:

CPSITE=00009041695
UID=W1KRVWHTSNRD,W1KRVULRRGPJ,
W1KRVWMTVNOJ,W1KRQUDTRMLK,W1KRVSHRUHTI,0 

或者像这样:

CPSITE=00009041695 
UID=0 

我希望实现的目标是创建一个文件,其中所有id W1K....列在另一个之上。每个文件可以包含0和无限数量的ID (简称1000

这是我迄今为止所能做到的:

第一个版本

SETLOCAL ENABLEDELAYEDEXPANSION
rem parsing all files of the folder
for %%f in (*.txt) do (
set "filename=%%f"
echo Traitement du fichier !filename!
rem for each line of the current file
for /f "delims=" %%a in (!filename!) do (
    SET s=%%a
    rem replace , with ; 
    SET s=!s:,=;!
    remstock line in a new file
    echo !s! >> v2!filename!
)
rem then finally replace old file with new one
mv v2!filename! !filename!
)

使用这是我的第一个想法,我只想使用new line character替换,,但我无法做到这一点,这也会导致线路出现问题以",#34;结尾因为它会创建第二条新线。

接下来的方法:

SETLOCAL ENABLEDELAYEDEXPANSION
rem parse file
for %%f in (*.txt) do (
    set "filename=%%f"
    rem for each file, removing first line as it will never be used
    for /f "skip=1 delims=" %%a in (!filename!) do (
        SET s=%%a
        rem if there is a coma in the line, the line contains at least one ID
        If NOT "!s!"=="!s:,=!" (
            echo , found in !s!
            rem this is where I struggle, this for what I understand is supposed to print all occurences of string delimited by "," but it only show the first one and then stops
            for /F "tokens=* delims=," %%a in ("!s!") do (
                echo id value: %%a
            )
        )
    )
)

我认为这里最好的解决方案是将所有找到的ID添加到新文件中,每次都会转到新行,但我无法执行此操作。

我期望从上面的例子中得到什么:

W1KRVWHTSNRD
W1KRVULRRGPJ
W1KRVWMTVNOJ
W1KRQUDTRMLK
W1KRVSHRUHTI

0 个答案:

没有答案