Solved

Find and redo files

Posted on 2016-09-06
5
31 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 30

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 30

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 108

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 108

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now