Author Topic: move files to folder script not working as planned  (Read 100 times)

jay7981

  • Senior Member
  • ****
  • Posts: 103
  • Karma: 2
  • I don't know what I don't know and neither do you.
    • View Profile
    • Angeldyne
Hey all, i have this script that i was wanting to take all the .png files from the /svss folder and move them into a folder structure based on thier creation date. it kinda works as long as there are no duplicate names, so i added a function to rename the duplicates with _X and that is not working can i get some fresh eyes on this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  
  
<?php 
  error_reporting
(E_ALL);
  
//setup some vars
  
/root path of .png files
  $mpath 
__DIR__ .'/svss';
  
$count 0;
  
$errors 0;
  
$ok 0;
 function 
file_newname($path$filename){
     if (
$pos strrpos($filename'.')) {
            
$name substr($filename0$pos);
            
$ext substr($filename$pos);
     } else {
            
$name $filename;
 
	
	
   
$ext "";
     }
 
     
$newpath $path.'/'.$filename;
     
$newname $filename;
     
$counter 0;
     while (
file_exists($newpath)) {
            
$newname $name .'_'$counter $ext;
            
$newpath $path.'/'.$newname;
            
$counter++;
      }
 
     return 
$newname;
 }
 
//Delete all .htm files
 
array_map('unlink'glob"$mpath/*.htm"));
 
 
//Loop through all .png files
 
foreach (glob("$mpath/*.png") as $file) { 
 
	
	

 
	
	
//set variables
         
$lastmoddate filemtime($file);
         
$basename basename($file);     
         
$month date("M"$lastmoddate);
 
	
	
$day date("d"$lastmoddate);
         
$year date("Y"$lastmoddate);
         
$newPath __DIR__ .'/svss/' .$year'/' .$month'/' .$day'/';
 
	
	
$count++;
 
 
	
	
//See if the new directory is already created, then create if not
         
if (!is_dir($newPath)) {
             
mkdir($newPath0775true);
         }
 
 
	
	
//put the new path and the file together
         
$newName '/' .$year'/' .$month'/' .$day'/' .$basename;
         
$old_Path $mpath'/' .$basename;
         
$new_Path $mpath.$newName;
 
	
	

         
// Move the file if it doesn't exist and count each iteration
 
	
	
if (
file_exists($newPath.$basename)) {
 
	
	
	
$errors++;
 
	
	
	
file_newname($mpath$basename);
 
	
	
	
rename($old_Path$new_Path);
 
	
	
	
continue;
 
	
	
} else {
 
	
	
	
$ok++;
 
	
	
	
rename($old_Path$new_Path);
 
	
	
	
continue;
 
	
	
}
         
     }
 echo 
'Total: ' .$count'<br />';
 echo 
'Errors: ' .$errors'<br />';
 echo 
'Moves: ' .$ok'<br />';
 
?>
 

Please read before posting - Posting Guidelines - Please read before posting
<-- Press my Give Karma button if I was helpful.

PHP Help Forum

move files to folder script not working as planned
« on: June 30, 2017, 08:48:14 pm »



PHP Help Forum

Re: move files to folder script not working as planned
« Reply #1 on: July 15, 2017, 12:43:39 pm »



PHP Help Forum

Re: move files to folder script not working as planned
« Reply #2 on: Yesterday at 05:09:43 pm »