中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-11 06:20
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » Batch files are sorted by name and their file names are written to a specified file
Printable Version  1,876 / 12
Floor1 ganjie Posted 2009-01-05 23:51
初级用户 Posts 37 Credits 49
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
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>
Floor2 creda Posted 2009-01-06 12:39
新手上路 Posts 10 Credits 12
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.
Floor3 ahongguo Posted 2009-01-06 20:01
新手上路 Posts 7 Credits 10 From 河北沧州
Is it not good to make the characters smaller? They are made so large.
Floor4 HAT Posted 2009-01-06 21:50
版主 Posts 5,017 Credits 9,023
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.
Floor5 netbenton Posted 2009-01-08 00:57
银牌会员 Posts 752 Credits 1,916 From 广西
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 ]
Floor6 ganjie Posted 2009-01-08 18:10
初级用户 Posts 37 Credits 49
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!
Floor7 netbenton Posted 2009-01-09 01:02
银牌会员 Posts 752 Credits 1,916 From 广西
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
Floor8 ganjie Posted 2009-01-09 18:01
初级用户 Posts 37 Credits 49
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!
Floor9 netbenton Posted 2009-01-09 21:18
银牌会员 Posts 752 Credits 1,916 From 广西
@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
Floor10 ganjie Posted 2009-01-09 21:56
初级用户 Posts 37 Credits 49
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?
Floor11 netbenton Posted 2009-01-09 22:46
银牌会员 Posts 752 Credits 1,916 From 广西
# 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
Floor12 ganjie Posted 2009-01-09 22:51
初级用户 Posts 37 Credits 49
#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!
Floor13 netbenton Posted 2009-01-09 23:36
银牌会员 Posts 752 Credits 1,916 From 广西
# 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.
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023