My output has a "?" on the name and the file doesn't open anymore

Jazzy 1012
Jazzy 1012 used Ask the Experts™
on
<?php
error_reporting(E_ALL);
ini_set('display_errors' ,1);

//expression to be found in file name
$find = '.5010.';

//directory name
//we will store renamed files here
$dirname = '5010';
if(!is_dir($dirname))
    mkdir($dirname, 0777);

//read all files from a directory
//skip directories
$directory_with_files = './';
$dh  = opendir($directory_with_files);
$files = array();
while (false !== ($filename = readdir($dh)))
{
    if(in_array($filename, array('.', '..')) || is_dir($filename))
        continue;

    $files[] = $filename;
}

//iterate collected files
foreach($files as $file)
{
    //check if file name is matching $find
    if(stripos($file, $find) !== false)
    {

        //open file
        $handle = fopen($file, "r");
        if ($handle)
        {
            //read file, line by line
            while (($line = fgets($handle)) !== false)
            {

                //find REF line
                        $refid = 'REF*2U*';
                        if(stripos($line, $refid) !== false)
                              {
                              //glue refernce numbers
                              //check if reference number is not empty
                              
                              $refnumber = str_replace(array($refid, '~'), array('', ''), $line);
                        if($refnumber != '')
                              {
                        $refnumber = '_'. $refnumber .'_';

                        $filerenamed = str_replace($find, $refnumber, $file);
                              copy($file, $dirname . '/' . $filerenamed);
                                    }

                        echo $refnumber . "\n";
                              }
            }

            //close file
            fclose($handle);
        }
    }
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
Please use the code snippet feature when you post code at E-E, thanks.

Please show us the file name that "doesn't open any more."  Then we can probably show you a tested and working example.

Author

Commented:
For example; My file is 4867586.5010.476564.ed

After the code executes and reads the file, the output should be: 4867586_SMIL01_476564.ed but instead its: 4867586_SMIL01

And when I checked it out on putty the file name was: 4867586_SMIL01?_476564.ed
Most Valuable Expert 2011
Top Expert 2016

Commented:
OK, thanks.  That's helpful.  I'll see if I can find what is causing the issue.  Back after I make a few tests!
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Author

Commented:
Alright, also I have the zend version of php so it's not the lastest update. Thank you!
Most Valuable Expert 2011
Top Expert 2016

Commented:
I wonder if there is something else in play here.  I can't see where "SMIL01" could be coming from.  Any ideas?

Author

Commented:
ISA*00*          *00*          *ZZ*133052274      *ZZ*362931550      *160822*1638*^*00501*000025142*0*P*:~
GS*HP*132274160822*1638*1*X*00X221A1~
ST*835*0001~
BPR*H*0*C*NON*****20160824~
TRN*1*1QG630664*1411289245*000087726~
REF*F2*1083~
DTM*405*20160819~
N1*PR*UNITED *XV*87726~
N3*990~
N4*MINKA*MN*559664~
REF*2U*SMIL01~
Most Valuable Expert 2011
Top Expert 2016

Commented:
OK, what is that?  Is it the contents of the file?  If so, it's helpful to attach the file  or if it's just a short text file, post it in the code snippet.
Most Valuable Expert 2011
Top Expert 2016
Commented:
Also, with a small bit of tinkering, I was able to run the script.  I put the original file here:
https://iconoun.com/demo/storage/4867586.5010.476564.ed

I put the "converted file" into the "5010" directory.  Here is the "view source" of the directory listing:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
 <head>
  <title>Index of /demo/5010</title>
 </head>
 <body>
<h1>Index of /demo/5010</h1>
<ul><li><a href="/demo/"> Parent Directory</a></li>
<li><a href="4867586_SMIL01%20%0a_476564.ed"> 4867586_SMIL01 
_476564.ed</a></li>
</ul>
</body></html>

Open in new window

As you can see we got what looks like an extraneous %20%0a injected into the file name.  My guess is that you want to trim() one of the data elements that is used to create the file name.
Most Valuable Expert 2011
Top Expert 2016

Commented:
This worked (I think).  Have a look at the lines that say REMOVE UNWANTED WHITESPACE CHARACTERS.

If you're new to PHP and want to learn the language, this article can help.  It has links to many good learning resources and more importantly, can help you avoid the many outdated and bad examples that litter the internet!
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html


<?php // demo/temp_jasmine.php
/**
 * https://www.experts-exchange.com/questions/28966264/My-output-has-a-on-the-name-and-the-file-doesn't-open-anymore.html
 *
 For example; My file is 4867586.5010.476564.ed
 After the code executes and reads the file, the output should be: 4867586_SMIL01_476564.ed but instead its: 4867586_SMIL01
 And when I checked it out on putty the file name was: 4867586_SMIL01?_476564.ed
 */
error_reporting(E_ALL);

//expression to be found in file name
$find = '.5010.';

//directory name
//we will store renamed files here
$dirname = '5010';
if(!is_dir($dirname)) mkdir($dirname, 0777);

//read all files from a directory
//skip directories
$directory_with_files = './';
$directory_with_files = 'storage'; // USE THIS DIRECTORY FOR TESTS

$dh  = opendir($directory_with_files);
$files = array();
while (false !== ($filename = readdir($dh)))
{
    if(in_array($filename, array('.', '..')) || is_dir($filename))
        continue;

    $files[] = $filename;
}

//iterate collected files
foreach($files as $file)
{
    //check if file name is matching $find
    if(stripos($file, $find) !== false)
    {
        $handle = fopen($directory_with_files . DIRECTORY_SEPARATOR . $file, "r");
        if ($handle)
        {
            while (($line = fgets($handle)) !== false)
            {
                $line = trim($line); // REMOVE UNWANTED WHITESPACE CHARACTERS
                //find REF line
                $refid = 'REF*2U*';
                if(stripos($line, $refid) !== false)
                {
                    $refnumber = str_replace(array($refid, '~'), array('', ''), $line);
                    $refnumber = trim($refnumber); // REMOVE UNWANTED WHITESPACE CHARACTERS
                    var_dump($refnumber);
                    if($refnumber != '')
                    {
                        $refnumber = '_'. $refnumber .'_';
                        $filerenamed = str_replace($find, $refnumber, $file);
                        copy($directory_with_files . DIRECTORY_SEPARATOR . $file, $dirname . DIRECTORY_SEPARATOR . $filerenamed);
                    }
                    echo $refnumber . "\n";
                } // END IF
            } // END WHILE READING FILE
            fclose($handle);
        }
    } // END IF FILENAME MATCH
}

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Here is a slightly more condensed version of your code - tested it on your sample file it seems to work.
<?php
error_reporting(E_ALL);

// Some setup stuff
$find = '.5010.';
$dirname = 't1517/5010';

// Get list of files using glob with wildcard
$files = glob("t1517/*{$find}*");

//iterate collected files
foreach($files as $file) {
  // Read all the lines in the file into an array
  $lines = file($file);
  
  // Iterate over the array of lines
  foreach($lines as $line) {
   
    // check if line contains search string
    // using preg_match to check and extract 
    // reference at the same time
    if (preg_match("/REF\*2U\*(.*?)\~/", $line, $match)) {
    
    // glob uses full paths so extract the filename from
    // the path
      $newfile = basename(str_replace($find, "_{$match[1]}_", $file));
    
    // Some debug code
      echo "f: {$file}, NF: {$newfile}<br/>";
    
    // And finally do the copy.
      copy($file, "{$dirname}/{$newfile}");
    }
  }
}

Open in new window

EDIT
the folder t1517 is my test folder - modify to match your environment
Most Valuable Expert 2011
Top Expert 2016

Commented:
@Julian: with file() you might  want to set both FILE_IGNORE_NEW_LINES and FILE_SKIP_EMPTY_LINES.  I say might  because I already posted a working example, and in the course of my testing I found extraneous whitespace to be the source of the file name problem.  But this hasn't been verified by the question author yet, and we don't know for sure whether all lines need to be trimmed, or whether only the information in the line with the REF*2U.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
I am cheating a bit because I am using information from his other thread. From what I understand he wants to use the REF*2U prefix to find the reference to use to rename the file. So while the file might have whitespace - as long as there is a line that matches the RegEx - it should work. I agree about the FILE_IGNORE_NEW_LINES / FILE_SKIP_EMPTY_LINES - might make the code a bit more efficient - but I was trying to keep it simple. I used file() as a shortcurt - but the solution can be adapted to use the opendir / while loop just as easily - the principle is the same.

For me the key piece here was the preg_match - stripos will work but preg_match checks for presence of the search string and extracts the bit you are looking for in the same call.

glob() also makes it easier allowing for a wildcard match - although there are some watch-it's with glob as well so the OP would do well to read up on the function.

I think Zephyr_hex' makes a good point in his  comment in the other thread. We might be chasing our tails a bit here trying to solve a problem that has the potential to grow into a very complicated beast.
Most Valuable Expert 2011
Top Expert 2016

Commented:
... potential to grow ...
Yes, exactly.  If the instant answers do not solve the issues, these questions might be good candidates for E-E Gigs.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Yup, I am going to object - don't know what

"The format inside is wrong"

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial