Skip to content

Instantly share code, notes, and snippets.

@gifnksm
Created December 22, 2009 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gifnksm/261775 to your computer and use it in GitHub Desktop.
Save gifnksm/261775 to your computer and use it in GitHub Desktop.
Firefoxの全てのプロファイルディレクトリ以下の*.sqliteを最適化するPowershellスクリプト。
param ([switch]$force)
$appProfDir = join-path $env:appdata "Mozilla\Firefox\Profiles"
$localProfDir = join-path $env:localappdata "Mozilla\Firefox\Profiles"
# http://csharper.blog57.fc2.com/blog-entry-206.html
function global:Invoke-Process
{
param ([string] $processPath, [string]$processArgs, [int]$timeoutMilliseconds = [System.Threading.Timeout]::Infinite)
trap
{
if ($() -ne $process)
{
$process.Dispose();
}
break;
}
if ([String]::IsNullOrEmpty($processPath)) { throw "引数 processPath が null または空の文字列です。"; }
$process = New-Object "System.Diagnostics.Process";
$process.StartInfo = New-Object "System.Diagnostics.ProcessStartInfo" @($processPath, $processArgs);
$process.StartInfo.WorkingDirectory = (Get-Location).Path;
$process.StartInfo.RedirectStandardOutput = $True;
$process.StartInfo.RedirectStandardError = $True;
$process.StartInfo.UseShellExecute = $False;
$process.Start() | Out-Null;
$message = $process.StandardOutput.ReadToEnd();
$errorMessage = $process.StandardError.ReadToEnd();
$complete = $process.WaitForExit($timeoutMilliseconds);
if (!$complete)
{
$process.Kill();
}
$result =
@{
"IsSuccess" = ($process.ExitCode -eq 0);
"IsComplete" = $complete;
"Message" = $message;
"ErrorMessage" = $errorMessage;
};
$process.Dispose();
return $result;
}
function parse-dir($path) {
write-host "$path";
get-childitem $path -force | where {
$_.Mode -match "^d"
} | foreach {
$profName = (split-path $_ -leaf).split(".", 2)[1];
write-host " * $profName";
get-childitem $_.fullname -filter "*.sqlite" | where {
$bak = "$($_.fullname).bak";
$force -or -not (test-path $bak) -or ($_.lastwritetime -gt (get-item $bak).lastwritetime)
} | foreach {
$file = $_;
$bakPath = "$($file.fullname).bak";
write-host " + $($file.name): " -nonewline;
copy-item $file.fullname $bakPath;
$bak = get-item $bakPath;
$res1 = Invoke-Process "sqlite3" "`"$($file.fullname)`" vacuum";
$res2 = Invoke-Process "sqlite3" "`"$($file.fullname)`" reindex";
if($res1.IsSuccess -and $res2.IsSuccess) {
# renew fileinfo (filesize)
$file = get-item $file.fullname
write-host "optimized" -foregroundcolor "blue";
$diff = 100 * (1 - $file.length / $bak.length);
write-host (" {0:N0} => {1:N0} bytes ({2:N2})%" -f
($bak.length, $file.length, $diff));
$bak.set_lastwritetime($(get-date));
} else {
write-host "error" -foregroundcolor "red";
write-host " vacuum: " -nonewline;
write-host $res1.ErrorMessage -foregroundcolor "red" -nonewline;
write-host " reindex: " -nonewline;
write-host $res2.ErrorMessage -foregroundcolor "red" -nonewline;
$bak.set_lastwritetime($file.lastwritetime - 10)
}
};
};
write-host "";
}
parse-dir $appProfDir
parse-dir $localProfDir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment