Help needed to create script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jeffhsu
Posts: 3
Joined: 05 Sep 2014 03:30

Help needed to create script

#1 Post by jeffhsu » 05 Sep 2014 03:37

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help needed to create script

#2 Post by foxidrive » 05 Sep 2014 06:26

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%"


jeffhsu
Posts: 3
Joined: 05 Sep 2014 03:30

Re: Help needed to create script

#3 Post by jeffhsu » 05 Sep 2014 09:16

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help needed to create script

#4 Post by foxidrive » 05 Sep 2014 09:28

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.

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.

jeffhsu
Posts: 3
Joined: 05 Sep 2014 03:30

Re: Help needed to create script

#5 Post by jeffhsu » 05 Sep 2014 09:56

It works!!!
Many many thanks!!!

Post Reply