Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get-LocateLite.ps1
$GetDll = $true
if ($PSScriptRoot) { $location = $PSScriptRoot } else { $location = (Get-Location).Path }
$dll = "$location\System.Data.SQLite.dll"
if ($GetDll) {
$wc = New-Object System.Net.WebClient
$url = "https://netnerds.net/System.Data.SQLite.dll"
$wc.DownloadFile($url,$dll)
}
if (!(Test-Path $dll)) { throw "System.Data.SQLite.dll not found. You can isntall SQL Lite, or automatically download the DLL using -GetDll" }
[void][Reflection.Assembly]::LoadFile($dll)
$database = "$location\locate.sqlite"
$connString = "Data Source=$database"
if (!(Test-Path $database)) {
# Create database
[void][System.Data.SQLite.SQLiteConnection]::CreateFile($database);
$connection = New-Object System.Data.SQLite.SQLiteConnection($connString)
$connection.Open()
# Create table, check if primary key is automatically unique
$table = "CREATE TABLE [Files] ([Name] nvarchar(450) PRIMARY KEY)"
$command = $connection.CreateCommand()
$command.CommandText = $table
$null = $command.ExecuteNonQuery()
$command.Dispose()
$connection.Close()
$connection.Dispose()
}
#########################################################################################
$connection = New-Object System.Data.SQLite.SQLiteConnection($connString)
$connection.Open()
$command = $connection.CreateCommand()
# delete table
$command.CommandText = "DROP TABLE [Files]"
[void]$command.ExecuteNonQuery()
$command.CommandText = "CREATE TABLE [Files] ([Name] nvarchar(450) PRIMARY KEY)"
[void]$command.ExecuteNonQuery()
$transaction = $connection.BeginTransaction()
Function Get-Filenames ($path) {
Set-Variable -ErrorAction SilentlyContinue -Name files
# Get Directories
try
{
$files = [IO.Directory]::GetFiles($path)
[System.IO.DirectoryInfo]$directoryInfo = New-Object IO.DirectoryInfo($path)
$folders = $directoryInfo.GetDirectories() | Where-Object {$_.Name -ne "`$Recycle.Bin" -and $folder -ne "System Volume Information" }
} catch { $folders = @()}
foreach($filename in $files)
{
$filename = $filename.replace('\\','\')
$filename = $filename.replace("'","''")
$command.CommandText = "insert into files values ('$filename')"
[void]$command.ExecuteNonQuery()
}
# Create list of non-indexed directories
$exculde = @($env:APPDATA)
$exclude += $env:LOCALAPPDATA
$exclude += $env:ProgramData
$exclude += $env:TMP
$exclude += $env:TEMP
try {
foreach($folder in $folders)
{
if ($exclude -notcontains "$path$folder") { Get-Filenames("$path\$folder")}
}
} catch {}
Remove-Variable -ErrorAction SilentlyContinue -Name files
}
Measure-command { Get-Filenames("C:\") }
$transaction.Commit()
$command.Dispose()
$connection.Close()
$connection.Dispose()
<#
##################################################
$connection = New-Object System.Data.SQLite.SQLiteConnection($connString)
$connection.Open()
$command = $connection.CreateCommand()
Measure-Command {
$file = "SqlServerCe"
$file = $file.Replace("`*","`%")
$sql = "select name from files where name like '%$file%'"
$command.CommandText = $sql
$datatable = New-Object System.Data.DataTable
$datatable.load($command.ExecuteReader())
}
$command.Dispose()
$connection.Close()
$connection.Dispose()
$datatable.name
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment