目标:我想在单个文件夹中搜索特定类型的文件。通过特定的文件名模式(例如“文件”)过滤并按文件名排序,结果如下所示:
file101.txt
file212.txt
file432.txt
现在,我只想选择最后一个项目(在这里file432.txt
:),然后对该文件执行进一步的操作(例如,复制和重命名)。
我该怎么做?
到目前为止,我得到的是以下内容:
@echo off
setlocal
set list=
for /r . %%g in (file*.txt) do echo %%g
..这给出了我问题顶部的列表。
据我了解,没有真正的列表-它只是带有返回/换行符的字符串。因此,我无法真正选择特定的搜索结果项。我将使用另一个for
循环,但我不知道除EOF之外我将针对哪种模式进行过滤。我唯一的想法是删除所有换行符并将结果保存在变量中。现在,找到的每一行都将覆盖前一行。
我该怎么做?
set lastitem=for %%g in (%list%) do REMOVE_NEWLINE and echo %%g
(此处:file432.txt),然后对该文件执行进一步的操作(例如,复制和重命名)。
使用以下批处理文件。
test.cmd:
@echo off
setlocal
for /r . %%g in (file*.txt) do (
set _lastitem=%%g
)
echo do something to the last file in the list, which is %_lastitem%
endlocal
示例输出:
F:\test>dir file*.txt
Volume in drive F is Expansion
Volume Serial Number is 3656-BB63
Directory of F:\test
22/08/2016 18:17 0 file101.txt
22/08/2016 18:17 0 file212.txt
22/08/2016 18:17 0 file432.txt
3 File(s) 0 bytes
0 Dir(s) 1,765,302,566,912 bytes free
F:\test>test
do something to the last file in the list, which is F:\test\file432.txt
F:\test>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句