简体   繁体   English

批量删除旧文件和空文件夹

[英]batch delete old files and empty folders

I have a batch file that works on my local machine (Windows 7), but doesn't fully work on the server it is intended to live on(Windows 2008 R2 Service Pack 1). 我有一个可在本地计算机(Windows 7)上运行的批处理文件,但是在打算用于其上的服务器(Windows 2008 R2 Service Pack 1)上无法完全运行。

It needs to delete files older than a specified number of days in all listed folders, then delete empty folders and sub-folders. 它需要删除所有列出的文件夹中超过指定天数的文件,然后删除空文件夹和子文件夹。

Script: 脚本:

set DaysOld=3
set "folders[0]=C:\Test\BatchDel\1"
set "folders[1]=C:\Test\BatchDel\2"
set "folders[2]=C:\Test\BatchDel\3"
set "folders[3]=C:\Test\BatchDel\DNE"


for /F "tokens=1* delims==" %%s in ('set folders[') do (
    forfiles /p "%%t" /s /m * /D -%DaysOld% /C "cmd /c del @path"
)

for /f "delims=" %%d in ('dir /ad/b/s ^| sort /R') do rd "%%d"
pause

When run on my local machine (spacing added): 在我的本地计算机上运行时(添加了间隔):

C:\Test\BatchDel>set DaysOld=3
C:\Test\BatchDel>set "folders[0]=C:\Test\BatchDel\1"
C:\Test\BatchDel>set "folders[1]=C:\Test\BatchDel\2"
C:\Test\BatchDel>set "folders[2]=C:\Test\BatchDel\3"
C:\Test\BatchDel>set "folders[3]=C:\Test\BatchDel\DNE"

C:\Test\BatchDel>for /F "tokens=1* delims==" %s in ('set folders[') do (forfiles /p "%t" /s /m * /D -3 /C "cmd /c del @path" )
C:\Test\BatchDel>(forfiles /p "C:\Test\BatchDel\1" /s /m * /D -3 /C "cmd /c del@path" )
ERROR: No files found with the specified search criteria.
C:\Test\BatchDel>(forfiles /p "C:\Test\BatchDel\2" /s /m * /D -3 /C "cmd /c del@path" )
ERROR: No files found with the specified search criteria.
C:\Test\BatchDel>(forfiles /p "C:\Test\BatchDel\3" /s /m * /D -3 /C "cmd /c del@path" )
ERROR: No files found with the specified search criteria.
C:\Test\BatchDel>(forfiles /p "C:\Test\BatchDel\DNE" /s /m * /D -3 /C "cmd /c del @path" )
ERROR: The specified directory does not exist.

C:\Test\BatchDel>for /F "delims=" %d in ('dir /ad/b/s | sort /R') do rd "%d"

C:\Test\BatchDel>rd "C:\Test\BatchDel\3"
C:\Test\BatchDel>rd "C:\Test\BatchDel\2"
C:\Test\BatchDel>rd "C:\Test\BatchDel\1"
The directory is not empty.

C:\Test\BatchDel>pause
Press any key to continue . . .

When I run it on the server it asks for permission to delete each folder. 当我在服务器上运行它时,它会请求删除每个文件夹的权限。 (spacing added). (添加了间隔)。

F:\data\Scripts>set DaysOld=5
F:\data\Scripts>set "folders[0]=F:\data\Environments\TestA\Cache_Data"
F:\data\Scripts>set "folders[1]=F:\data\Environments\TestB\Cache_Data"

F:\data\Scripts>for /F "tokens=1* delims==" %s in ('set folders[') do (forfiles /p "%t" /s /m * /D -5 /C "cmd /c del @path" )
F:\data\Scripts>(forfiles /p "F:\data\Environments\TestA\Cache_Data" /s /m * /D -5 /C "cmd /c del @path" )

F:\data\Environments\TestA\Cache_Data\0432f59d-9fd1-46ed-8579-9ebe358113fb\*, Are you sure (Y/N)?

I need it to delete the empty folders on the server without pressing Y for each one. 我需要它删除服务器上的空文件夹,而不必每个都按Y。

EDIT: I updated the script. 编辑:我更新了脚本。 Thanks aschipfl! 谢谢aschipfl! it is now closer to working. 现在它已经接近工作了。 It is now asking Y/N to delete each folder. 现在,它要求是/否删除每个文件夹。

EDIT 2: updated the outputs. 编辑2:更新了输出。

I found this solution: 我找到了这个解决方案:

for /F "tokens=1* delims==" %%s in ('set folders[') do (
    forfiles /p "%%t" /s /m *.* /D -%DaysOld% /C "cmd /c del @path"
    for /f "delims=" %%d in ('dir "%%t" /s /b /ad ^| sort /r') do rd "%%d"
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM