I need to write a script to run it from a target parent folder, it searches all the subfolders containing the subfolder name "draft"
and delete any files that is more than 4 months old in it
Can someone help me out?
Thanks
Help needed to create script
Moderator: DosItHelp
Re: Help needed to create script
Remove ECHO after testing because at the moment it will only echo the filenames to the screen.
Code: Select all
@echo off
echo Processing, please wait...
for /d /r %%a in (draft*) do if /i "%%~nxa"=="draft" call :next "%%a"
echo Done!
pause
goto :EOF
:: Deletes files older than -nn days
:: days is the number of days ago (set days=-30)
:: change set from= to your desired drive and folder
:: fs is the filespec (*.*)
:: set s=-s to recursively process folders
:: remove -s to make it process only the current folder
:next
set p1=%comspec% /C if not @ISDIR
set p2=ECHO del @PATH
set days=-40
set from="%~1"
set fs=*.*
set s=
FORFILES -p %from% -m %fs% -d %days% %s% -c "%p1%==TRUE %p2%"
Re: Help needed to create script
Many thanks for your script
I adjusted your line FORFILES -p%from% -m%fs% -d%days% %s% -c"%p1%==TRUE %p2%"
to have a space after -p -m -d -c, otherwise I got syntax error for FORFILES
FORFILES -p %from% -m %fs% -d %days% %s% -c "%p1%==TRUE %p2%"
I got the following results,
it did correctly identify all the files in the subfolders of drafts with files more than 40 days to delete,
Processing, please wait...
del ""C:\Users\Admin\Desktop\Parent\1\draft\hpcomplaint.txt"\"hpcomplaint.txt""
del ""C:\Users\Admin\Desktop\Parent\1\draft\Robin Rachel Actual Day Itinerary_30
032013 Rev0.pdf"\"Robin Rachel Actual Day Itinerary_30032013 Rev0.pdf""
Processing, please wait...
del ""C:\Users\Admin\Desktop\Parent\3\draft\Robin Rachel Actual Day Itinerary_30
032013 Rev0.pdf"\"Robin Rachel Actual Day Itinerary_30032013 Rev0.pdf""
Done!
Press any key to continue . . .
I notice the syntax for del command have extra repeat file name twice
and when I remove the ECHO word, it fail to delete any file
Kindly advice.
May I ask what does -s to recursively process folders actually mean?
I am not very sure what it does when I do it and do not put it when I tested it
Thanks
I adjusted your line FORFILES -p%from% -m%fs% -d%days% %s% -c"%p1%==TRUE %p2%"
to have a space after -p -m -d -c, otherwise I got syntax error for FORFILES
FORFILES -p %from% -m %fs% -d %days% %s% -c "%p1%==TRUE %p2%"
I got the following results,
it did correctly identify all the files in the subfolders of drafts with files more than 40 days to delete,
Processing, please wait...
del ""C:\Users\Admin\Desktop\Parent\1\draft\hpcomplaint.txt"\"hpcomplaint.txt""
del ""C:\Users\Admin\Desktop\Parent\1\draft\Robin Rachel Actual Day Itinerary_30
032013 Rev0.pdf"\"Robin Rachel Actual Day Itinerary_30032013 Rev0.pdf""
Processing, please wait...
del ""C:\Users\Admin\Desktop\Parent\3\draft\Robin Rachel Actual Day Itinerary_30
032013 Rev0.pdf"\"Robin Rachel Actual Day Itinerary_30032013 Rev0.pdf""
Done!
Press any key to continue . . .
I notice the syntax for del command have extra repeat file name twice
and when I remove the ECHO word, it fail to delete any file
Kindly advice.
May I ask what does -s to recursively process folders actually mean?
I am not very sure what it does when I do it and do not put it when I tested it
Thanks
Re: Help needed to create script
I edited the code above. Try it again.
The issues you saw are from changes in forfiles.exe since the days where it was an optional download for XP.
If you add -s then folders below each draft folder will also be processed.
So folders like draft\books and draft\photos would also be processed.
The issues you saw are from changes in forfiles.exe since the days where it was an optional download for XP.
jeffhsu wrote:May I ask what does -s to recursively process folders actually mean?
I am not very sure what it does when I do it and do not put it when I tested it
If you add -s then folders below each draft folder will also be processed.
So folders like draft\books and draft\photos would also be processed.
Re: Help needed to create script
It works!!!
Many many thanks!!!
Many many thanks!!!