批处理文件来删除文件和文件夹的 [英] Batch file to delete files and folder

查看:125
本文介绍了批处理文件来删除文件和文件夹的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找那些文件夹中包含与特定的名称,然后删除该文件夹之后删除文件的方式。

I am looking for a way of deleting files that are contained in a folder with a specific name and then delete that folder afterwards.

我知道有没有,我可以用它来删除文件FORFILES命令行,我只是不知道如何让文件夹名进入的路径。该文件夹是基于它的创建日期命名,我想删除就是7天前的文件夹。该文件夹格式为YYYYMMDD。

I know there there is the forfiles command line that I can use to delete the files, I just don't know how to get the folder name into the path. The folder is named based on the date that it was created and I want to delete the folder that is 7 days old. The folder is formatted as yyyymmdd.

基本上我需要

forfiles /P "C:\backups\[DATE 7 DAYS AGO IN yyyymmdd format]" /S /M *.BAK /C "cmd /c del @path"

然后我想删除这将是以后的文件夹

and then I think to delete the folder after it would be

forfiles /P "C:\backups\[DATE 7 DAYS AGO IN yyyymmdd format]" /S /C "cmd /c IF @isDir == TRUE rd @path"

我如何填写[日期7天前...]这些。

How do I fill in the [DATE 7 DAYS AGO ...] for these.

推荐答案

好吧,所以我也跟着电力币黑客的建议,我结束了通过PowerShell中,它看起来如下这样:

Alright so I followed Elektro Hacker's advice and I ended up doing this via powershell, which looks as follows:

$currentDate = get-date
$backupPath = "C:\backup\"
$logFilePath = ("$backupPath" + "ScriptAndQuery\Logs\")

$dailyLog = ("$logFilePath" + "daily_delete.txt")
$weeklyLog = ("$logFilePath" + "weekly_delete.txt")
$monthlyLog = ("$logFilePath" + "monthly_delete.txt")

$dailyBackup = ("$backupPath" + "Daily\")
$weeklyBackup = ("$backupPath" + "Weekly\")
$monthlyBackup = ("$backupPath" + "Monthly\")

$addDateToFile = "TRUE"

get-childitem $dailyBackup |? { $_.psiscontainer -and $_.lastwritetime -le ($currentDate).adddays(-7) } |% { 
    try {
        if($addDateToFile -eq "TRUE") {
            Add-Content "$dailyLog" "$currentDate`r`n"
            $addDateToFile = "FALSE"
        }

        $fullPath = $_.FullName
        remove-item $fullPath -Recurse -force

        if (test-path $fullPath) {
            Add-Content "$dailyLog" "FAILED TO DELETE $fullPath`n"
        } else {
            Add-Content "$dailyLog" "SUCCESSFULLY DELETED $fullPath`n"
        }
    }
    catch {
        Add-Content "$dailyLog" "Exception Occurred: $_.Exception`n"
    }
}

if ($addDateToFile -eq "FALSE") {
    Add-Content "$dailyLog" "`r`n"
    $addDateToFile = "TRUE"
}

get-childitem $weeklyBackup |? {$_.psiscontainer -and $_.lastwritetime -le ($currentDate).adddays(-28)} |% { 
    try {
        if ($addDateToFile -eq "TRUE") {
            Add-Content "$weeklyLog" "$currentDate`r`n"
            $addDateToFile = "FALSE"
        }

        $fullPath = $_.FullName
        remove-item $fullPath -Recurse -force

        if (test-path $fullPath) {
            Add-Content "$weeklyLog" "FAILED TO DELETE $fullPath`n"
        } else {
            Add-Content "$weeklyLog" "SUCCESSFULLY DELETED $fullPath`n"
        }
    }
    catch {
        Add-Content "$weeklyLog" "Exception Occurred: $_.Exception`n"
    }
}

if ($addDateToFile -eq "FALSE") {
    Add-Content "$weeklyLog" "`r`n"
    $addDateToFile = "TRUE"
}

get-childitem $monthlyBackup |? {$_.psiscontainer -and $_.lastwritetime -le ($currentDate).adddays(-365)} |% { 
    try {
        if($addDateToFile -eq "TRUE") {
            Add-Content "$monthlyLog" "$currentDate`r`n"
            $addDateToFile = "FALSE"
        }

        $fullPath = $_.FullName
        remove-item $fullPath -Recurse -force 

        if (test-path $fullPath) {
            Add-Content "$monthlyLog" "FAILED TO DELETE $fullPath`n"
        } else {
            Add-Content "$monthlyLog" "SUCCESSFULLY DELETED $fullPath`n"
        }
    }
    catch {
        Add-Content "$monthlyLog" "Exception Occurred: $_.Exception`n"
    }
} 

if ($addDateToFile -eq "FALSE") {
    Add-Content "$monthlyLog" "`r`n"
}

在此我有发生(每日,每周,每月)3个不同的备份。所以基本上我删除了,每天都是周龄是一个月龄和月是一岁的所有文件,每周一次。我还打印到文件被为每个取出的日期和文件夹,或在该文件夹仍然存在或已发生异常事件失败消息。该脚本然后每天运行和是伟大的工作至今。

In this I have 3 different backups that occur (daily, weekly, and monthly). So basically I delete all the files from daily that are a week old, weekly that are a month old and monthly that are a year old. I also print to a file the date and folder that was removed for each, or a failure message in the event that the folder still exists or and exception has occurred. This script is then run daily and is working great so far.

谢谢,
DMAN

Thanks, DMan

这篇关于批处理文件来删除文件和文件夹的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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