# ---------------------------------------------# USER CONFIGURATION (用户配置)# ---------------------------------------------$LiteDbDllPath="E:\Games\Playnite\LiteDB.dll"$DbFilePath="E:\Games\Playnite\library\games.db"# 查找内容,旧的路径$SearchText="F:\Games\"# 替换内容,{PlayniteDir} 是 Playnite 软件目录变量$ReplaceText="{PlayniteDir}\..\"$CollectionName="Game"$FieldName="InstallDirectory"# ---------------------------------------------# 1. Load LiteDB DLL (加载 LiteDB DLL)# ---------------------------------------------try{Add-Type-Path$LiteDbDllPathWrite-Host"LiteDB DLL loaded successfully."}catch{Write-Error"Failed to load LiteDB DLL. Please check the path: $LiteDbDllPath"exit}# ---------------------------------------------# 2. Define Variables and Types (定义变量和类型)# ---------------------------------------------$LiteDatabaseType=[LiteDB.LiteDatabase]$QueryType=[LiteDB.Query]$db=$null# Initialize $db outside try block# ---------------------------------------------# 3. Execute Database Update (执行数据库更新操作)# ---------------------------------------------try{# Establish database connection$db=New-Object$LiteDatabaseType-ArgumentList$DbFilePath$collection=$db.GetCollection($CollectionName)# Construct the query condition: Find documents where InstallDirectory starts with the search path# Using StartsWith is efficient as it uses the LiteDB index$queryCondition=$QueryType::StartsWith($FieldName,$SearchText)# Get the count of documents to be updated$count=$collection.Count($queryCondition)if($count-eq0){Write-Host"No records found matching the search text ('$SearchText')."}else{Write-Host"Found $count records to update..."$updatedCount=0# Find all documents matching the criteria and iterate$collection.Find($queryCondition)|ForEach-Object{$doc=$_# Get current value (as a string)$currentValue=$doc[$FieldName].AsString# Perform string replacement# Note: The 'Replace' method works on the string value$newValue=$currentValue.Replace($SearchText,$ReplaceText)# Update the BsonDocument field value$doc[$FieldName]=$newValue# Commit the update back to the databaseif($collection.Update($doc)){$updatedCount++}}Write-Host"Batch replacement completed. Successfully updated $updatedCount records."}}catch{Write-Error"Database operation failed:"Write-Error$_.Exception.Message}finally{# Ensure the database connection is closed and resources are releasedif($db-ne$null){$db.Dispose()Write-Host"Database connection closed."}}
LiteDB DLL loaded successfully.
Found 29 records to update...
Batch replacement completed. Successfully updated 29 records.
Database connection closed.