2017-01-09 56 views
-2

我有多個.pdf位於一個目錄中的文件。基於文件名和目錄創建文本文件 - 批次

目錄:測試score_test分數_9999999999_smith.pdf`文件名稱的示例。

我需要解析的文件名是:9999999999|test score|smith|||||||

而且我還需要打印文件的位置到一個文本文件開頭的@符號。

所以它看起來像這樣的文本文件:@D:\TEMP\test score_test score _9999999999_smith.pdf

在文本文件中的文件輸出爲:

9999999999|test score|smith||||||| 
@D:\TEMP\test score_test score _9999999999_smith.pdf 

我只有去儘可能輸出目錄和文件名稱到一個文本文件。

現在我只是在將@添加到目錄/文件名的開頭。

dir /s/b *.pdf > C:\temp\test.txt 

我已經試過這樣:

@echo off 
set [email protected] 
dir *.pdf /b /s DO ECHO %var1%*.pdf > C:\temp\test.txt 

我已經試過這樣:

dir *.pdf /b /s ren *.pdf  "@"????????????????????????????????????????????????????????.pdf >>  C:\temp\test.txt 
+0

問題問我們爲**推薦或找到一本書,工具,軟件庫,教程或其他非現場資源**,因爲它們傾向於吸引自以爲是的答案和垃圾郵件,因此無法用於Stack Overflow。相反,[描述問題](http://meta.stackoverflow.com/questions/254393)以及迄今爲止已經做了什麼來解決它。 – nyedidikeke

+0

命令'for' resp。 'for/f'可以做你想做的事。你的問題有點含糊 - 兩個字符串'測試分數'是否總是相等,如果不是,哪一個可以選擇輸出? – LotPings

+0

謝謝。測試分數並不總是相同的。我很抱歉,我需要從文件名中提取第一個_之後的第二個測試分數或名稱。 – user7395572

回答

0
@Echo off&SetLocal EnableExtensions 
PushD "C:\path\to\your\pdffolder" 
Set "FileOut=X:\path\to\LogFile.txt" 
:: initially clear Logfile 
Type Nul >"%FileOut%" 
:: first for enumerates the pdfs, second for /f parses the name 
:: to tokens %%A .. %%D delimited by underscore 
For %%F in (*.pdf) do For /f "tokens=1-4 delims=_" %%A in ("%%~nF" 
) Do (
>>"%FileOut%" Echo:%%C^|%%B^|%%D^|^|^|^|^|^|^| 
>>"%FileOut%" Echo:@%%F 
) 

樣品LOGFILE.TXT

8888888888|test score |miller||||||| 
@test score_test score _8888888888_miller.pdf 
9999999999|test score |smith||||||| 
@test score_test score _9999999999_smith.pdf 
7777777777|test score |baker||||||| 
@test score_test score _7777777777_baker.pdf 
相關問題