Windows批处理脚本:仅对文件夹排序并仅处理第一个文件夹 [英] Windows Batch Script : Sort folders and process first folder only
问题描述
我有一个文件夹目录,我正在尝试编写Windows批处理脚本以按名称对文件夹进行排序,然后将第一个文件夹中的文件复制到其他位置.
I have a directory of folders and I am trying write a windows batch script to sort the folders by name and then copy the files in the first folder to a different location.
目录
- 2020-09-02
- 2020-09-01
此处为"2020-09-02";和"2020-09-01"是文件夹(或目录"文件夹下的子文件夹).每个文件夹(2020-09-02和2020-09-01)内部都有一组文件.
Here "2020-09-02" and "2020-09-01" are folders (or sub-folders under the folder 'Directory'). Each folder (2020-09-02 and 2020-09-01) has a set of files inside them.
我需要选择文件夹2020-09-01(因为这是最旧的文件夹),然后将文件夹2020-09-01中的文件复制到其他位置,然后删除文件夹2020-09-01.
I need to pick the folder 2020-09-01 (as that is the oldest folder) and copy the files inside the folder 2020-09-01 into a different location, and then delete the folder 2020-09-01.
我不需要循环执行此操作(以处理目录"下的所有文件夹).我只需要能够选择日期最少的文件夹并单独处理该文件夹即可.
I do not need to do this in a loop (to process all folders under 'Directory'). I just need to be able to pick the folder with least date and process that folder alone.
我擅长将目录中的文件复制到其他位置的代码,但是无法选择日期最旧的文件夹(没有for循环).
I am good with the code to copy the files in a directory to a different location, but having trouble to be able to select the folder with oldest date (without a for loop).
感谢您的帮助.在此先感谢!
Any help is appreciated. Thanks in Advance!
推荐答案
您可以使用dir
中的/o
开关来使用最早的项目,使用/ad
开关以仅显示目录,而使用/b
开关查看没有卷标和目录信息的目录,仅显示文件.
You can use the /o
switch from dir
to use the oldest item, the /ad
switch to only show directories and the /b
switch to view the directories without volume label and directory info, only shows the files.
rem Ensure that 'oldest' variable isn't being used before.
set oldest=
for /f "delims=" %%A in ('dir /od /ad /b') do if not defined oldest set "oldest=%%A"
rem Oldest folder is stored into %oldest%
我不明白您为什么不想使用for循环.
for
语句可能是批处理中功能最强大的语句,请查看 SS64 和 Rob van der Woude for
解释和示例.
I don't understand why you don't want to use a for loop.
for
statement is probably the most powerful one in batch, take a look at SS64 and Rob van der Woude for
explanation and examples.
这篇关于Windows批处理脚本:仅对文件夹排序并仅处理第一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!