问题描述
据我所知,有吨在这个网站就设立了通过在指定的文件夹中去,并删除它们,如果满足条件规定的批处理文件的问题。
I understand that there are tons of questions on this site regarding the creation of a batch file that goes through the file in a specified folder and deletes them if it satisfies the condition stated.
不过,我想的调整,一点点。在我的批处理文件,我想看看在一个文件夹,比如C:\\开发,并得到所有都是一样的一个月之内的文件。让所有这些文件后,我想通过所有的日期进行排序和删除除最新一期的一切。所以,如果我有5个文件月份与日期该文件夹于27 12年1月1日,20日,和30,我会只保留日的文件,1月30日,并删除所有其他人。
However, I would like to tweak that a little bit. In my batch file, I would like to look at a folder, say C:\Dev and get all the files that are within the same month. After getting all those files, I want to sort through all the dates and delete everything except for the latest one. So if I have 5 files for January on that folder with dates January 1, 12, 20, 27, and 30, I would only keep the file dated January 30th and delete all the others.
这可能吗?
推荐答案
<郎-DOS - >
< lang-dos -->
@ECHO OFF
SETLOCAL
SET "targetdir=c:\sourcedir"
SET "pfname="
PUSHD "%targetdir%"
FOR /f "delims=" %%a IN ('dir /b /a-d /o:d "*" ') DO (
SET "fname=%%a"
SET "fdate=%%~ta"
CALL :process
)
POPD
GOTO :EOF
:process
:: reformat date - this depends on yout local date-format.
:: YY(YY)MM required - my format is dd/mm/yyyy
SET fdate=%fdate:~6,4%%fdate:~3,2%
IF NOT DEFINED pfname GOTO nodel
IF %fdate%==%pfdate% ECHO DEL "%targetdir%\%pfname%"
:nodel
SET pfdate=%fdate%
SET "pfname=%fname%"
GOTO :eof
这应该为你工作。所需的DEL命令仅仅是为了测试目的 ECHO
编辑。您确认后,该命令是正确的,将 ECHO DEL
到 DEL
来实际删除文件。
This should work for you. The required DEL commands are merely ECHO
ed for testing purposes. After you've verified that the commands are correct, change ECHO DEL
to DEL
to actually delete the files.
首先,目标目录设置和 pfname
被清零。
First, the target directory is set up and pfname
is cleared.
的 PUSHD
更改当前目录
直到 POPD
执行
在 DIR
命令输出档案名称只( / B
),没有目录名(日期顺序(/广告
) / O:D
)。每一行设置 FNAME
来的文件名和 FDATE
到FILEDATE。
the dir
command outputs filenames only (/b
), no directory names (/a-d
) in date-order (/o:d
). Each line sets fname
to the filename and fdate
to the filedate.
在:进程
,日期字符串操纵。我不知道你使用哪种格式,但基本的计算公式为%变量:指定startPosition〜长度%
指定startPosition哪里开始于0 =第一个字符。这个想法是有 FDATE
格式 YYYYMM
within :process
, the date-string is manipulated. I don't know which format you use, but the basic formula is %variable:~startposition,length%
where startposition starts at 0=first character. The idea is to have fdate
in the format yyyymm
如果 pfname
(previos文件名)没有设定,这是第一个找到的文件,所以我们不要删除。
if pfname
(previos filename) is not set, this is the first file found, so we don't delete that.
有关每个其他文件,如果FILEDATE是一样的previous FILEDATE,则删除previous文件名
For every other file, if the filedate is the same as the previous filedate, then delete the previous filename.
当前文件名/日期,然后被记录为previous版本。
The current filename/date is then recorded as the previous version.
完成!
这篇关于批处理文件删除文件的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!