
ziliao0616
[新手]
求提取txt文件第一行作为文件名的批处理命令bat
100分
回答:3 浏览:1463 提问时间:2008-12-21 14:46
我有100多本文件名为数字的小说 txt格式,文件正文第一行是 四个空格+[标题 书名]
急求批处理命令,一次修改文件名为正文第一行文字
BAT命令
急求批处理命令,一次修改文件名为正文第一行文字
BAT命令
2008-12-21 15:56 补充问题
第一行内容 [名人演讲在北大 / 和弦 著 ]
[提升你的创意能力:全新思维 / [美]丹尼尔·平克 著 ]
[提升你的创意能力:全新思维 / [美]丹尼尔·平克 著 ]
for %%i in (*.txt) do call :re_name "%%i"
pause
exit
:re_name
for /f "delims=" %%i in ('findstr /n .* %1') do (
set "str=%%i"
setlocal enabledelayedexpansion
set str=!str:*:=!
set str=!str: =!
set str=!str: =!
ren %1 !str!.txt
endlocal
goto :eof
)
以上為批處理內容,也可以自己加上“@echo off”。在打包文件內有。此批處理凡遇到特殊字符則自動跳過,所以如果你個個文件都有“/”這類不能作為文件名的字符,則不適用。
這個批處理要在txt同一個文件夾(目錄)內運行。
批處理速度比較慢。
因此我寫了一個exe程序:先選擇文件夾,然後設置一下(例如是否保留空格,是否替換諸如“/”這些字符,也可以自定義所要剔除的字符),按“開始更名”就可以了。
此程序基於delphi7寫出來,內容比較長(主要是函數),所以不貼出來了,如果你需要源程序和代碼也可以另附給你。
打包中一個叫“笑話”的文件夾(目錄),是一個測試目錄,裏面有若幹個txt文件,可以測試程序的功能。若你不明白程序使用方法,也可以拿這個目錄去試,就不用擔心你的數字小說會改錯名。
希望對你有幫助。
ps:回答問題附件居然不能超過200k,沒辦法,只有上傳到這裏讓你下載:
http://www.dadao.net/temp/geniuz/text.rar
或者到到我iask資料裏下載:
http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=4699461
pause
exit
:re_name
for /f "delims=" %%i in ('findstr /n .* %1') do (
set "str=%%i"
setlocal enabledelayedexpansion
set str=!str:*:=!
set str=!str: =!
set str=!str: =!
ren %1 !str!.txt
endlocal
goto :eof
)
以上為批處理內容,也可以自己加上“@echo off”。在打包文件內有。此批處理凡遇到特殊字符則自動跳過,所以如果你個個文件都有“/”這類不能作為文件名的字符,則不適用。
這個批處理要在txt同一個文件夾(目錄)內運行。
批處理速度比較慢。
因此我寫了一個exe程序:先選擇文件夾,然後設置一下(例如是否保留空格,是否替換諸如“/”這些字符,也可以自定義所要剔除的字符),按“開始更名”就可以了。
此程序基於delphi7寫出來,內容比較長(主要是函數),所以不貼出來了,如果你需要源程序和代碼也可以另附給你。
打包中一個叫“笑話”的文件夾(目錄),是一個測試目錄,裏面有若幹個txt文件,可以測試程序的功能。若你不明白程序使用方法,也可以拿這個目錄去試,就不用擔心你的數字小說會改錯名。
希望對你有幫助。
ps:回答問題附件居然不能超過200k,沒辦法,只有上傳到這裏讓你下載:
http://www.dadao.net/temp/geniuz/text.rar
或者到到我iask資料裏下載:
http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=4699461
回答:2008-12-26 18:53
提问者对答案的评价:









非常感谢您的帮助,改天一定和您多聊聊
Option Explicit
On Error Resume Next ' 容错语句,避免程序崩溃 '有可能重命名文件已经存在,忽略错误。
Dim fso,fs,f
Dim i
Dim strNewName
Const strCurrentPath = "."
Msgbox "根据文本文件第一行批量重命名的VBS程序" & vbcrlf & vbcrlf & "Created By Shortway",0,"QQ:380710203"
Set fso = Wscript.CreateObject("Scripting.FileSystemObject")
Set fs = fso.GetFolder(strCurrentPath).Files
i = 0
For Each f In fs '遍历当前文件夹内每个文件
If LCase(right(f.name,3))="txt" Then '判断是否是文本文件
strNewName = Trim(fso.OpenTextFile(f, 1, False).ReadLine) '得到第一行
'以下剔除不能作文件名的特殊字符
strNewName = Replace(strNewName, "\", "")
strNewName = Replace(strNewName, "/", "")
strNewName = Replace(strNewName, ":", "")
strNewName = Replace(strNewName, "*", "")
strNewName = Replace(strNewName, "?", "")
strNewName = Replace(strNewName, """", "")
strNewName = Replace(strNewName, ">", "")
strNewName = Replace(strNewName, "<", "")
strNewName = Replace(strNewName, "|", "")
strNewName = left(strNewName,50) '有时第一行文字太多了,就选50个字符了
f.name=strNewName & ".txt"
i = i + 1
End if
Next
Msgbox i & "个文件改名完成!(忽略重名)"
Set fs = Nothing
Set fso = Nothing
参考文献:Shortway 大师原创。回答是辛苦,爱问是幸福o(∩_∩)o...
附件:RenameText.vbs
On Error Resume Next ' 容错语句,避免程序崩溃 '有可能重命名文件已经存在,忽略错误。
Dim fso,fs,f
Dim i
Dim strNewName
Const strCurrentPath = "."
Msgbox "根据文本文件第一行批量重命名的VBS程序" & vbcrlf & vbcrlf & "Created By Shortway",0,"QQ:380710203"
Set fso = Wscript.CreateObject("Scripting.FileSystemObject")
Set fs = fso.GetFolder(strCurrentPath).Files
i = 0
For Each f In fs '遍历当前文件夹内每个文件
If LCase(right(f.name,3))="txt" Then '判断是否是文本文件
strNewName = Trim(fso.OpenTextFile(f, 1, False).ReadLine) '得到第一行
'以下剔除不能作文件名的特殊字符
strNewName = Replace(strNewName, "\", "")
strNewName = Replace(strNewName, "/", "")
strNewName = Replace(strNewName, ":", "")
strNewName = Replace(strNewName, "*", "")
strNewName = Replace(strNewName, "?", "")
strNewName = Replace(strNewName, """", "")
strNewName = Replace(strNewName, ">", "")
strNewName = Replace(strNewName, "<", "")
strNewName = Replace(strNewName, "|", "")
strNewName = left(strNewName,50) '有时第一行文字太多了,就选50个字符了
f.name=strNewName & ".txt"
i = i + 1
End if
Next
Msgbox i & "个文件改名完成!(忽略重名)"
Set fs = Nothing
Set fso = Nothing
参考文献:Shortway 大师原创。回答是辛苦,爱问是幸福o(∩_∩)o...
附件:RenameText.vbs
回答:2008-12-24 15:50

最佳答案


