Skip to content

Instantly share code, notes, and snippets.

@LiuQixuan
Last active September 7, 2023 02:04
Show Gist options
  • Save LiuQixuan/2b829d4ebe0039efa1ed7135895c0731 to your computer and use it in GitHub Desktop.
Save LiuQixuan/2b829d4ebe0039efa1ed7135895c0731 to your computer and use it in GitHub Desktop.
[GenshinImpact][YuanShen][原神]deleteOldFiles
$deleteFilesPath = ".\deletefiles.txt"
$GamePath = "E:\Games\Genshin Impact\Genshin Impact game"
$github = "https://gist.github.com/LiuQixuan/2b829d4ebe0039efa1ed7135895c0731"
function Protect-Exit([string]$keyValue='.',[string]$addtext=''){
if($addtext -ne ''){
$addtext = "`t" + $addtext
}
$keyText = ''
if($keyValue -eq '.'){
$keyText = "任意键"
}else{
$keyText = $keyValue
}
$isContinue = Read-Host "$keyText`退出$addtext"
if($isContinue -match "[$keyValue]" ){
exit
}
}
Class Logger{
$time = ""
$logPath = ""
Logger(){
$this.time = Get-Date -UFormat "%Y_%m_%d_%H_%M"
$this.logPath = Join-Path -Path $PSScriptRoot -ChildPath "deletefiles.Debug.$($this.time).log"
}
Logger([string]$Path){
$this.time = Get-Date -UFormat "%Y_%m_%d_%H_%M"
$this.logPath = Join-Path -Path (Get-ItemProperty -Path $Path).Root -ChildPath "deletefiles.$($this.time).log"
}
[void]refreshTime(){
$this.time = Get-Date -UFormat "%Y_%m_%d_%H_%M_%S"
}
[void]print([string]$Message,[string]$type){
Write-Host -Object "[$($this.time)]-[$($type.ToUpper())]-$Message"
}
[void]print([string]$Message){
$this.print($Message,'Error')
}
[void]log([string]$Message,[string]$type){
Write-Host -Object "[$($this.time)]-[$($type.ToUpper())]-$Message">>$($this.logPath)
}
[void]log([string]$Message){
$this.log($Message,'Error')
}
[void]write([string]$Message,[string]$type){
"[$($this.time)]-[$($type.ToUpper())]-$Message">>$($this.logPath)
}
[void]write([string]$Message){
$this.write($Message,'Error')
}
}
[Logger]$logger = [Logger]::new()
if(Test-Path -Path $deleteFilesPath){
$logger.print('[Action]:检测到升级包中的deletefiles.txt文件,准备删除旧文件','info')
$delList = Get-Content $deleteFilesPath
foreach($item in $delList){
if(Test-Path -Path (Join-Path -Path $GamePath -ChildPath $item)){
Remove-Item -Path (Join-Path -Path $GamePath -ChildPath $item)
}else{
$logger.write("[FileNotFound]:$item")
}
}
Write-Host "旧文件删除完成,现在可以关闭窗口.`n如有错误请把$($logger.logPath)`的内容发到项目留言中寻求帮助.`n项目地址:$github"
}else{
$logger.log("[FileNotFound:deletefiles.txt]:deletefiles.txt 未找到,确认$deleteFilesPath 路径正确")
}
Protect-Exit -keyValue '.'
@LiuQixuan
Copy link
Author

LiuQixuan commented Sep 7, 2023

此脚本用于删除无用的历史文件,适用于使用脚本跨版本升级,虽然进入游戏下载完残缺文件后也能正常游戏,但由于未正确删除之前版本的无用文件导致游戏占用空间过大.
各版本游戏升级包中的deletefiles.txt文件将从即日起开始收集,发布地址日后更新.

使用方法:

  1. $GamePath 改成自己的游戏路径(路径以Genshin Impact game结尾)
  2. deletefiles.txt文件与脚本放到同一个文件夹
  3. 脚本拖到powershell 里运行

注意:如果脚本报错,请使用最新的powershell 7.x.x运行.由于api限制不再兼容powershell 5.x.x
问题难以解决携带deletefiles.Debug.xxxxx.log在留言区一起讨论

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment