poele
asked on
delete subfolder created by date in batch file
I have a folder which is used as receptacle for folders which are named by their creation date(%yyyy%-%mm%-%dd%-%hh% -%mn%). And I want a batch file who can delete all folders except the last 40 folders.This operation will be done on WinXP who didn't contain
the deltree command and I want to automate the task. So a batch file could be just fine.
the deltree command and I want to automate the task. So a batch file could be just fine.
The remove directory command will do the same thing as deltree.
rd /s /q foldername
Removes (deletes) a directory.
RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path
/S Removes all directories and files in the specified directory
in addition to the directory itself. Used to remove a directory
tree.
/Q Quiet mode, do not ask if ok to remove a directory tree with /S
rd /s /q foldername
Removes (deletes) a directory.
RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path
/S Removes all directories and files in the specified directory
in addition to the directory itself. Used to remove a directory
tree.
/Q Quiet mode, do not ask if ok to remove a directory tree with /S
poele,
you would't believe it (I guess, neither did I), but it's only one line (yeah, I love batch :-)
Here it is:
__________________________ __________
for /F "usebackq skip=40" %%i in (`dir /a:d /o:-d /b`) DO @ECHO rd /s /q "%%i"
__________________________ __________
A little explanation:
First of all: dir /a:d /o:d /b
Basically it says: give me all directorienames (no files, no extra info) and sort those; newest folders first
Second:
With that output: skip the first 40 lines (the newest 40 files) and with the rest: delete those, including files and subfolders.
So: If there are less than 40 folders, it wouldn't do anything
You might have noticed the @ECHO command. This preventsthe command from actually deleting dfiles. it only shows on yoyr screen what it would do.
make the last part: DO rd /s /q "%%i" to make it actually work.
Hope this will do?
you would't believe it (I guess, neither did I), but it's only one line (yeah, I love batch :-)
Here it is:
__________________________
for /F "usebackq skip=40" %%i in (`dir /a:d /o:-d /b`) DO @ECHO rd /s /q "%%i"
__________________________
A little explanation:
First of all: dir /a:d /o:d /b
Basically it says: give me all directorienames (no files, no extra info) and sort those; newest folders first
Second:
With that output: skip the first 40 lines (the newest 40 files) and with the rest: delete those, including files and subfolders.
So: If there are less than 40 folders, it wouldn't do anything
You might have noticed the @ECHO command. This preventsthe command from actually deleting dfiles. it only shows on yoyr screen what it would do.
make the last part: DO rd /s /q "%%i" to make it actually work.
Hope this will do?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you don't mind a VB script this can be easily accomplished by sorting an array.
poele,
any progress? does it work?
any progress? does it work?
ASKER
Ello MaartenG,
Waiting is harder than working.
I try your solution with some variable with the same result.
it gived me the reteining hold back line ie: for /F "usebackq skip=40", but the actual automatic backup does the result in this form ex: 'f:\vaillant\backup form\%yyyy%-%mm%-%dd%-%hh% -%mn%\fact uration\*. *' And %yyyy%-%mm%-%dd%-%hh%-mn% mean 2004-07-13-09-59
In eatch folder automaticly named by date in this form:%yyyy%-%mm%-%dd%-%hh% -%mn% I found the folder: Facturation. Anothe point, when I used your suggest edition with %%i it reported abnornal terminaition %%i.
If I just put %i I can read on screen the total line deternime by the skip=X command but even if i took all the contained files on the folder: Facturation and put it directly on the dated folders :%yyyy%-%mm%-%dd%-%hh%-%mn % I get the same result without geting rid of the folders i want to delete.
Waiting is harder than working.
I try your solution with some variable with the same result.
it gived me the reteining hold back line ie: for /F "usebackq skip=40", but the actual automatic backup does the result in this form ex: 'f:\vaillant\backup form\%yyyy%-%mm%-%dd%-%hh%
In eatch folder automaticly named by date in this form:%yyyy%-%mm%-%dd%-%hh%
If I just put %i I can read on screen the total line deternime by the skip=X command but even if i took all the contained files on the folder: Facturation and put it directly on the dated folders :%yyyy%-%mm%-%dd%-%hh%-%mn
ASKER
Ello MaartenG,
this is the result I get:
H:\NOUVEA~1\BUREAU~1\back> for /F "usebackq skip=10" %i in (`dir /a:d /o:-d`) DO
@ECHO rd /s /q H:\Nouvea~1\Bureau~1\back " %i"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 0"
rd /s /q H:\Nouvea~1\Bureau~1\back " 20"
But all the folders still in the 'back' folder
P.S.: I don't know if I maked myself enought clear by the way I try to explain it but my english is not so good than my french. I try hard ;)
this is the result I get:
H:\NOUVEA~1\BUREAU~1\back>
@ECHO rd /s /q H:\Nouvea~1\Bureau~1\back " %i"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 2004-07-23"
rd /s /q H:\Nouvea~1\Bureau~1\back " 0"
rd /s /q H:\Nouvea~1\Bureau~1\back " 20"
But all the folders still in the 'back' folder
P.S.: I don't know if I maked myself enought clear by the way I try to explain it but my english is not so good than my french. I try hard ;)
poele,
>> Waiting is harder than working.
The reason I asked is because I'm on holidays right now (won't be back for a couple of weeks) ....
So I'll keep it short:
There's a little error in your batch; it should state (I'll just go with you here):
H:\NOUVEA~1\BUREAU~1\back> for /F "usebackq skip=10" %i in (`dir /a:d /o:-d`) DO @ECHO rd /s /q "H:\Nouvea~1\Bureau~1\back \%i"
OR:
C:\> for /F "usebackq skip=10" %i in (`dir /a:d /o:-d "H:\NOUVEA~1\BUREAU~1\back "`) DO @ECHO rd /s /q "%i"
NB1:You have to remove the @ECHO to actually delete the dirs.
NB2: Nothing wrong with your English (as far as I'm concerned; I'm from the Netherlands)
Good luck,
MaartenG
>> Waiting is harder than working.
The reason I asked is because I'm on holidays right now (won't be back for a couple of weeks) ....
So I'll keep it short:
There's a little error in your batch; it should state (I'll just go with you here):
H:\NOUVEA~1\BUREAU~1\back>
OR:
C:\> for /F "usebackq skip=10" %i in (`dir /a:d /o:-d "H:\NOUVEA~1\BUREAU~1\back
NB1:You have to remove the @ECHO to actually delete the dirs.
NB2: Nothing wrong with your English (as far as I'm concerned; I'm from the Netherlands)
Good luck,
MaartenG
ASKER
Finaly I aggre and discovered I was sleeping on the switch. You make me redive and force to awake my memory about batching files. I discovered the versatility of cmd compare to the old com.
Now I reserch a good reference book on the subject .
A real thank to you.
Gil
poele
Now I reserch a good reference book on the subject .
A real thank to you.
Gil
poele
ASKER
For poeple who are interesting by this question the solution proposed by MaartenG was the right one.
Here it is : for /F "usebackq skip=X" %%i in (`dir /a:d /o:-n /b [path:\folder]`) DO rd /s /q "%%i"
1) skip=X mean number of folders retained
2) [path:\folders] mean the tree you want to treat
Here it is : for /F "usebackq skip=X" %%i in (`dir /a:d /o:-n /b [path:\folder]`) DO rd /s /q "%%i"
1) skip=X mean number of folders retained
2) [path:\folders] mean the tree you want to treat
I'm already thinking about it. Just a question:
Do all the folders in this directory have this format (%yyyy%-%mm%-%dd%-%hh%-%mn
Maarten