Avatar of Jazzy 1012
Jazzy 1012
 asked on

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

<?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);
        }
    }
}
PHP

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
Ray Paseur

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.
Jazzy 1012

ASKER
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
Ray Paseur

OK, thanks.  That's helpful.  I'll see if I can find what is causing the issue.  Back after I make a few tests!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Jazzy 1012

ASKER
Alright, also I have the zend version of php so it's not the lastest update. Thank you!
Ray Paseur

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

ASKER
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~
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

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.
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

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

Julian Hansen

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Ray Paseur

@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.
Julian Hansen

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.
Ray Paseur

... potential to grow ...
Yes, exactly.  If the instant answers do not solve the issues, these questions might be good candidates for E-E Gigs.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

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

"The format inside is wrong"