Solved

Find and redo files

Posted on 2016-09-06
5
40 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 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 110

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 110

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 …

734 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