Jump to content

Recommended Posts

hey guys i have a file that was printed to txt from my windows dir format is like this

07/13/2009  08:04 PM            2,560 bootstr.dll

i'm curious if anyone knows a way in php to strip this line of the date and the spaces and just leave this from that line above

2,560;bootsrt.dll <-- so the size then a ; then the file name??

 

there is 1002 records to perform this on...

 

any ideas?

 

Link to comment
https://forums.phpfreaks.com/topic/223814-this-possible/
Share on other sites

$inputDir = dirname(__FILE__); // Replace with your starting directory...

$dir = new DirectoryIterator($inputDir);
foreach($dir as $fileinfo)
{
    if(!$fileinfo->isDot())
    {
    	// Replace this line with whatever size and filename processing you need...
        echo $fileinfo->getSize() . ';' . $fileinfo->getFilename() . "\n";
    }
}

Link to comment
https://forums.phpfreaks.com/topic/223814-this-possible/#findComment-1156830
Share on other sites

$lines = file('sample.txt'); // Replace sample.txt with the full path to your text file.
foreach($lines as $line)
{
$data = array();
$result = preg_match('/(?P<date>\S+)\s+(?P<time>\S+ \S\S)\s+(?P<size>[\d,]+)\s+(?P<name>.*)/', $line, $data);
if($result)
{
	// Replace this line with whatever size and filename processing you need...
	echo $data['size'] . ";" . $data['name'] . "\n";
}
}

Link to comment
https://forums.phpfreaks.com/topic/223814-this-possible/#findComment-1156858
Share on other sites

That is a bit more involved. Have you already created the database / tables? What database are you using (mysql, mssql, sqlite, etc)? What does the database schema look like?

We'll be able to help you further with these details, but in general your script will do something like:

 

$connection = new PDO(...); // This needs to be configured to your specific database settings

$lines = file('sample.txt');
foreach($lines as $line)
{
$data = array();
$result = preg_match('/(?P<date>\S+)\s+(?P<time>\S+ \S\S)\s+(?P<size>[\d,]+)\s+(?P<name>.*)/', $line, $data);
if($result)
{
                $sql = "INSERT into table_name (size, filename) VALUES ('{$data['size']}', '{$data['name']}')"; // Replace table_name, size, filename with the correct values from your database schema
	$connection->exec($sql);
}
}

Link to comment
https://forums.phpfreaks.com/topic/223814-this-possible/#findComment-1156930
Share on other sites

^^ above will run 1000 queries or how many lines you have in the file. Construct the query using the loop, run it outside of the loop.

 

Great examples as well nderevj!

 

Was thinking, if the separation in the middle is a tab, I'm sure you could use:

 

foreach($lines as $line){
  
  $leftright = explode("\t", $line);
  $sizename = explode(' ', $leftright);
  
  $size = $sizename[0];
  $name = $sizename[1];
  
}

Link to comment
https://forums.phpfreaks.com/topic/223814-this-possible/#findComment-1157036
Share on other sites

Anti-Moronic, you have a good point. My quick script is very inefficient.

 

Gathering all of the data to be inserted and constructing one SQL statement would cut down on database resources. Another thought I had was using prepared statements. PDO offers them- http://us.php.net/manual/en/pdo.prepared-statements.php.

Link to comment
https://forums.phpfreaks.com/topic/223814-this-possible/#findComment-1157209
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.