开发者

Check if file exists in 2 directories using ASP or PHP

I am looking for a way to compare 2 directories to see 开发者_如何学JAVAif a file exists in both. What I want to do is delete a file in 1 of the directories if it exists in both.

I can either use ASP or PHP.

Example:

/devices/1001
/devices/1002
/devices/1003
/devices/1004
/devices/1005

/disabled/1001
/disabled/1002
/disabled/1003

So since 1001, 1002, 1003 exist in /disabled/, I want to remove them from /devices/ and only be left with 1004, 1005 in /devices/.


Using scandir() to get an array of the file names in each directory, and then using array_intersect() to find elements of the first array that are present in any additional arguments given.

http://au.php.net/manual/en/function.scandir.php

http://au.php.net/manual/en/function.array-intersect.php

<?php
$devices = scandir('/i/auth/devices/');
$disabled = scandir('/i/auth/disabled/');

foreach(array_intersect($devices, $disabled) as $file) {
    if ($file == '.' || $file == '..')
        continue;
    unlink('/i/auth/devices/'.$file);
}

Applied as a function including checking the directories are valid:

<?php
function removeDuplicateFiles($removeFrom, $compareTo) {
    $removeFromDir = realpath($removeFrom);
    if ($removeFromDir === false)
        die("Invalid remove from directory: $removeFrom");

    $compareToDir = realpath($compareTo);
    if ($compareToDir === false)
        die("Invalid compare to directory: $compareTo");

    $devices = scandir($removeFromDir);
    $disabled = scandir($compareToDir);

    foreach(array_intersect($devices, $disabled) as $file) {
        if ($file == '.' || $file == '..')
            continue;
        unlink($removeFromDir.DIRECTORY_SEPARATOR.$file);
    }
}

removeDuplicateFiles('/i/auth/devices/', '/i/auth/disabled/');


It's very easy with PHP - in this example we set the two base directories and the filename... this could easily be an array in a foreach() loop. Then we check in both directories to see if it does indeed reside in each. If so, we delete from the first. This can be easily modified to delete from the second.

See below:

<?php 

$filename = 'foo.html';
$dir1 = '/var/www/';
$dir2 = '/var/etc/';
if(file_exists($dir1 . $filename) && file_exists($dir2 . $filename)){
  unlink($dir1 . $filename);
}


if ($handle = opendir('/disabled/')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            unlink('/devices/' . $file);            
        }
    }
    closedir($handle);
}


In php, use this for checking if the file exists.... it will return true or false...

file_exists(relative file_path)


For each file in devices check to see if it exists in disabled using the disabled path and the file name from devices.

<%

    Set fso = server.createobject("Scripting.FileSystemObject")

    Set devices   = fso.getfolder(server.mappath("/i/auth/devices/"))
    Set disabledpath  = server.mappath("/i/auth/disabled/")

    For each devicesfile in devices.files
        if directory.fileExists(disablepath & devicesfile.name ) Then 

            Response.Write " YES "
            Response.write directoryfile.name & "<br>"

        Else

            Response.Write " NO "
            Response.write directoryfile.name & "<br>"

        End if 
    Next    

%>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜