Solved

Script to delete folders

Posted on 2015-01-31
9
185 Views
Last Modified: 2015-02-07
I have the following script to delete files inside a folder that I use in a scheduled task

Now want to do the same but delete a folder instead with all files in it

forfiles /p "D:\Google Drive\xTuple Backups" /s /d -5 /m *.backup /c "cmd /c del @file"
forfiles /p "D:\Google Drive\xTuple Backups" /s /d -5 /m *.sql /c "cmd /c del @file"

Open in new window

0
Comment
Question by:Gerhardpet
  • 4
  • 3
  • 2
9 Comments
 
LVL 11

Assisted Solution

by:Wilder1626
Wilder1626 earned 500 total points
Comment Utility
Hi

This is an example that you can adjust:
set folder="D:\Google Drive\xTuple Backups"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)

Open in new window

0
 
LVL 11

Expert Comment

by:Wilder1626
Comment Utility
If you want something very easy also:

@echo off
rd D:\Google Drive\xTuple Backups /s /q

Open in new window


If it is the Google Drive folder that you want to delete, then:
@echo off
rd D:\Google Drive /s /q

Open in new window

0
 
LVL 1

Author Comment

by:Gerhardpet
Comment Utility
I want to be able to specify how many folders to delete based on date. How would one of your scripts do that?
0
 
LVL 11

Accepted Solution

by:
Wilder1626 earned 500 total points
Comment Utility
when you say by date, is it for example to delete all files in the last 7days?

@echo off
:: set folder path
set dump_path=D:\Google Drive
 
:: set min age of files and folders to delete
set max_days=7
 
:: remove files from %dump_path%
forfiles -p %dump_path% -m *.* -d -%max_days% -c "cmd  /c del /q @path"
 
:: remove sub directories from %dump_path%
forfiles -p %dump_path% -d -%max_days% -c "cmd /c IF @isdir == TRUE rd /S /Q @path"

Open in new window

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
Wilder,  rd /S /q removes the files of that folder, so the first forfiles line is superfluous. At least if we could trust the folder timestamp, which we can not.

And that leads to the question: what is your intention? The parent of a folder having files changed will not change the timestamp, Only folders being the parent of the changed file do.
0
 
LVL 1

Author Comment

by:Gerhardpet
Comment Utility
I use Syncback Pro to make cumalative backup using the %DATE% variable in the file path. I want to run the script a after the job is done running to delete old backups (folders).

Is there a way to count the folders based on date created and then delete the folder pass a certain date?
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
Since the folders are created on backup, folder timestamps can be relied on, and forfiles on folders should work. Other approaches are more difficult, require VBS or PowerShell, or other external tools.
0
 
LVL 1

Assisted Solution

by:Gerhardpet
Gerhardpet earned 0 total points
Comment Utility
Instead of using the %DATE% variable in the folder path I now use it with the file name which is creating a zip file.

So I can use my original script.
0
 
LVL 1

Author Closing Comment

by:Gerhardpet
Comment Utility
My original script works as well when using the %DATE% variable in the file name
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to dynamically set the form action using jQuery.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now