批处理文件txt转换

时间:2017-04-01 00:10:03

标签: batch-file

我有转换请求,需要转换txt文件以仅显示BATCH中的区别文件夹。

所以这是一个示例文件:

zpaq v7.15 journaling archiver, compiled Aug 17 2016
zpaq_m1.zpaq: 1 versions, 14 files, 125 fragments, 4.521800 MB

- 2017-03-31 21:22:24      9025719 D     c:/2/testdata/
- 2016-09-26 13:26:07         9149 A     c:/2/testdata/test.txt
- 2017-03-31 21:22:48      9014621 D     c:/2/testdata/Subfolder/
- 2017-03-31 21:22:59        11856 D     c:/2/testdata/Subfolder/History/
- 2017-03-31 21:22:24        11856 D     c:/2/testdata/Subfolder/History/2016-03-12/
- 2016-03-11 23:31:49         1976 A     c:/2/testdata/Subfolder/History/2016-03-12/00h31m49s  v1.9.m3p
- 2016-03-11 23:36:47         1976 A     c:/2/testdata/Subfolder/History/2016-03-12/00h36m47s  v1.9.m3p
- 2016-03-11 23:37:09         1976 A     c:/2/testdata/Subfolder/History/2016-03-12/00h37m09s  v1.9.m3p
- 2016-03-11 23:38:34         1976 A     c:/2/testdata/Subfolder/History/2016-03-12/00h38m34s  v1.9.m3p
- 2016-03-11 23:40:00         1976 A     c:/2/testdata/Subfolder/History/2016-03-12/00h40m00s  v1.9.m3p
- 2016-03-11 23:40:25         1976 A     c:/2/testdata/Subfolder/History/2016-03-12/00h40m25s  v1.9.m3p
- 2016-03-11 23:39:43      9001472 A     c:/2/testdata/Subfolder/test.exe
- 2017-03-28 10:45:03         1293 A     c:/2/testdata/Subfolder/test.ini
- 2016-09-26 14:04:43         1949 A     c:/2/testdata/todo.txt

9.025719 MB of 9.025719 MB (14 files) shown
  -> 9.017815 MB (129 refs to 125 of 125 frags) after dedupe
  -> 4.521800 MB compressed.

以下是我要转换文件的步骤:

  1. 删除前三行
  2. 删除最后四行
  3. 剪下每行的前41个字母
  4. 将正斜杠替换为反斜杠
  5. 删除最后一个反斜杠后的所有内容(文件名)
  6. 区分文件,以便每个文件夹只有一次
  7. 所以最终的结果应该是:

    c:\2\testdata\
    c:\2\testdata\Subfolder\
    c:\2\testdata\Subfolder\History\
    c:\2\testdata\Subfolder\History\2016-03-12\
    

    非常感谢你的帮助

    这是我目前的状态:

    ::delete first 2 lines 
    for /f "skip=1 delims=*" %%a in (Listing.txt) do (
    echo %%a >>Listing2.txt    
    )
    
    ::delete last 3 lines
    @echo off
    setlocal enabledelayedexpansion
    set count=
    for /f %%x in ('type Listing2.txt ^| find /c /v ""') do set /a lines=%%x-3
    copy /y nul %tmp%\tmp.zzz > nul
    for /f "tokens=*" %%x in ('type Listing2.txt ^| find /v ""') do (
      set /a count=count+1
      if !count! leq %lines% echo %%x>>%tmp%\tmp.zzz
    )
    move /y %tmp%\tmp.zzz Listing2.txt > nul
    
    ::Delete last cr flag at the end of the file
    ::@echo off
    setlocal DisableDelayedExpansion
    set "firstLineReady="
    (
        for /F "eol=$ delims=" %%a in (Listing2.txt) DO (
            if defined firstLineReady (echo()
            set "firstLineReady=1"
            <nul set /p "=%%a"
        )
    ) > Listing_Final.txt
    del Listing2.txt /f /q
    
    ::take everything after line 1 und each line after character41
    for /f "skip=1 usebackq delims=" %%i in ("Listing_Final.txt") do call :ProcessLine "%%i"
    :PROCESSLINE
    set "Zeile=%~1"
    ::put everything after 41
    (echo %Zeile:~41%)>>"Listing_Final_Folder_Files_temp.txt"
    
    
    del Listing_Final.txt /f /q
    
    
    ::Remove double entries
    ::@ECHO OFF
    SETLOCAL
    :: remove variables starting $
    FOR  /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
    
    FOR /f "delims=" %%a IN (Listing_Final_Folder_Files_temp.txt) DO SET $%%a=Y
    (FOR  /F "delims=$=" %%a In ('set $ 2^>Nul') DO ECHO %%a)>Listing_Final_Folder_Files_temp2.txt
    
    ::del Listing_Final_Folder_Files_temp.txt /f /q
    

    现在我正在尝试将斜杠转换为反斜杠但是使用此代码它不起作用:

    ::Replace / to \
    echo off
    setlocal enabledelayedexpansion
    FOR %%f IN ("Listing_Final.txt") DO (
     set old=%%f
     echo !old!
     set new=!old:/=\!
     echo !new!>tesss.txt  
     echo.                 
    )
    

    最后一步是在最后一步之后删除所有内容但是我没有找到任何内容。

1 个答案:

答案 0 :(得分:1)

我建议您使用完全不同的方法吗?

  • 您可以只处理以“ - ”开头的行,而不是删除前三行和后四行。这允许处理具有更多或更少行的任何文件。
  • 您可以只处理每一行的第6个以空格分隔的标记,而不是剪切每行的前41个字符
  • 您可以获取此类令牌的驱动器和路径,而不是删除最后一个反斜杠后的所有内容。这也自动将任何正斜杠转换为反斜杠。
  • 最后,您可以使用 subscript 这样的值来定义数组元素,因此只保留具有唯一下标的元素,而不是查找和删除重复元素。

@echo off
setlocal EnableDelayedExpansion

rem Processing lines that have a dash in first column, get the sixth token
for /F "tokens=6" %%a in ('findstr "^-" Listing.txt') do (

   rem Get just the drive and path of sixth token
   rem at same time, convert Forward slash to Backslash
   rem and use it as the subscript of an array,
   rem so just unique elements are stored:

   set "folder[%%~DPa]=1"
)

rem Show the result
for /F "tokens=2 delims=[]" %%a in ('set folder[') do echo %%a

输出:

c:\2\testdata\Subfolder\History\2016-03-12\
c:\2\testdata\Subfolder\History\
c:\2\testdata\Subfolder\
c:\2\testdata\