我有转换请求,需要转换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.
以下是我要转换文件的步骤:
所以最终的结果应该是:
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.
)
最后一步是在最后一步之后删除所有内容但是我没有找到任何内容。
答案 0 :(得分:1)
我建议您使用完全不同的方法吗?
@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\