Solved

Script to delete folders

Posted on 2015-01-31
9
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 11

Assisted Solution

by:Wilder1626
Wilder1626 earned 500 total points
ID: 40582016
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
ID: 40582017
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
ID: 40582020
I want to be able to specify how many folders to delete based on date. How would one of your scripts do that?
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 11

Accepted Solution

by:
Wilder1626 earned 500 total points
ID: 40582023
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
 
LVL 69

Expert Comment

by:Qlemo
ID: 40582479
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
ID: 40583032
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 69

Expert Comment

by:Qlemo
ID: 40583412
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
ID: 40583950
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
ID: 40595415
My original script works as well when using the %DATE% variable in the file name
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

756 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