Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 47
  • Last Modified:

Find and redo files

$directory_with_files= 'C:\Users\Jazz\Desktop\file';
$dirname =$directory_with_files. date('m-d-Y') ; 

Open in new window


I have the code above
$directory_with_files; is the place where I have my files
and
$dirname ; is the place I want my new files to be. (which is inside "$directory_with_files" but in a folder with todays date)
But now all I get is : C:\Users\Jazz\Desktop\file09-06-2016
Any ideas?
0
Jasmine Ikhreishi
Asked:
Jasmine Ikhreishi
  • 2
  • 2
2 Solutions
 
Marco GasiFreelancerCommented:
Just add a backslash to $directory_with_files:
$directory_with_files= 'C:\Users\Jazz\Desktop\file\';
$dirname =$directory_with_files. date('m-d-Y') ; 

Open in new window

will ouput C:\Users\Jazz\Desktop\file\09-06-2016
0
 
Jasmine IkhreishiAuthor Commented:
My whole code turned into an error (the red x) when I did the backslash
0
 
Marco GasiFreelancerCommented:
Ok, probably you are using that variable in other part of your code. The do this instead:
$directory_with_files= 'C:\Users\Jazz\Desktop\file';
$dirname =$directory_with_files.'\'.date('m-d-Y') ; 

Open in new window

0
 
Ray PaseurCommented:
Beware of this kind of file name! file09-06-2016.

You almost certainly want to use the ISO-8601 standard date notation.  How to handle Date/Time values in PHP is explained in this article.
https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html
0
 
Ray PaseurCommented:
This tests out OK.
<?php // demo/temp_jasmine_6.php
/**
 * https://www.experts-exchange.com/questions/28967928/Find-and-redo-files.html
 * "in a folder with todays date"
 */
error_reporting(E_ALL);
echo '<pre>';


// WHERE ARE WE FINDING OUR FILES?
$cwd = 'storage';

// WHERE ARE WE PUTTING THEM?
$new = $cwd . DIRECTORY_SEPARATOR . 'files' . date('Ymd');
if (!is_dir($new)) mkdir($new);

// START BY LOCATING THE FILES
$iterator = new FilesystemIterator('storage', FilesystemIterator::SKIP_DOTS + FilesystemIterator::KEY_AS_FILENAME);

// USE A REGULAR EXPRESSION TO MATCH THE DESIRED FILE NAMES
$regex
= '#'          // REGEX DELIMITER
. DIRECTORY_SEPARATOR
. '389'        // STRING LITERAL "the beginning 3"
. '.*?'        // ANYTHING OR NOTHING
. 'edi_'       // STRING LITERAL
. '[\d]'       // CHARACTER CLASS FOR DIGITS
. '{1}'        // EXACTLY ONE DIGIT
. '$'          // AT THE END OF THE STRING
. '#'          // REGEX DELIMITER
;

// GATHER THE LIST OF THE DESIRABLE FILES
$desirable = [];
foreach ($iterator as $name => $filepath)
{
    if (preg_match($regex, $filepath))
    {
        $desirable[] = $name;
    }
}

// COPY THE FILES INTO THE NEW DIRECTORY
foreach ($desirable as $name)
{
    file_put_contents($new . DIRECTORY_SEPARATOR . $name, file_get_contents($cwd . DIRECTORY_SEPARATOR . $name));
}

// SHOW A LINK SO WE CAN SEE THE COMBINED DATA
$link = '<a target="_blank" href="' . $new . '">SEE THE FILES</a>' . PHP_EOL;
echo $link;

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now