Archive

Posts Tagged ‘delete’

Deleting Directories With Phing

January 13th, 2009 No comments

Although using Phing is mainly about copying files, you might also need to delete directories and files using the delete element. Remember that the default behavior of copy command copies files only if the source files are different from the destination files. A prudent approach might be to delete the build directory and then recreate it, ready for Phing to copy files into.

To delete a directory you need to use the dir attribute of the delete element, the delete element also accepts the file attribute to delete specific files. The following target will delete the directory myProject_build.

<target name="prepare">
<delete dir="myProject_build" />
</target>

The delete element can also accept a fileset element, which will delete multiple files. the following code will delete any php file from the myProject_build folder and from a sub folder called app.

<fileset dir="./" id="deleteFiles">
 <include name="myProject_build/*.php" />
 <include name="myProject_build/app/*.php" />
</fileset>
 
<target name="prepare">
  <delete>
    <fileset refid="deleteFiles" />
  </delete>
</target>

Remember when doing this to remove any dir or file attributes from the delete element as they can interfere with your fileset. Also, if you use a fileset be careful not to use the same fileset that copies the files into the build directory as the only thing you will accomplish is the deletion of your application. This is quite easy to do, I even did it whilst testing the previous example!

Categories: PHP Tags: , , , , , , ,

Function To Delete Temporary Files

May 13th, 2008 No comments

If you allow users to upload data to your site you might have a situation where a data directory might be full of temporary files. In the long term you will want to get rid of these files as they have served their purpose and are no longer needed.

Here is a function that can be used to delete any files in a directory that were created more than 20 minutes ago. It uses the glob() function to find all files of a particular type and then uses the filectime() function to figure out when the file was last modified (or created). It will then delete (unlink) any files that were created more than 20 minutes ago.

function deleteTemporaryFiles(){
 // Define the folder to clean (keep trailing slashes)
 $dataFolder = 'data/';
 
 // Filetypes to check - use glob syntax
 $fileTypes = '*.{txt,xml}';
 
 // Here you can define after how many minutes the files should get deleted
 $expire_time = 20;
 
 // Find all files of the given file type
 foreach(glob($dataFolder.$fileTypes,GLOB_BRACE) as $Filename) {
  // Read file creation time
  $FileCreationTime = filectime($Filename);
 
  // Calculate file age in seconds
  $FileAge = time() - $FileCreationTime;
 
  // Is the file older than the given time span?
  if($FileAge > ($expire_time * 60)){
   // delete the file
   unlink($Filename);
  }
 }
}

To use it simply call it like this.

deleteTemporaryFiles();

Note that the filectime() function can give an incorrect value on some Win32 systems by returning the file creation time. This is what we are looking for, but if you find you are having problems with this function then replace filectime() with filemtime().

Categories: PHP Tags: , , , ,

Delete File By inode Reference

May 9th, 2008 No comments

If you want to delete a file that you can’t type in the name of either because the name is long and complicated, or because it is difficult to type in without causing a syntax error then here is the solution.

You first need to find the inode reference of the file. This can be done by using the command ls -li. The start of each line has a number that is specific to that file. You could use the command ls -i , but the output is a little confusing.

To delete the file use the find command with the flag -inum, followed by a pipe into the rm (remove file) command like the following.

find . -inum 916618 | xargs rm

The xargs bit is used to pass a list of the files found from the find command to the rm command.

Categories: Unix/Linux Tags: , , , ,

Finding Duplicate Values In A MySQL Table

February 6th, 2008 No comments

To find duplicate values you need to use the MySQL COUNT() function and then pick out all of the counts that are greater than one.

SELECT value,COUNT(value) AS Count
FROM test
GROUP BY value
HAVING (COUNT(value) > 1)
ORDER BY Count DESC;

Conversely you can also select the rows that only have a single entry.

SELECT value,COUNT(value) AS Count
FROM test
GROUP BY value
HAVING (COUNT(value) =; 1)
ORDER BY Count DESC;

However, it is very nice to pick out the duplicate entries in a table, but you might still need to do something with them. Here is a query to delete any duplicate rows from a table. It does a simple self join and deletes the row value with the lowest ID.

DELETE bad_rows.*
FROM tests AS good_rows
INNER JOIN tests AS bad_rows ON bad_rows.number = good_rows.number
AND bad_rows.id > good_rows.id;

More information on this deletion query and other methods of deleting duplicates can be found at Xaprb.com.

Categories: MySQL Tags: , , ,