Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Find and redo files

Posted on 2016-09-06
5
Medium Priority
?
45 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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 111

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 111

Accepted Solution

by:
Ray Paseur earned 1000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

670 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