Tag Archives: Files

PHP: Stream a file line-by-line using a generator

This function will “stream” a file line-by-line, as a Generator.

Can be very useful if for example you need to process a big file, don’t want to read the whole thing into memory, but only process each line by itself.

function read($file)
{
    $fp = fopen($file, 'rb');

    while(($line = fgets($fp)) !== false)
        yield rtrim($line, "\r\n");

    fclose($fp);
}

// Usage
foreach(read('http://example.com') as $line)
{
    var_dump($line);
}

PowerShell: Read hashtable from a file

Had a file with the following kind of data.

10.0.0.1=alice.example.com
10.0.0.2=bob.example.com

Wanted to read this in as a hashtable so that I could use it for lookup in a script. Tried doing the following, but ended up with an array of hashtables instead of one hashtable. This is because Get-Content by default actually gives you an array of lines, which are then piped into ConvertFrom-StringData one by one.

PS> Get-Content .\hostnames.txt | ConvertFrom-StringData
PS> $names.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

PS> $names[0].GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Hashtable                                System.Object

Turns out it was easy to fix by adding the -raw parameter.

PS> $names = Get-Content -raw .\hostnames.txt | ConvertFrom-StringData
PS> $names.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Hashtable                                System.Object

PS> $names['10.0.0.1']
alice.example.com

PHP: Simple directory recursion

Keep running into scenarios where I need to scan through a file system and it’s actually pretty simple if you just know what classes to use. So… note to self and others:

// This should return false if there is something you want excluded
function filter($file, $key, $iterator)
{
    $exclude = array('.git');
    return ! in_array($file->getFilename(), $exclude);
}

// Recursive directory iterator for current directory, ignoring dots
$it = new RecursiveDirectoryIterator('.', FilesystemIterator::SKIP_DOTS);
// Wrapped by a filtering iterator with our filter function
$it = new RecursiveCallbackFilterIterator($it, 'filter');
// Wrapped by an iterator which automatically traverses children for us
$it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::SELF_FIRST);

// And then just loop :)
foreach($it as $file)
{
    echo str_repeat("\t", $it->getDepth())
        . $file->getRealPath()
        . PHP_EOL;
}

This skips the annoying dots, properly excludes directories you don’t want and pretty much works the way it should.

Convert windows-1252 to utf-8 on Windows

Tried to find out how to convert windows-1252 code files to utf-8 without messing up Norwegian characters today. Couldn’t really find anything good other than linux tools and php stuff. Finally, *facepalm*, I remembered it might be possible using Notepad… And sure enough, seems to work great. Just open up the windows-1252 encoded file in Notepad, then choose ‘Save as’ and set encoding to UTF-8.

Hopefully I won’t forget this the next time I need it… *sigh*

How to change or remove file extensions in Windows

Say you have a bunch of files and you want to quickly change or remove the file extension of all of them. Turns out that’s very simple to do with the command-line in Windows. I had no idea…

Change

ren *.old *.new

Will change all files with ‘old’ file extension to have the ‘new’ file extension.

Remove

ren *.old *.

Will remove the ‘old’ file extension.

Simple!