这个批处理脚本有什么问题?我只能获得无穷无尽的名单

时间:2013-02-27 12:21:15

标签: batch-file

以下情况: 我有一个140个协议文件的文件夹,格式如下(我审查了一些)

    LÖSCHPROTOKOLL

HDShredder 3.2
licenses for
%Corporation%
%address%
%address%

Drive: %numbers and letters%
Controller: IDE #0
Kanal: Primary 
Gerät: Master   
LUN: N/A
serial number: %numbers and letters% 
Größe: 78165360 Sektoren (38166 MB)   

HDShredder gestartet am 2.01.2012 um 14:48:46
Verwendete Löschmethode: %deletion methode% (Standard)

Durchlauf 1 von 3 gestartet um 14:48:46
Lösche
Muster: FFFFFFFF
Durchlauf 2 von 3 gestartet um 15:02:34
Lösche
Muster: 00000000
Durchlauf 3 von 3 gestartet um 15:16:23
Lösche
Muster: 55555555
Beendet am 2.01.2012 um 15:30:12

END OF FILE

然而,这些文件的名称类似于SHRED001.LOG SHRED002.LOG等,我喜欢用序列号重命名它们。

这是我到目前为止所尝试的

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

echo test


for %%d in (C:\Protokolle\protokolle\SHRED*) do (
    for /F "skip=13 eol=; delims=?" %%i in (%%d) do (
      echo %%i >> C:\Protokolle\protokolle\liste.txt    
      echo %%i
    )
)

当它完成时,它应该重命名上面提到的文件(尚未实现)并收集外部文件中的所有数字liste.txt

但是当我启动批处理时,我得到的是一个无休止的文本文件,从一个序列号开始,所有文本文件的内容组合在一起。

希望有人有个主意。 问候:)

2 个答案:

答案 0 :(得分:3)

您的内部for循环for /F "skip=13 eol=; delims=?" %%i in (%%d) do (将迭代在%%d给出的文件中所有行,省略前13行(reference)。这就是为什么输出看起来像你描述它。如果我理解正确,您只想解析以字符串serial number:开头的行 - 您可以使用FINDSTRreference)来实现此目的。我试着编写脚本,包括重命名你:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

FOR %%d IN ("C:\Protokolle\protokolle\SHRED*") do (
    FOR /F "tokens=2 delims=:" %%i IN ('FINDSTR /c:"serial number" "%%d"') DO (
       ECHO %%i
       :: copy string for string manipulation
       SET tmp=%%i
       :: get rid of leading space
       SET serial=!tmp: =!
       :: log serial to list file
       ECHO !serial! >> "C:\Protokolle\protokolle\liste.txt"
       :: rename file
       MOVE "%%d" "C:\Protokolle\protokolle\SHRED!serial!.log"
    )
)

答案 1 :(得分:0)

这是完成的工作脚本,感谢您的帮助:)

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET /A files=0
SET /A todo=0

::  Checking wheather a log-file exists
::  If not: Error message and exit.
IF NOT EXIST .\SHRED* (
    ECHO No matching logfile found.
    ECHO Exit!
    ECHO.
    PAUSE
    EXIT
)

::for the "loading Screen"
@FOR %%e in (".\SHRED*") do set /A files=files+1
ECHO !files! Files found!
:: waiting... this way we avoid the need of pressing a button
PING 127.0.0.1 -n 3 > NUL

FOR %%d IN (".\SHRED*") do (
    ::  Checking for a "Seriennummer"-String and copying everything after it
    FOR /F "tokens=2 delims=:" %%i IN ('FINDSTR /c:"serial number" "%%d"') DO (
        CLS
        ::    for the "loading Screen"
        SET  /A todo=todo+1
        ::   copy string for string manipulation
        SET  tmp=%%i
        ::   get rid of leading space
        SET  serial=!tmp: =!
        ::   delete the last character of the string
        SET  serial=!serial:~0,-1!
        ::   ECHOing of the serial and the "loading screen"
        ECHO !serial! !todo! von !files!
        ::   log serial to list file
        ECHO !serial!>> ".\1Uebersicht.txt"
        ::   rename file
        MOVE "%%d" ".\!serial!.log"
    )
)

::  checking if there is still a log-file left
IF EXIST ".\SHRED*" (
    ECHO An error occoured.
    ECHO Not all log-files were renamed
    ECHO.
    PAUSE
    EXIT
)

CLS
ECHO !files! files renamed.
ECHO overview list created.
ECHO please refresh the directory
ECHO.
PAUSE