Jump to content

How to have a script move 30 files every time it is run?


ghurty

Recommended Posts

How would I go about writing a php script that every time it runs, it will move 30 files out of a particular directory, into a different directory.

 

The folder will be the current date (ex: 012410 or 01242010). The folder will have a few hundred files. I want all the files moved into a different directory, but due to different reasons, I would not want them moved all at once, rather lets say thirty at a time. So I would want it that each time the script is run, it will move 30 of the files over. However, I dont want it that if the directory is empty (or has less then 30 files), it will cause the server to crash (or use tons of resources) because it is bumping into errors.

 

 

 

Thank You

Link to comment
Share on other sites

here is some pseudocode:

use some filesystem functions to check number of files in directory.
if more than 30 files in folder:
   use for loop to loop around 30 times
      move files from directory to destination directory, one file per loop of for loop. (verify each move else output warning/error)
   exit script with success message, show user what happened.
else:
   do not do anything.
   exit script showing user there is not enough files in the directory.

 

here are some links to php manual pages that will help:

 

http://www.php.net/manual/en/refs.fileprocess.file.php - Filesystem functions

  http://www.php.net/manual/en/function.scandir.php - scandir()

http://www.php.net/manual/en/control-structures.for.php - for() control structure

  http://www.php.net/manual/en/function.rename.php - rename() - also used to move files

 

Link to comment
Share on other sites

To check whether the directory you are using for example the current date, you could use the scandir() function to test whether there are 30 or more files in the directory. So for example you could use this to test:

 

<?php

$directory = '012410' ;
$directoryScan = scandir($directory) ;

$numFiles = count($directoryScan) - 1 ;

if ($numFiles >= 30) {
echo "There are 30 or more files in the directory: $directory" ;
} else {
echo "There are no files to move today." ;
}

?>

 

This code will just check to see if there are more than 30 files on the particular folder you want to check.

 

Link to comment
Share on other sites

Perhaps I'm thinking too far outside of the box, but if this is really just a matter of moving (files in) a directory when a CRON runs, can't you just use shell commands to get the job done (thinking mv)? Needing to only move 30 files at a time is a very strange requirement, could you elaborate on exactly why that's a necessity?

Link to comment
Share on other sites

The files will always be moved into the same directory. I have an app that automatically process files in that directory. However the app has been known to crash if to many files are added in at once.

 

So all I am trying to do is slow down the transfer of files into it into a manageable rate.

 

The origination folder can have a few hundred files in it. Eventually I want all the files moved into the other folder, but not all at once.

 

So I thought the best way was to have a cron job run a php file that copies 30 files at once, and I would have the cron job run every few minutes.

 

If you have a better suggestion, I would love to hear it.

 

Also how would I tell that the variable $origdirectory should equal todays date?

 

 

Thank You

Link to comment
Share on other sites

<?php
$origdirectory=date('mdy').'/';
$destdirectory='process/';
if(is_dir($origdirectory))
{
  $dh=opendir($origdirectory);
  $ctr=0;
  while($ctr<30 && $file=readdir($dh))
  {
    if(is_file($origdirectory.$file))
    {
      rename($origdirectory.$file,$desdirectory.$file);
      $ctr++;
    }
}
closedir($dh);
if(!$ctr) rmdir($origdirectory);
?>

 

Edit: added file check, directory check, and orginal directory removal

Link to comment
Share on other sites

Hey give this code a shot. It will move all files in the variable $directory to the NewDirectory only if there are 30 or more files in the $directory variable. The $directory variable will hold the current date for each day in the format of 01162010. If there are only a couple hundred files in the $directory variable it will not over run your server. I just moved 147 files in about 2 seconds with this same script.

 

<<?php

$directory = date('djY') ;
$directoryScan = scandir($directory) ;

$numFiles = count($directoryScan) - 1 ;


if ($numFiles >= 30) {
	foreach ($directoryScan as $value) {
		if ((is_file($value)) AND (substr($item, 0, 1) != '.')) {
			rename("$directory/$value" , "NewFolder/$value") ;
		}
}
echo "There are 30 or more files in the directory: $directory<br /><br /> The files have been moved successfully." ;
} else {
echo "There are no files to move today." ;
}

?>

 

Link to comment
Share on other sites

Regarding FTP, I am trying to transfer from one directory to another on the same server.

 

I also need it to be completely automatic.

 

$Three3 - From what I understand from your script, it will copy all the files at once, as long as there are more the 30 files there.  That is not what I need.

 

What I am trying to complete is the following scenario:

 

Origination folder has for example 200 files. I want it that the script runs once, moves 30 files. Two minutes later, the cron job runs again, so another 30 files are moved. This continues every two minutes until all the files are moved over. If I have more then 30 files moved into the new directory at a time, the app that is monitoring that directory will crash. So I only want thirty files moved over at once.

 

 

Thank You

 

 

Link to comment
Share on other sites

the app that is monitoring that directory will crash.

 

Ohhh.. i thought you want moving from a buggy server..

call app provider.. tell them to fix it!

 

try this.. (its very basic)

<?php
$folder = date("mdY");
$count = 30;
$target = "target/$folder/";
mkdir($target);
mvr("source/$folder/",$target);

function mvr($dir,$target){
global $count;
if(!$dh = @opendir($dir)) return;
while (false !== ($file = readdir($dh))){
	if($file == '.' || $file == '..') continue;
	if(is_dir($dir.'/'.$file)){
		mkdir($target.$file);
		mvr($dir.'/'.$file.'/', $target.$file.'/');
		rmdir($dir.$file);
	}else{
		if($count==0) return;
		rename($dir.$file,$target.$file);
		$count--;
	}
}
closedir($dh);
}
?>

 

EDIT: oops updated $count to 30

Link to comment
Share on other sites

laffin - If lets say there are fifty files will it do thirty and then twenty, or will it stop when the number of files remaining is not divisible by thirty?

 

Does the folder get processed from reletive to site root, or from the diretroy where the php file is located?

 

Also, I am getting a parse error on line 18.

 

Thank You.

Link to comment
Share on other sites

last lines should have been

  if(!$ctr) rmdir($origdirectory);
}
?>

 

the script runs from the PWD (Present Working Directory)

if running from CRON, I would make them full path directories

$origdirectory='/my/logs/' . date('mdy').'/';
$destdirectory='/my/working/process/';

 

MadTechie's code is approx the same (cept his is recursive, it handles sub-folders)

you would have to change these lines

$target = "/my/working/folder/";
mvr("/my/logs/$folder/",$target);

 

 

Link to comment
Share on other sites

I am trying your script:

In the root I have the php file.

It is copying the file out, but instead of to the process folder, it is copying into the root directory (the same directory that I have the php file in).

 

 

$destdirectory='process/';

 

 

Thank You

Link to comment
Share on other sites

I have the files stored in a path not  part of the website:

The PHP file is in : /var/www/html

The orig folder is in:/orig/

The destination folder is in: /var/spool/test

So I have it:

$origdirectory='/orig/' . date('mdy').'/';

$destdirectory='/var/spool/test';

 

I run it, and the files dont go anywhere.

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.