C# Avalonia 專案編譯會在runtimes 資料夾內產生很多垃圾資料夾和垃圾檔案 如何使用BAT直接清除不必要的垃圾

C# Avalonia 專案編譯會在runtimes 資料夾內產生很多垃圾資料夾和垃圾檔案 如何使用BAT直接清除不必要的垃圾

C# Avalonia 專案編譯會在runtimes 資料夾內產生很多垃圾資料夾和垃圾檔案 如何使用BAT直接清除不必要的垃圾


資料來源: copilot+自己


問題00:我有一個runtimes資料夾 裡面有很多子檔案夾 且所有資檔案夾裡面又有檔案和檔案夾 

我要如何使用BAT的命令只保留win開頭的子目錄和他底下的檔案夾和目錄


bat_code

REM cleanup_runtimes.bat
@echo off
setlocal enabledelayedexpansion

REM 設定 ruintimes 資料夾的路徑
set "sourceDir=.\runtimes"

REM 遍歷所有子目錄
for /d %%i in ("%sourceDir%\*") do (
    set folderName=%%~nxi
    REM 檢查子目錄是否以 "win" 開頭
    if /i not "!folderName:~0,3!"=="win" (
        REM 刪除不以 "win" 開頭的子目錄
        rd /s /q "%%i"
    )
)

echo 清理完畢!
pause

問題01:那如我要改成 只留下 win win-x64 兩個子目錄和他的下面檔案 要如何修改


bat_code

@echo off
setlocal enabledelayedexpansion

REM 設定 runtimes 資料夾的路徑
set "sourceDir=.\runtimes"

REM 遍歷所有子目錄
for /d %%i in ("%sourceDir%\*") do (
    set folderName=%%~nxi
    REM 檢查子目錄是否以 "win" 或 "win-x64" 命名
    if /i not "!folderName!"=="win" if /i not "!folderName!"=="win-x64" (
        REM 刪除不符合條件的子目錄
        rd /s /q "%%i"
    )
)

echo 清理完畢!
pause


問題02:讓VS2022編譯專案時自動執行BAT清除不要了垃圾檔


VPOS_Avalonia.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <BuiltInComInteropSupport>true</BuiltInComInteropSupport>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
    <Platforms>AnyCPU;x64</Platforms>
    <ApplicationIcon>sys.ico</ApplicationIcon>
    <AssemblyName>VPOS</AssemblyName>
  </PropertyGroup>	
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
    <Deterministic>True</Deterministic>
    <Optimize>False</Optimize>
    <DebugType>full</DebugType>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
    <Deterministic>True</Deterministic>
    <Optimize>False</Optimize>
    <DebugType>full</DebugType>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
    <Deterministic>True</Deterministic>
    <Optimize>True</Optimize>
    <DebugType>full</DebugType>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
    <CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
    <Deterministic>True</Deterministic>
    <Optimize>True</Optimize>
    <DebugType>full</DebugType>
  </PropertyGroup>
  <PropertyGroup Label="Avalonia">
    <AvaloniaXamlIlDebuggerLaunch>False</AvaloniaXamlIlDebuggerLaunch>
    <AvaloniaXamlVerboseExceptions>False</AvaloniaXamlVerboseExceptions>
    <FileVersion>2.0.0.1</FileVersion>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="Models\" />
    <AvaloniaResource Include="Assets\**" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="sys.ico" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="AnimatedImage.Avalonia" Version="2.0.0" />
    <PackageReference Include="Avalonia" Version="11.2.3" />
    <PackageReference Include="Avalonia.Controls.DataGrid" Version="11.2.3" />
    <PackageReference Include="Avalonia.Desktop" Version="11.2.3" />
    <PackageReference Include="Avalonia.Themes.Fluent" Version="11.2.3" />
    <PackageReference Include="Avalonia.Fonts.Inter" Version="11.2.3" />
    <!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
    <PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.2.3" />
    <PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
    <PackageReference Include="Costura.Fody" Version="6.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Dapper" Version="2.1.35" />
    <PackageReference Include="Jint" Version="3.0.1" />
    <PackageReference Include="Microsoft.NET.ILLink.Tasks" Version="9.0.1" />
    <PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
    <PackageReference Include="System.Drawing.Common" Version="9.0.1" />
    <PackageReference Include="System.IO.Ports" Version="9.0.1" />
    <PackageReference Include="System.Management" Version="9.0.1" />
    <PackageReference Include="System.Text.Encoding.CodePages" Version="9.0.1" />
  </ItemGroup>

  <ItemGroup>
    <None Update="cleanup.bat">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="SQLite3.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>

	<!-- 新增一個全域的後建目標 -->
	<Target Name="CleanupAfterBuild" AfterTargets="Build;Rebuild;Publish">
		<Exec Command="cmd /c &quot;$(TargetDir)cleanup.bat&quot;" />
		<Exec Command="cmd /c del &quot;$(TargetDir)cleanup.bat&quot;" />
		<Exec Command="cmd /c del &quot;$(TargetDir)log.txt&quot;" />
	</Target>
</Project>


cleanup.bat

@echo off
setlocal enabledelayedexpansion

echo 開始清理>>%~dp0log.txt

REM 設定 runtimes 資料夾的路徑
set "sourceDir=%~dp0\runtimes"

REM 遍歷所有子目錄
for /d %%i in ("%sourceDir%\*") do (
    set folderName=%%~nxi
    echo 檢查資料夾: !folderName! >>%~dp0log.txt
    REM 檢查子目錄是否以 "win" 或 "win-x64" 命名
    if /i not "!folderName!"=="win" if /i not "!folderName!"=="win-x64" (
        REM 刪除不符合條件的子目錄
        rd /s /q "%%i"
        echo 刪除: %%i >>%~dp0log.txt
    )
)

One thought on “C# Avalonia 專案編譯會在runtimes 資料夾內產生很多垃圾資料夾和垃圾檔案 如何使用BAT直接清除不必要的垃圾

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *