China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-05 11:37
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] Problem with batch renaming of text files View 1,780 Replies 8
Original Poster Posted 2007-07-14 15:23 ·  中国 广东 汕头 电信
初级用户
Credits 125
Posts 11
Joined 2004-01-25 00:00
22-year member
UID 16050
Gender Male
Status Offline
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
Floor 2 Posted 2007-07-14 16:12 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
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
)
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 3 Posted 2007-07-14 16:15 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
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
)
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 4 Posted 2007-07-15 02:30 ·  中国 广东 汕头 电信
初级用户
Credits 125
Posts 11
Joined 2004-01-25 00:00
22-year member
UID 16050
Gender Male
Status Offline
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.
Attachments
temp.rar (1.28 KiB, Downloads: 15)
Floor 5 Posted 2007-07-28 21:08 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
```
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
```
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
slore +1 2007-07-28 21:26
Floor 6 Posted 2007-07-28 21:27 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
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
Floor 7 Posted 2007-07-29 09:02 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
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=
)
)
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 8 Posted 2007-08-05 14:32 ·  中国 广东 汕头 电信
初级用户
Credits 125
Posts 11
Joined 2004-01-25 00:00
22-year member
UID 16050
Gender Male
Status Offline
I couldn't get on before, and just as I got on, I found many solutions. Thanks to all the kind friends above
Floor 9 Posted 2007-08-05 15:04 ·  中国 广东 汕头 电信
初级用户
Credits 125
Posts 11
Joined 2004-01-25 00:00
22-year member
UID 16050
Gender Male
Status Offline
Originally posted by ccwan at 2007-7-29 09:02 AM:

Your test failed due to 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 (
...

If there are both full-width and half-width characters, it won't work.

Also, when using findstr, it takes a long time when the file is large.

Originally posted by slore at 2007-7-28 09:27 PM:
Set fso = CreateObject("Scripting.FileSystemObject")

This will go wrong when there are file names with "《》" symbols.
Forum Jump: