|
ganjie
初级用户
 
积分 49
发帖 37
注册 2007-5-8
状态 离线
|
『楼 主』:
批量文件按名称排序并把文件名写入指定文件
使用 LLM 解释/回答一下
某文件夹下若干个文件,例如有A至E 5个文件,按名称排序后读取文件名,把它们放入文本中,按文件名保存,如A就保存为A.txt,同理B就是B.txt,A.txt是第一个文件,内容为 <var>A</var> <var>B</var> <var>C</var>
B.txt 内容如下
<var>A</var> <var>B</var> <var>C</var>
C.txt 内容如下
<var>B</var> <var>C</var> <var>D</var>
D.txt 内容如下
<var>C</var> <var>D</var> <var>E</var>
E.txt 内容如下
<var>C</var> <var>D</var> <var>E</var>
请注意看一下蓝色的部分,除了第一个和最后一个外,都是文本文件名的在中间,左边为按名称排序后的上一个,右边为按名称排序后的下一个,除第一个与最后一个外,下面的代码已经可以现实
@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
现在小弟想修改输出的内容
设A.txt为第一个文件,内容为 <var>A<tr>1</tr></var> <var>B<tr>2</tr></var><var>C<tr>3</tr></var> <var>B<tr>2</tr></var>
B.txt 内容如下
<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</tr></var>
同理 C.txt内容如下
<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>
但 D.txt内容有点变
<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>
注意黑色的部分
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>
可以看出A.txt和B.txt 、C.txt的黑色部分是ABC D.txt和E.txt、F.txt 的黑色部分是DEF
红色为上一个文件名 蓝色为下一个文件名 数字为文件名按名称排序后的序号
只有第一个和最后一个有特殊
假设Z.txt最后一个文件,其内容如下
<var>Y<tr>25</tr></var> <var>X<tr>24</tr></var><var>Y<tr>25</tr></var> <var>Z<tr>26</tr></var>
Last edited by ganjie on 2009-1-5 at 23:53 ]
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>
|
|
2009-1-5 23:51 |
|
|
creda
新手上路

积分 12
发帖 10
注册 2008-12-30
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
伙计能否告诉我,怎么读取文件夹中的文件名啊,再跟一个固定字符串进行判断。
比如 A文件夹 ,有 几个文件 ,让他们跟当前日期+0000 或当前日期+1200进行对比,如果对应就执行1 ,不对应执行2.
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.
|
|
2009-1-6 12:39 |
|
|
ahongguo
新手上路

积分 10
发帖 7
注册 2008-10-15 来自 河北沧州
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
建议弄的字小一点不好吗 弄这大
Is it not good to make the characters smaller? They are made so large.
|

DOS下燃起火热的电脑人生 |
|
2009-1-6 20:01 |
|
|
HAT
版主
       
积分 9023
发帖 5017
注册 2007-5-31
状态 离线
|
『第 4 楼』:
Re 2楼
使用 LLM 解释/回答一下
有问题请单独发帖提问,你在别人的帖子里面跟帖提问的话,很少有人能看到你的问题,愿意回答问题的就更少了。
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.
|

 |
|
2009-1-6 21:50 |
|
|
netbenton
银牌会员
     批处理编程迷
积分 1916
发帖 752
注册 2008-12-28 来自 广西
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
搞了一天,终于好了!^_^
@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 ]
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 ]
|
|
2009-1-8 00:57 |
|
|
ganjie
初级用户
 
积分 49
发帖 37
注册 2007-5-8
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
楼上的,我测试了,没有什么反应呀,提示说找不到文件,是不是你这个%temp%要改成%cd%,可是我改了后也没有测试成功!
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!
|
|
2009-1-8 18:10 |
|
|
netbenton
银牌会员
     批处理编程迷
积分 1916
发帖 752
注册 2008-12-28 来自 广西
状态 离线
|
『第 7 楼』:
改红色部分
使用 LLM 解释/回答一下
Originally posted by ganjie at 2009-1-8 18:10:
楼上的,我测试了,没有什么反应呀,提示说找不到文件,是不是你这个%temp%要改成%cd%,可是我改了后也没有测试成功!
@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
在红色部分指明你的目录和文件
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
|

精简
=> 个人 网志  |
|
2009-1-9 01:02 |
|
|
ganjie
初级用户
 
积分 49
发帖 37
注册 2007-5-8
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
经测试,输出文本的结果是我想要的内容,谢谢了,不过我不太理解您的编批算法和一些变量的意思,你能注释一下吗?授人鱼不如授人渔,烦麻一下!
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!
|
|
2009-1-9 18:01 |
|
|
netbenton
银牌会员
     批处理编程迷
积分 1916
发帖 752
注册 2008-12-28 来自 广西
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
@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 以上取得排序后的目录列表,并在前后加"#", 在这里加pause可以看到结果
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 文件名及备写内容数组,执行一次,递进一次(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 根据文件转化成备写内容,为#写入空,处理前后的特殊
:: 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 写入文件,三条一组 变量排列是按你的要求排的
::e
set /a var=n/3
::e 只有是3的倍数,var才会发生改变
set /a n+=1
)
::f 最后不足三条的,现在写入
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
@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
|

精简
=> 个人 网志  |
|
2009-1-9 21:18 |
|
|
ganjie
初级用户
 
积分 49
发帖 37
注册 2007-5-8
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
if "%%i"=="#" (set e1=) else (set "e1=^<var^>!f1!^<tr^>!n!^</tr^>^</var^>")
::c 根据文件转化成备写内容,为#写入空,处理前后的特殊
这句我不太理解,可以说得更详细吗?
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?
|
|
2009-1-9 21:56 |
|
|
netbenton
银牌会员
     批处理编程迷
积分 1916
发帖 752
注册 2008-12-28 来自 广西
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
#是另外加入列表前后的,你要求的第一项和最后项不是特殊吗
set "e1=^<var^>!f1!^<tr^>!n!^</tr^>^</var^>"
和
set e1=^^^<var^^^>!f1!^^^<tr^^^>!n!^^^</tr^^^>^^^</var^^^>
的结果是一样的,你自己试试吧
再说下去我也说不出来了,水平有限
# 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
|

精简
=> 个人 网志  |
|
2009-1-9 22:46 |
|
|
ganjie
初级用户
 
积分 49
发帖 37
注册 2007-5-8
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
#是另外加入列表前后的?是不是这就是第一个和最后要输出的内容,举个例子看看!还有test!代表什么? set /a var=(n-2)%%3这句,举个例子,想知道为何n-2,谢谢!
#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!
|
|
2009-1-9 22:51 |
|
|
netbenton
银牌会员
     批处理编程迷
积分 1916
发帖 752
注册 2008-12-28 来自 广西
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
#就是辅助处理前后特殊的,并不会生成#.txt文件,
test变量是为了三个一组输出设置的,如:abc与def各自有特殊关系
!var!大于!test! 才输出,而
set /a var=n/3
随n的变化,会使 var 增加,n与var的关系如下
n var
0 0
1 0
3 1
4 1
6 2
set /a var=(n-2)%%3 取(n-2 )除以3的余数
就是用来处理不足一组的文件,因为在for里面是三个一组输出的,不足的还没有处理
至于n-2是因为在for里面是读到第四个文件时才有输出的,而输出后又加了1 如:
0 #
1 a
2 b
3 c
4 d
5 e
6 #
n+1
7
for出来后n=7
但只有
(n-2)%%3 =2
项是需要输出的
所以要n-2
# 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.
|

精简
=> 个人 网志  |
|
2009-1-9 23:36 |
|