|
venchia
初级用户
 
积分 125
发帖 11
注册 2004-1-25
状态 离线
|
『楼 主』:
[求助]文本文件批量改名的问题
使用 LLM 解释/回答一下
比如在某文件夹有若干文本文件,想在想提取这些文本文件的前两行,并组合更改文本文件的名字,不知道要怎么做呢
比如有这个文件
魔斗士.txt
文件的内容如下
魔斗士
作者:徐奇峰
【内容简介】
身怀一身精湛武艺的张如飞,在一次醉酒之后意外与来自K星球的不明飞行器打了照面,并意外获得了有“南斗六星主生”功效的六粒丹药,此丹药能让受到意外伤害死亡的人起死回生并很快恢复到死前的状态。
从此,张如飞走上了不平凡的道路。
文件前面都有4个半角字符,不过由于在这里会变形,所以用了两个全角字符代替
现在想提取文件的第一行和第二行,组合成
《魔斗士》作者:徐奇峰.txt
这样的形式,不知道能做到吗,谢谢
For example, there are several text files in a certain folder. I want to extract the first two lines of these text files and combine them to change the names of the text files. I don't know how to do it.
For example, there is this file
Moshidou.txt
The content of the file is as follows
Moshidou
Author: Xu Qifeng
【Content Introduction】
Zhang Rufei, who is proficient in martial arts, accidentally came into contact with an unidentified flying object from planet K after a drunk, and accidentally obtained six pills with the effect of "the six stars of the Southern Dipper master life". These pills can bring back to life people who have died from accidental injuries and quickly restore them to the state before death.
From then on, Zhang Rufei embarked on an extraordinary path.
There are 4 half-width characters in front of the file, but since it will be deformed here, two full-width characters are used instead
Now I want to extract the first line and the second line of the file and combine them into the form "《Moshidou》Author: Xu Qifeng.txt". I don't know if it can be done. Thank you
|
|
2007-7-14 15:23 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
试试这个:
@echo off&setlocal enabledelayedexpansion
for /f "tokens=1* delims=:" %%i in ('findstr /n .* a.txt') do (
set var=!var!%%j
set var=!var: =!
if %%i equ 2 ren a.txt !var!.txt
)
Try this:
@echo off&setlocal enabledelayedexpansion
for /f "tokens=1* delims=:" %%i in ('findstr /n .* a.txt') do (
set var=!var!%%j
set var=!var: =!
if %%i equ 2 ren a.txt !var!.txt
)
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2007-7-14 16:12 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
呵呵,加上书名号:
@echo off&setlocal enabledelayedexpansion
for /f "tokens=1* delims=:" %%i in ('findstr /n .* a.txt') do (
set var=《!var!》%%j
set var=!var: =!
set var=!var:《》=!
if %%i equ 2 ren a.txt !var!.txt
)
Hehe, add the book title marks:
@echo off&setlocal enabledelayedexpansion
for /f "tokens=1* delims=:" %%i in ('findstr /n .* a.txt') do (
set var=《!var!》%%j
set var=!var: =!
set var=!var:《》=!
if %%i equ 2 ren a.txt !var!.txt
)
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2007-7-14 16:15 |
|
|
venchia
初级用户
 
积分 125
发帖 11
注册 2004-1-25
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
我是要批量的,不是单个的
还有,就算用a.txt也不行,我用下面附件的几个文件测试失败
比如下面的几个文件是想一次性的改名为
《魔斗士》作者:徐奇峰.txt
《逍遥寻秦传》作者:楚凌天.txt
《妖怪传记》作者:岚.txt
不过还是谢谢
I want to do it in batch, not individually. Also, even using a.txt doesn't work. I tested with the following attached files and it failed. For example, the following several files are supposed to be renamed at one time as:
"Magic Fighter" Author: Xu Qifeng.txt
"X逍遥 Seeking Qin Biography" Author: Chu Lingtian.txt
"Monster Biography" Author: Lan.txt. But still, thank you.
附件
1: temp.rar (2007-7-15 02:30, 1.28 KiB,下载次数: 15)
|
|
2007-7-15 02:30 |
|
|
jmz573515
银牌会员
    
积分 1212
发帖 464
注册 2006-12-13
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
set fso=createobject("scripting.filesystemobject")
set folder=fso.getfolder(".")
for each file in folder.files
if ucase(right(file,3))="TXT" then
with fso.opentextfile(file)
n="《" & trim(.readline) & "》"
n=n & trim(.readline)
.close
end with
file.name=n & ".txt"
end if
next
```
set fso=createobject("scripting.filesystemobject")
set folder=fso.getfolder(".")
for each file in folder.files
if ucase(right(file,3))="TXT" then
with fso.opentextfile(file)
n="《" & trim(.readline) & "》"
n=n & trim(.readline) & ".txt"
.close
end with
file.name=n & ".txt"
end if
next
```
此帖被 +1 点积分 点击查看详情 评分人:【 slore 】 | 分数: +1 | 时间:2007-7-28 21:26 |
|
|
|
2007-7-28 21:08 |
|
|
slore
铂金会员
      
积分 5212
发帖 2478
注册 2007-2-8
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.getfolder(".")
For Each file In folder.files
If UCase(Right(file,3)) = "TXT" Then
With fso.OpenTextFile(file)
n = "《" & Trim(.ReadLine) & "》"
n = n & Trim(.ReadLine)
.Close
End With
file.name = n & ".txt"
End If
Next
Set folder = Nothing
Set fso = Nothing
VBS脚本对字符串处理还是方便
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.getfolder(".")
For Each file In folder.files
If UCase(Right(file,3)) = "TXT" Then
With fso.OpenTextFile(file)
n = "《" & Trim(.ReadLine) & "》"
n = n & Trim(.ReadLine)
.Close
End With
file.name = n & ".txt"
End If
Next
Set folder = Nothing
Set fso = Nothing
VBS scripts are still convenient for string processing
|
|
2007-7-28 21:27 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
Originally posted by venchia at 2007-7-15 02:30:
我是要批量的,不是单个的
还有,就算用a.txt也不行,我用下面附件的几个文件测试失败
比如下面的几个文件是想一次性的改名为
《魔斗士》 ...
你的测试失败是因为全角、半角问题。
用你的附件测试了以下代码,成功。
@echo off&setlocal enabledelayedexpansion
for /f %%a in ('dir/b *.txt') do (
for /f "tokens=1* delims=:" %%i in ('findstr /n .* %%a') do (
set "var=!var!%%j"
set var=!var: =!
if %%i equ 2 ren %%a 《%%~na》!var!.txt
set var=
)
)
Originally posted by venchia at 2007-7-15 02:30:
I need batch processing, not single processing
Also, even using a.txt doesn't work. I tested with the following attached files and failed
For example, the following several files are intended to be renamed at one time as
"Magic Fighter" ...
Your test failed because of full-width and half-width issues.
Tested the following code with your attachment and it succeeded.
@echo off&setlocal enabledelayedexpansion
for /f %%a in ('dir/b *.txt') do (
for /f "tokens=1* delims=:" %%i in ('findstr /n .* %%a') do (
set "var=!var!%%j"
set var=!var: =!
if %%i equ 2 ren %%a 《%%~na》!var!.txt
set var=
)
)
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2007-7-29 09:02 |
|
|
venchia
初级用户
 
积分 125
发帖 11
注册 2004-1-25
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
前阵都无法上来,刚刚上来就发现好多解答的方法了,感谢上面所有热心的朋友
I couldn't get on before, and just as I got on, I found many solutions. Thanks to all the kind friends above
|
|
2007-8-5 14:32 |
|
|
venchia
初级用户
 
积分 125
发帖 11
注册 2004-1-25
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Originally posted by ccwan at 2007-7-29 09:02 AM:
你的测试失败是因为全角、半角问题。
用你的附件测试了以下代码,成功。
@echo off&setlocal enabledelayedexpansion
for /f %%a in ('dir/b *.txt') do (
...
如果遇到全角跟半角都有的,就不行了
还有用的是findstr,遇到文件比较大时候,需要等很久
Originally posted by slore at 2007-7-28 09:27 PM:
Set fso = CreateObject("Scripting.FileSystemObject")
这个遇到有《》符号的文件名,就会出错
|
|
2007-8-5 15:04 |
|