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-06 07:26
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] Where is the mistake in the text replacement/renaming script I wrote? (with image) View 2,248 Replies 31
Original Poster Posted 2007-10-31 22:52 ·  中国 台湾 中华电信(HiNet)数据中心
初级用户
Credits 96
Posts 43
Joined 2007-10-31 01:20
18-year member
UID 101245
Gender Male
Status Offline
I'm a newbie, just started trying to write this ^^"
I have a directory of text files, they are data text files from a forum program


The text uses as the separator

The text content is as follows:
流浪漢∥∥A handy tool for searching Japanese AV actress pictures∥http://image.baidu.jp/∥59.113.162.145∥2007/03/21 13:37∥54188@IMYF.COM∥

I want to use the 2007/03/21 13:37 time part in every text file, which must be on the first line, to change the text filename

As shown below:


But with the way I wrote it, I can't get it done. Which part did I write wrong?
Please give me some guidance, big brothers, many thanks ^^


@echo off
for /f "delims=" %%f in ('dir /on/b "*.txt"') do (
for /f "tokens=1,2,3,4,5,6,7 delims=∥" %%i in (%%f) do echo %%i∥%%j∥%%k∥%%m∥%%o∥%%n∥%%l∥
&setlocal enabledelayedexpansion
for /f "delims=." %%i in ('dir /on/b *.txt') do (
ren "%%i".txt
)
pause


[ Last edited by leo1984 on 2007-10-31 at 10:53 PM ]
Floor 2 Posted 2007-10-31 23:07 ·  中国 陕西 西安 电信
初级用户
Credits 106
Posts 44
Joined 2007-06-01 22:25
19-year member
UID 90001
Gender Male
Status Offline
I see that in your picture it's “||” and not “//”
Floor 3 Posted 2007-10-31 23:16 ·  中国 浙江 衢州 中移铁通
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
Floor 4 Posted 2007-11-01 01:43 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
I'm not quite clear on what you mean: if your text content is like in your picture, then it should be || rather than ∥. How about trying this?

@echo off &setlocal enabledelayedexpansion
for /f "delims=" %%f in ('dir /on/b "*.txt"') do (
for /f "tokens=6 delims=^|^|" %%i in (%%f) do (
if not defined aaa set aaa=%%i.txt
set aaa=!aaa:/=!
set aaa=!aaa::=:!
set aaa=!aaa: =-!
)
ren %%f !aaa!
)
pause


[ Last edited by terse on 2007-11-1 at 02:00 AM ]
Floor 5 Posted 2007-11-01 02:06 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Modified the post above a bit (if it's ||, change ∥ to ^|, and set tokens=5 according to the position of the characters you need): it's recommended to test it first and only do it for real after it's correct
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%f in ('dir /on/b "*.txt"') do (
set n=
for /f "tokens=5 delims=∥" %%i in (%%f) do (
set /a n+=1
if !n! == 1 (
set str=%%i
set str=!str:/=!
set str=!str::=!
ren "%%f" "!str!.txt"
)
)
)
pause


[ Last edited by zh159 on 2007-11-1 at 02:08 AM ]
Floor 6 Posted 2007-11-01 02:30 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Changed it again. Here I tested it based on what was posted, so it's tokens=6. Of course that's not important, the key thing is that it can process it. In the previous post I didn't notice it was the first line.
:流浪漢|| ||A handy tool for searching Japanese AV actress pictures||http://image.baidu.jp/||59.113.162.145||2007/03/20 13:37||54188@IMYF.COM||


@echo off&setlocal enabledelayedexpansion
for %%i in (*.txt) do (
set i=%%i
call :rena
)
pause
goto :eof
:rena
for /f "tokens=6 delims=^|^|" %%i in ('more ^< "%i%"') do (
if not defined line set "line=%%i"
set line=!line:/=!
set line=!line::=!
)
ren "!i!" "!line!.txt"
set line=


[ Last edited by terse on 2007-11-1 at 02:59 AM ]
Floor 7 Posted 2007-11-01 05:31 ·  中国 台湾 中华电信(HiNet)数据中心
初级用户
Credits 96
Posts 43
Joined 2007-10-31 01:20
18-year member
UID 101245
Gender Male
Status Offline
Looks like you really need logic to play with this stuff. Every big brother writes it differently, but they all work.
I still need to practice more, otherwise I'm practically making a joke of myself by coming here @@

1112yuhua
It really is ^^

abcd
Haha, big bro is awesome indeed, it worked as soon as I tested it. If you ever come to Taiwan, I definitely have to treat you to a meal ^^

terse
Both of your commands worked, as long as the parameters are changed to match what I need. I did test the first one, but some symbols weren't removed from the filename, so it became 2007/03/21 13:37.txt
Many thanks, big bro ^^
@echo off &setlocal enabledelayedexpansion
for /f "delims=" %%f in ('dir /on/b "*.txt"') do (
for /f "tokens=5 delims=" %%i in (%%f) do (
if not defined aaa set aaa=%%i.txt
set aaa=!aaa:/=!
set aaa=!aaa::=:!
set aaa=!aaa: =-!
)
ren %%f !aaa!
)
pause

zh159
Yours also works every single time, no problem at all ^^

It's just that I can't figure out what these strings do @@
set aaa=!aaa:/=!
set aaa=!aaa::=:!
set aaa=!aaa: =-!

[ Last edited by leo1984 on 2007-11-1 at 05:41 AM ]
Floor 8 Posted 2007-11-01 07:25 ·  中国 浙江 衢州 中移铁通
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
∥ is one character, not two characters like //

The OP really gets up early too
Floor 9 Posted 2007-11-01 07:40 ·  中国 台湾 中华电信(HiNet)数据中心
初级用户
Credits 96
Posts 43
Joined 2007-10-31 01:20
18-year member
UID 101245
Gender Male
Status Offline
Haha~ I was exhausted last night working on a website
so I went to sleep first, and got up earlier today ^^

Can I also ask something else? When I was changing the text filenames,
I found that it can't recognize the - character
So after renaming, it becomes 61.224.104.78 - 61.224.104.78.txt

But I don't need it to recognize the - character
I just want to turn 61.224.104.78 - 61.224.104.78 into 61.224.104.78

Before processing:

0o0∥1∥ Vista-style language bar & new sound icon & volume control interface∥It looks pretty good after installation, but it's in Simplified Chinese = =!!!, if I want to change it back how do I do it @0@???∥61.224.104.78 - 61.224.104.78∥2006/11/01 09:23∥zerg0517@yahoo.com.tw

After processing:

0o0∥1∥ Vista-style language bar & new sound icon & volume control interface∥It looks pretty good after installation, but it's in Simplified Chinese = =!!!, if I want to change it back how do I do it @0@???∥61.224.104.78∥2006/11/01 09:23∥zerg0517@yahoo.com.tw

I tried writing another for to grab and delete the characters between
∥61.224.104.78 - 61.224.104.78
but it all failed. How should this be written?

[ Last edited by leo1984 on 2007-11-1 at 07:43 AM ]
Floor 10 Posted 2007-11-01 08:11 ·  中国 浙江 衢州 中移铁通
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
If the number of ∥ before the IP is fixed, then the following should work.
for /f "tokens=1,2,3,4,5,6* delims=∥-" %%i in (test.txt) do (
echo %%i∥%%j∥%%k∥%%l∥%%m∥%%o
)
Floor 11 Posted 2007-11-01 08:22 ·  中国 浙江 衢州 中移铁通
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
@echo off
for /f "tokens=1* delims=-" %%i in (test.txt) do (
set "str=%%j"
set "str1=%%i"
call set "str=%%str:*∥=%%"
call echo "%%str1:~0,-1%%∥%%str%%"
)
pause


This is for cases where the count isn't fixed, but because the string contains the special character &, you have to add "" when outputting it.
Floor 12 Posted 2007-11-01 08:27 ·  中国 台湾 中华电信(HiNet)数据中心
初级用户
Credits 96
Posts 43
Joined 2007-10-31 01:20
18-year member
UID 101245
Gender Male
Status Offline
Big bro, it seems it doesn't work
∥ really is a fixed count
- is also fixed

It looks like this loop is finding the character pair ∥- , right?
But when I tested it just now, there was no response @@

Also, big bro, haven't you gone to work or class yet?
Thanks for the enthusiastic help ^^
Floor 13 Posted 2007-11-01 08:29 ·  中国 台湾 中华电信(HiNet)数据中心
初级用户
Credits 96
Posts 43
Joined 2007-10-31 01:20
18-year member
UID 101245
Gender Male
Status Offline
The command in the second section that you gave says

This time < should not appear

What does that mean @@?
Floor 14 Posted 2007-11-01 08:29 ·  中国 浙江 衢州 中移铁通
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
sed "s/\(\{1,3\}\.\{1,3\}\.\{1,3\}\.\{1,3\}\) - \1/\1/" test.txt


Using sed is much faster
Floor 15 Posted 2007-11-01 08:31 ·  中国 浙江 衢州 中移铁通
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
Because I copied this section into Notepad to process it:
"0o0∥1∥ Vista-style language bar & new sound icon & volume control interface∥It looks pretty good after installation, but it's in Simplified Chinese = =!!!, if I want to change it back how do I do it @0@???∥61.224.104.78 - 61.224.104.78∥2006/11/01 09:23∥zerg0517@yahoo.com.tw∥“

I tested every section, and it works on my side.
Forum Jump: