Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am having a directory named as t001. it has following contents
file1.txt
sample.org
t001.test
DOC(thsi is a directory)
when i run a test script inside thsi folder some new folder are created and the new directory contents become
file1.txt
sample.org
t001.test
DOC(this is a already existing directory)
DIR1( a new directory)
DIR2(a new directory)
what i want is that when my test is over, that means it returns from code which has created above folders, all these newly created directories should be deleted. only those files and directories which were present before running test should remain, rest all newly created files and directories should be deleted.
  • Comment on delete newly created files and directories leaving old ones

Replies are listed 'Best First'.
Re: delete newly created files and directories leaving old ones
by davidj (Priest) on Feb 28, 2005 at 07:27 UTC
    If your script is creating temporary files and directories when it is run, take a look at File::Temp. It gives you control over the creation/deletion of temporary files and directories.

    davidj
Re: delete newly created files and directories leaving old ones
by chas (Priest) on Feb 28, 2005 at 07:12 UTC
    If you know which files and directories are created by your script, you can use unlink and/or rmdir or the operating system equivalents in a system command to remove them. If the created files/directories vary, you can use file test operators like -M to check on the age and just delete things which are very recent.
    (I'd make a "junk" directory to try this in so you don't accidently delete anything important...)
    chas
Re: delete newly created files and directories leaving old ones
by dorward (Curate) on Feb 28, 2005 at 11:19 UTC

    My instinct would have been to use File::Find and Array->diff(), but davidj's suggestion of File::Temp sounds like a better approach to the problem - even if you have to backtrack on your implementation (which presumably already creates the files and directories).

Re: delete newly created files and directories leaving old ones
by Anonymous Monk on Feb 28, 2005 at 06:59 UTC
    what is the question?
      what i want is that when my test is over, that means it returns from code which has created above folders, all these newly created directories should be deleted. only those files and directories which were present before running test should remain, rest all newly created files and directories should be deleted.
A reply falls below the community's threshold of quality. You may see it by logging in.