desjardins2010 Posted January 8, 2011 Share Posted January 8, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/223814-this-possible/ Share on other sites More sharing options...
nderevj Posted January 8, 2011 Share Posted January 8, 2011 $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"; } } Quote Link to comment https://forums.phpfreaks.com/topic/223814-this-possible/#findComment-1156830 Share on other sites More sharing options...
desjardins2010 Posted January 8, 2011 Author Share Posted January 8, 2011 Hey mate; thanks for the input can you give me a little info on your script here, like how it works? what I need to do? Quote Link to comment https://forums.phpfreaks.com/topic/223814-this-possible/#findComment-1156834 Share on other sites More sharing options...
nderevj Posted January 8, 2011 Share Posted January 8, 2011 I just noticed that your trying to parse a text file that has the contents of a directory. My script actually reads files from the file system. My mistake. Let me post another solution. Quote Link to comment https://forums.phpfreaks.com/topic/223814-this-possible/#findComment-1156840 Share on other sites More sharing options...
nderevj Posted January 8, 2011 Share Posted January 8, 2011 Does your list of files contain directories as well? Quote Link to comment https://forums.phpfreaks.com/topic/223814-this-possible/#findComment-1156848 Share on other sites More sharing options...
nderevj Posted January 8, 2011 Share Posted January 8, 2011 $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"; } } Quote Link to comment https://forums.phpfreaks.com/topic/223814-this-possible/#findComment-1156858 Share on other sites More sharing options...
desjardins2010 Posted January 9, 2011 Author Share Posted January 9, 2011 ok, that got me a file now i need to store this information in a database the current format is 152121,"acb.ini" so size,"filename" need two cols file size and filename possible with this format Quote Link to comment https://forums.phpfreaks.com/topic/223814-this-possible/#findComment-1156912 Share on other sites More sharing options...
nderevj Posted January 9, 2011 Share Posted January 9, 2011 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); } } Quote Link to comment https://forums.phpfreaks.com/topic/223814-this-possible/#findComment-1156930 Share on other sites More sharing options...
Anti-Moronic Posted January 9, 2011 Share Posted January 9, 2011 ^^ 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]; } Quote Link to comment https://forums.phpfreaks.com/topic/223814-this-possible/#findComment-1157036 Share on other sites More sharing options...
nderevj Posted January 10, 2011 Share Posted January 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/223814-this-possible/#findComment-1157209 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.