如何删除其中超过2天以上的所有空文件夹? [英] How to delete all empty folders which are older than 2 days?

查看:116
本文介绍了如何删除其中超过2天以上的所有空文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做一个脚本,删除子文件夹的所有空文件夹中的路径。
现在,我必须作出,如果一个文件夹的创建2天前和空它应该早于2 Days.And如果不是,应该不会被删除的其他空文件夹中删除。
我也需要/想做出删除的文件夹都写在日志中。
我提出,用5个文件类型,但我不知道如何与文件夹,这个schould的工作。

林真正的新批,所以我不知道我应该做的。
我检查了谷歌,但结果并没有与我的问题相匹配。
我希望有人能帮助我。

下面是我到目前为止这从来就写成code:

 关闭@echo::启动路径/变量
集STARTDIR =C:\\用户
::初始化变量
设置/循环= 0
::目录C:\\ TEMP \\将被创建,如果文件夹不存在
如果不存在C:\\ TEMP \\ MD C:\\ TEMP \\
:: C语言为删除的文件类型创建日志文件:\\登录\\ LOG_Useless_File_Killer.txt
呼应----------------------------------->> C:\\登录\\ LOG_Useless_File_Killer.txt
在%的时间%GT%日期%;>:ECHO从日志文件C:\\登录\\ LOG_Useless_File_Killer.txt
回声。 >> C:\\登录\\ LOG_Useless_File_Killer.txt
::这5文件类型都将被立即删除
德尔C:\\用户\\的Thumbs.db / F / Q / S>> C:\\登录\\ LOG_Useless_File_Killer.txt
德尔C:\\用户\\的desktop.ini / F / Q / S>> C:\\登录\\ LOG_Useless_File_Killer.txt
德尔C:\\用户\\ * DS_Store / F / Q / S>> C:\\登录\\ LOG_Useless_File_Killer.txt
德尔C:\\用户\\ * ._ DS_Store / F / Q / S>> C:\\登录\\ LOG_Useless_File_Killer.txt
德尔C:\\用户\\ *桌面/ F / Q / S>> C:\\登录\\ LOG_Useless_File_Killer.txt
::写directorys在C:\\ TEMP \\ tmp.txt。
DIR / AD / B / S%STARTDIR%GT; C:\\ TEMP \\ tmp.txt
::在转到开始将再次启动
:开始
::变量%循环%加1
设置/循环=%循环%+ 1
::在5 - >转到退出
如果%循环%== 5转到退出
::在5 - >转到启动
否则转到启动
::删除这是用C写的每一个空文件夹:\\ TEMP \\ tmp.txt
FOR / Fdelims =%%我在(C:\\ TEMP \\ tmp.txt)做RD我%%
:: - >转到启动和重新开始
转到启动
::%循环%已达到5 - >出口
:出口
::控制台窗口将被关闭
出口暂停出口


解决方案

普拉迪普是绝对正确的最好方法走的是对的文件。


  

有关删除文件夹,试试这个:结果
  FORFILES -p/ D -15 / CCMD / c如果@isdir == TRUE RD / S / Q @Path结果
  / D是天数,你也可以用命令的参数发挥,以满足精确的要求。


您还可以使用环境变量太多,所以你可以很容易地只对当前登录的用户删除文件。例如,你可以使用%HOMEPATH%\\桌面来得到当前用户的桌面。更多的环境变量<一个href=\"https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx?mfr=true\"相对=nofollow>这里。

I make a Script which deletes all empty Folders with Subfolders in a Path. Now i have to make, if a folder was created 2 days ago and its empty it should be deleted with the other empty folders that are older than 2 Days.And if not it should be not deleted. And i also need/want to make that deleted Folder are written in a Log. I made that with the 5 Filetypes but i dont know how this schould work with the Folders.

Im really new to Batch, so i dont know what i should do. I checked Google but the Results did not match with my problem. I hope someone can help me.

Here is my Code that i´ve written so far:

@echo off

::start path / Variables
set startdir="C:\Users" 
::Initialize the Variable
set /a loop=0
::Directory c:\temp\ will be created, if the folder not exists 
if not exist c:\temp\ md c:\temp\
::Create Logfile for Deleted Filetypes in C:\Log\LOG_Useless_File_Killer.txt
echo ----------------------------------- >> C:\Log\LOG_Useless_File_Killer.txt
echo Logfile from: %date% at %time% >> C:\Log\LOG_Useless_File_Killer.txt
echo. >> C:\Log\LOG_Useless_File_Killer.txt
::this 5 Filetypes are going to be deleted immediately
del C:\Users\Thumbs.db /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt
del C:\Users\desktop.ini /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt 
del C:\Users\*.DS_Store /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt 
del C:\Users\*._DS_Store /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt 
del C:\Users\*.desktop /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt 
::Writes the directorys in c:\temp\tmp.txt. 
dir /AD /b /s %startdir% > c:\temp\tmp.txt
::at goto start it will be start again
:start
::the Variable %loop% is increased by 1  
set /a loop =%loop%+1
::at 5 --> goto exit
if %loop%==5 goto exit
::Under 5 --> goto start 
else goto start
::deletes every empty folder which is written in C:\temp\tmp.txt
for /F "delims=" %%i in (c:\temp\tmp.txt) do rd "%%i" 
::--> goto start and begins again 
goto start
::%loop% has reached 5 --> exit
:exit
::Console window will be closed 
exit

pause 

exit

解决方案

Pradeep is absolutely right about the best way to go is for files.

For deleting folders, try this:
FORFILES -p "" /D -15 /C "cmd /c IF @isdir == TRUE rd /S /Q @path"
/D is for number of days, you can play with command parameters to meet exact requirement.

You can also use environment variables too so you can easily only delete files on the user that is currently logged on. For example, you can use %HOMEPATH%\Desktop to get to the desktop of the current user. More environment variables here.

这篇关于如何删除其中超过2天以上的所有空文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆