Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Script to delete folders

Posted on 2015-01-31
9
Medium Priority
?
220 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Accepted Solution

by:
Wilder1626 earned 2000 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 71

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 71

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Make the most of your online learning experience.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Six Sigma Control Plans
Suggested Courses

636 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