?
Solved

Script to delete folders

Posted on 2015-01-31
9
Medium Priority
?
207 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 70

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 70

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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 …
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…
Suggested Courses

764 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