Batch file to delete files over 10 days old

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
h17rj0
Posts: 2
Joined: 20 May 2010 03:28

Batch file to delete files over 10 days old

#1 Post by h17rj0 » 20 May 2010 03:35

Hi,

Can someone please help me I have created a batch file that deletes files over 10days old. I have managed to do this but this will not delete files with spaces in the name, any ideas?


forfiles "-pC:\Documents and Settings\admin\Desktop\New Folder" -m*.* -d-10 -s -c"cmd /c del /k @FILE| echo @FILE"

apologies in advance, if anything is wrong, i'm a newbie.

thanks
Harj

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch file to delete files over 10 days old

#2 Post by aGerman » 20 May 2010 11:42

AFAIK you have to substitude @FILE by \"@FILE\" .

Regards
aGerman

h17rj0
Posts: 2
Joined: 20 May 2010 03:28

Re: Batch file to delete files over 10 days old

#3 Post by h17rj0 » 21 May 2010 07:36

hey excellent thanks agerman.

can you tell me how i need to amend the path so this works on the server.
so instead of "-pC:\Documents and Settings\admin\Desktop\New Folder" -m*.* -d-10 -s -c"cmd /c del /k @FILE| echo @FILE"

i thought i could do

"-p\\server address" -m*.* -d-10 -s -c"cmd /c del /k @FILE| echo @FILE". but this does not work any ideas?

thanks again

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch file to delete files over 10 days old

#4 Post by aGerman » 21 May 2010 11:14

Hmm. I never used FORFILES (because it's not available on each computer). But I didn't found any documentation to use it with a server path too.
So, my recommendation would be connecting the server share with a drive letter.
Example:

Code: Select all

net use X: "\\server address\share"
forfiles -p X:\  .......your stuff
net use X: /delete


Regards
aGerman

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Batch file to delete files over 10 days old

#5 Post by alan_b » 26 May 2010 04:54

I know nothing of FORFILES etc. but my attention is focussed on this snippet
"-pC:\Documents and Settings\admin\Desktop\New Folder"

To avoid problems with embedded spaces the file path/name need encapsulating with quotes.
Just possibly this might work if the -p argument is confusing the quotes whilst within them
-p"C:\Documents and Settings\admin\Desktop\New Folder"

A very plausible solution is to use the short names and avoid embedded spaces,
i.e.
C:\Documents and Settings\admin\Desktop\New Folder
becomes
C:\DOCUME~1\admin\Desktop\NEWFOL~1

I determine the short name by appending /X to the DIR command,
which then displays both the short and the long names for the files and folders.
There may be alternative solutions.

WARNING - danger if C:\DOCUME~2\ also exists :-
C:\DOCUME~1\ does NOT necessarily refer to C:\Documents and Settings\
If you have an additional folder with the name C:\Documents to Purge\
then amongst the short names you will have C:\DOCUME~1\ and C:\DOCUME~2\,
and I would not like to predict which short name refers to which long name.

Alan

Post Reply