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
Solved

Script to delete folders

Posted on 2015-01-31
9
192 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
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
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.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

792 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