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-11 05:29
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Batch files are sorted by name and their file names are written to a specified file View 1,873 Replies 12
Original Poster Posted 2009-01-05 23:51 ·  中国 上海 华为云
初级用户
Credits 49
Posts 37
Joined 2007-05-08 09:38
19-year member
UID 87820
Gender Male
Status Offline
There are several files in a certain folder. For example, there are 5 files from A to E. After sorting by name, read the file names and put them into the text, and save them according to the file name. For example, A is saved as A.txt, and similarly B is B.txt. The content of A.txt is <var>A</var> <var>B</var> <var>C</var>
The content of B.txt is as follows
<var>A</var> <var>B</var> <var>C</var>
The content of C.txt is as follows
<var>B</var> <var>C</var> <var>D</var>
The content of D.txt is as follows
<var>C</var> <var>D</var> <var>E</var>
The content of E.txt is as follows
<var>C</var> <var>D</var> <var>E</var>

Please pay attention to the part in blue. Except for the first and the last, the text file name is in the middle. The left is the previous one after sorting by name, right is the next one after sorting by name. Except for the first and the last, the following code can already be displayed
 
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir /b /a-d *.*^|find /i /v "%~nx0"') do (
set str2=!str1!
set str1=!var!
set var=%%~ni
if defined str2 (
if not defined var1 (
set "var1=!str2!"
>"!str2!.txt" echo ^^^<var^^^>!str2!^^^</var^^^> ^^^<var^^^>!str1!^^^</var^^^> ^^^<var^^^>%%~ni^^^</var^^^>
) else (
>"!str2!.txt" echo ^^^<var^^^>!var1!^^^</var^^^> ^^^<var^^^>!str2!^^^</var^^^> ^^^<var^^^>!str1!^^^</var^^^>
set "var1=!str2!"
set "var2=!var1!"
)))
>"!str1!.txt" echo ^^^<var^^^>!str2!^^^</var^^^> ^^^<var^^^>!str1!^^^</var^^^> ^^^<var^^^>!var!^^^</var^^^>
>"!var!.txt" echo ^^^<var^^^>!str2!^^^</var^^^> ^^^<var^^^>!str1!^^^</var^^^> ^^^<var^^^>!var!^^^</var^^^>
pause
Now the little brother wants to modify the output content
Let A.txt be the first file, and the content is <var>A<tr>1</tr></var> <var>B<tr>2</tr></var><var>C<tr>3</tr></var> <var>B<tr>2</tr></var>
The content of B.txt is as follows
<var>A<tr>1</tr></var> <var>A<tr>1</tr></var> <var>B<tr>2</tr></var> <var>C<tr>3</tr></var> <var>C<tr>3</var>

Similarly, the content of C.txt is as follows
<var>B<tr>0</tr></var> <var>A<tr>1</tr></var> <var>B<tr>2</tr></var> <var>C<tr>3</tr></var> <var>D<tr>4</tr></var>

But the content of D.txt has changed a bit
<var>C<tr>3</tr></var> <var>D<tr>4</tr></var> <var>E<tr>5</tr>
</var>var>F<tr>6</tr></var> <var>E<tr>5</tr></var>
Pay attention to the black part
E.txt
<var>D<tr>4</tr></var> <var>D<tr>4</tr></var> <var>E<tr>5</tr> </var>var>F<tr>6</tr></var> <var>F<tr>6</tr></var>

F.txt
<var>E<tr>5</tr></var> <var>D<tr>4</tr></var> <var>E<tr>5</tr> </var>var>F<tr>6</tr></var> <var>G<tr>7</tr></var>
It can be seen that the black part of A.txt and B.txt, C.txt is ABC The black part of D.txt and E.txt, F.txt is DEF


Red is the previous file name Blue is the next file name The number is the serial number after sorting the file names by name
Only the first and the last are special

Assume Z.txt is the last file, and its content is as follows
<var>Y<tr>25</tr></var> <var>X<tr>24</tr></var><var>Y<tr>25</tr></var> <var>Z<tr>26</tr></var>
Floor 2 Posted 2009-01-06 12:39 ·  中国 浙江 绍兴 电信
新手上路
Credits 12
Posts 10
Joined 2008-12-30 18:19
17-year member
UID 135372
Gender Male
Status Offline
Dude, can you tell me how to read the file names in a folder and then make a judgment with a fixed string?

For example, in folder A, there are several files. Compare them with the current date + 0000 or the current date + 1200. If it matches, execute 1; otherwise, execute 2.
Floor 3 Posted 2009-01-06 20:01 ·  中国 山东 淄博 联通
新手上路
Credits 10
Posts 7
Joined 2008-10-15 05:13
17-year member
UID 128404
Gender Male
From 河北沧州
Status Offline
Is it not good to make the characters smaller? They are made so large.
DOS下燃起火热的电脑人生
Floor 4 Posted 2009-01-06 21:50 ·  中国 重庆 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
If you have a question, please post it separately. If you ask a question in someone else's post, few people will see your question, and even fewer will be willing to answer it.
Floor 5 Posted 2009-01-08 00:57 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
Got it done after a whole day!^_^

@echo off&setlocal enabledelayedexpansion
echo #>%temp%\fs.tmp
dir aa\*.* /b /a-d>>%temp%\fs.tmp
echo #>>%temp%\fs.tmp
type %temp%\fs.tmp
set test=0
set n=0
set var=0
for /f "delims=" %%i in (%temp%\fs.tmp) do (
set "f5=!f4!"&set "e5=!e4!"
set "f4=!f3!"&set "e4=!e3!"
set "f3=!f2!"&set "e3=!e2!"
set "f2=!f1!"&set "e2=!e1!"
set f1=%%~ni
if "%%i"=="#" (set e1=) else (set "e1=^<var^>!f1!^<tr^>!n!^</tr^>^</var^>")
if !var! gtr !test! (
set test=!var!
>!f4!.txt echo !e5!!e4!!e3!!e2!!e3!
>!f3!.txt echo !e4!!e4!!e3!!e2!!e2!
>!f2!.txt echo !e3!!e4!!e3!!e2!!e1!
)
set /a var=n/3
set /a n+=1
)
set /a var=(n-2)%%3
if !var! gtr 1 echo !e4!!e4!!e3!!e2!!e2!>!f3!.txt
if !var! gtr 0 echo !e3!!e4!!e3!!e2!!e1!>!f2!.txt
dir *.txt
del %temp%\fs.tmp
pause

[ Last edited by netbenton on 2009-1-8 at 01:01 ]
Floor 6 Posted 2009-01-08 18:10 ·  中国 上海 华为云
初级用户
Credits 49
Posts 37
Joined 2007-05-08 09:38
19-year member
UID 87820
Gender Male
Status Offline
Upstairs, I tested it, but there was no response. It said it couldn't find the file. Is it that this %temp% should be changed to %cd%, but after I changed it, it still didn't test successfully!
Floor 7 Posted 2009-01-09 01:02 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
Originally posted by ganjie at 2009-1-8 18:10:
Above, I tested, but there was no response. It prompted that the file was not found. Did you need to change %temp% to %cd%? But after I changed it, it still didn't test successfully!


@echo off&setlocal enabledelayedexpansion
echo #>%temp%\fs.tmp
dir aa\*.* /b /a-d>>%temp%\fs.tmp
echo #>>%temp%\fs.tmp
type %temp%\fs.tmp
set test=0
set n=0
set var=0

Specify your directory and file in the red part
精简
=> 个人网志
Floor 8 Posted 2009-01-09 18:01 ·  中国 上海 华为云
初级用户
Credits 49
Posts 37
Joined 2007-05-08 09:38
19-year member
UID 87820
Gender Male
Status Offline
After testing, the result of the output text is what I want. Thank you. But I don't quite understand your coding algorithm and the meaning of some variables. Can you add comments? It's better to teach a man to fish than to give him fish. Please trouble you!
Floor 9 Posted 2009-01-09 21:18 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
@echo off&setlocal enabledelayedexpansion
echo #>%temp%\fs.tmp
dir aa\*.* /b /a-d>>%temp%\fs.tmp
echo #>>%temp%\fs.tmp
type %temp%\fs.tmp
::a Get the sorted directory list above, and add "#" before and after. Adding pause here can see the result

set test=0
set n=0
set var=0
for /f "delims=" %%i in (%temp%\fs.tmp) do (

::b
set "f5=!f4!"&set "e5=!e4!"
set "f4=!f3!"&set "e4=!e3!"
set "f3=!f2!"&set "e3=!e2!"
set "f2=!f1!"&set "e2=!e1!"
set f1=%%~ni
::b File name and backup content array, execute once, progress once (f1,e1)->f2,e2)->f3,e3)->f4,e4)->f5,e5)

::c
if "%%i"=="#" (set e1=) else (set "e1=^<var^>!f1!^<tr^>!n!^</tr^>^</var^>")
::c Convert according to the file to the backup content, write empty for #, process special before and after

:: d
if !var! gtr !test! (
set test=!var!
>!f4!.txt echo !e5!!e4!!e3!!e2!!e3!
>!f3!.txt echo !e4!!e4!!e3!!e2!!e2!
>!f2!.txt echo !e3!!e4!!e3!!e2!!e1!
)
:: d Write to file, three in a group. The variable arrangement is arranged according to your requirements

::e
set /a var=n/3
::e Only when it is a multiple of 3, var will change

set /a n+=1
)

::f Write the last insufficient three now
set /a var=(n-2)%%3
if !var! gtr 1 echo !e4!!e4!!e3!!e2!!e2!>!f3!.txt
if !var! gtr 0 echo !e3!!e4!!e3!!e2!!e1!>!f2!.txt
dir *.txt
del %temp%\fs.tmp
pause
精简
=> 个人网志
Floor 10 Posted 2009-01-09 21:56 ·  中国 上海 华为云
初级用户
Credits 49
Posts 37
Joined 2007-05-08 09:38
19-year member
UID 87820
Gender Male
Status Offline
if "%%i"=="#" (set e1=) else (set "e1=^<var^>!f1!^<tr^>!n!^</tr^>^</var^>")
::c Convert content according to the file, set empty for #, handle special before and after

I don't understand this sentence very much. Can you explain it in more detail?
Floor 11 Posted 2009-01-09 22:46 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
# is added before and after the list. Are the first and last items you requested special?

set "e1=^<var^>!f1!^<tr^>!n!^</tr^>^</var^>"
and
set e1=^^^<var^^^>!f1!^^^<tr^^^>!n!^^^</tr^^^>^^^</var^^^>
have the same result, you can try it yourself
I can't say any more, I'm limited in level
精简
=> 个人网志
Floor 12 Posted 2009-01-09 22:51 ·  中国 上海 华为云
初级用户
Credits 49
Posts 37
Joined 2007-05-08 09:38
19-year member
UID 87820
Gender Male
Status Offline
#Is it the content that is added before and after the list? Is this the first and last content to be output? Give an example to see! And what does test! represent? For the line set /a var=(n-2)%%3, give an example, I want to know why n-2, thank you!

#Is it the content that is added before and after the list? Is this the first and last content to be output? Give an example to see! And what does test! represent? For the line set /a var=(n-2)%%3, give an example, I want to know why n-2, thank you!
Floor 13 Posted 2009-01-09 23:36 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
# is only for assisting in handling special cases before and after, and it will not generate a #.txt file.

The test variable is set for outputting in groups of three. For example, abc and def have special relationships respectively.

!var! is greater than!test! to output, and
set /a var=n/3
As n changes, var increases. The relationship between n and var is as follows
n var
0 0
1 0
3 1
4 1
6 2

set /a var=(n-2)%%3 is used to handle files that are less than a group, because in the for loop, it outputs in groups of three, and the insufficient ones have not been processed yet.

As for n-2, it is because in the for loop, there is output only when the fourth file is read, and then 1 is added after output. For example:
0 #

1 a
2 b
3 c

4 d
5 e
6 #
n+1
7
After for, n=7
But only
(n-2)%%3 =2
item needs to be output
So n-2 is needed.
精简
=> 个人网志
Forum Jump: