2014-04-18 92 views
1

我有一個問題。我有很多文本文件(.txt)。我想寫在他們內部的相應文本文件的名稱,並最終合併它們。我有很多。我不能手動完成它,它太耗時。如何使用bat將文本文件的名稱寫入文本文件?

想,我有一個名爲「windows.txt」的文本文件,其內容是

Windows XP 
Windows 7 
Windows 8 

我希望自己的內容是

windows 
Windows XP 
Windows 7 
Windows 8 

合併相似的文件後,他們應該像

windows 
Windows XP 
Windows 7 
Windows 8 

continents 
Europe 
Asia 
Africa 

languages 
English 
Chinese 

請幫助!

+0

'type * .txt'> final.txt做你想做的事情嗎? – Stephan

+0

上述命令只合並它們。我還需要其中的相應.txt文件的名稱 – pnkjmndhl

+0

@npocmaka向您展示瞭如何將文件名包含到生成的文件中。你真的需要將文件名寫入單個文件嗎? – Stephan

回答

0
@echo off 
echo. >result 
for /f %%i in ('dir /b /a-d *.txt') do (
echo. 
echo %%~ni 
type %%i 
)>>result 
ren result result.txt 

更好理解的Pseude-Code:

turn echo off 
delete result file 
for all txt files in current directory do: 
print empty line 
print name of textfile without extension 
print textfile 
and put it into new file "result" 
if done, rename the resulting file 
+0

我應該怎麼做才能打印像** $$ **這樣的常量字符串**文本文件的名稱** 例如。 ** $$ name ** – pnkjmndhl

+0

找到該行,寫入文件名(它是'echo %%〜ni')並添加您的字符串:'echo $$ %%〜ni' – Stephan

0

也許是太容易,但我認爲這是有效的答案:

type *.txt >result 2>&1 
ren result result.txt 

的技巧是當type用於文件或通配符的列表它打印的文件名錯誤stream.And它不是重定向到文件與txt擴展名以避免它的打印,但在最後重新命名。

有關寫入其中的文件的名稱(更好的測試第一)

@echo off 
for %%a in (*.txt) do ( 
type %%~na.txt* >%%~na 2>&1 
del /q /f %%~na.txt 
ren %%~na %%~na.txt 
) 

編輯

可能會工作,如果8.3符號已啓用:

@echo off 
ren ????????.txt ???????? >nul 2>&1 
for /f "tokens=* delims=" %%# in ('for %%a in (????????^) do @echo %%a') do ( 

echo %%#>%%~#_ 
rem echo %%~n#_ 
type %%~n# >>"%%~#_" 2>nul 
del /q /f "%%~n#" 
ren "%%~n#_" "%%~n#.txt" 
) 
+0

它完全工作,但文件的名稱後有兩個返回(輸入)。並且請在文件的打印名稱後刪除.txt – pnkjmndhl

+0

使用bat文件創建沒有文件 – pnkjmndhl

+0

@ user3548453檢查我的編輯 – npocmaka

相關問題