Solved

Find and redo files

Posted on 2016-09-06
5
38 Views
Last Modified: 2016-09-09
$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
Comment
Question by:Jasmine Ikhreishi
  • 2
  • 2
5 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41785991
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
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41785998
My whole code turned into an error (the red x) when I did the backslash
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 250 total points
ID: 41786003
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41786144
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 41786159
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Installer 5 38
How to change the link of an image using md5 in php ? 3 47
PHP alternative to file_get_contents('php://input') 4 72
Undefined variable problem 5 23
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question