|
|
使用 DeepSeek 生成自动扫描选择本地磁盘,整合这个帖子的代码,并经过精简优化。
特点:
自动扫描所有本地磁盘:
排除系统盘
根据 Drivers\ 和 Users\ 文件夹的优先级选择最佳驱动器
将所有"反馈"信息都精简掉。- <#*,:
- @echo off
- cd /d "%~dp0"
- set "batchfile=%~f0"
- Powershell -ExecutionPolicy Bypass -C "Set-Location ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create([IO.File]::ReadAllText($env:batchfile,[Text.Encoding]::Default)))"
- exit /b
- #>
- # 1. 获取符合条件的驱动器
- $selected = $null
- $top_priority = 0
- # Windows 7的Get-WmiObject写法
- try {
- $allDrives = Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" -ErrorAction Stop
- } catch {
- # 如果Get-WmiObject失败,尝试备用方法
- $allDrives = @()
- Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Root -like "*:" } | ForEach-Object {
- $driveLetter = $_.Root.Substring(0,1)
- $allDrives += New-Object PSObject -Property @{
- DeviceID = "$driveLetter`:"
- }
- }
- }
- foreach ($driveObj in $allDrives) {
- $drive_letter = $driveObj.DeviceID.Substring(0,1)
-
- # 跳过C盘
- if ($drive_letter -eq "C") { continue }
-
- $drive_path = "$drive_letter`:"
-
- if (Test-Path $drive_path) {
- # 排除系统盘
- if (-not (Test-Path "$drive_path\Windows\explorer.exe")) {
- $priority = 0
- $has_drivers = 0
- $has_users = 0
-
- if (Test-Path "$drive_path\Drivers") { $has_drivers = 1 }
- if (Test-Path "$drive_path\Users") { $has_users = 1 }
-
- # 计算优先级
- if ($has_drivers -eq 1 -and $has_users -eq 1) {
- $priority = 3
- } elseif ($has_drivers -eq 1) {
- $priority = 2
- } elseif ($has_users -eq 1) {
- $priority = 1
- }
-
- # 选择优先级最高的驱动器
- if ($priority -gt $top_priority) {
- $top_priority = $priority
- $selected = "$drive_letter`:"
- } elseif ($priority -eq $top_priority -and $selected -eq $null) {
- $selected = "$drive_letter`:"
- }
- }
- }
- }
- if ($selected -eq $null) { exit }
- $BaseDir = "$selected$env:HOMEPATH"
- # 2. 文件夹GUID映射
- $FolderMap = @{
- "Downloads" = "{374DE290-123F-4565-9164-39C4925E467B}"
- "Saved Games" = "{4C5C32FF-BB9D-43B0-B5B4-2D72E54EAAA4}"
- "Contacts" = "{56784854-C6CB-462B-8169-88E350ACB882}"
- "Searches" = "{7D1D3A04-DEBB-4115-95CF-2F29DA2920DA}"
- "Links" = "{BFB9D5E0-C6A9-404C-B2B2-AE6DB6AF4968}"
- "Desktop" = "{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}"
- "Favorites" = "{1777F761-68AD-4D8A-87BD-30B759FA33DD}"
- "Music" = "{4BD8D571-6D19-48D3-BE97-422220080E43}"
- "Pictures" = "{33E28130-4E1E-4676-835A-98395C3BC3BB}"
- "Videos" = "{18989B1D-99B5-455B-841C-AB7C74E4DDFC}"
- "Documents" = "{FDD39AD0-238F-46AF-ADB4-6C85480369C7}"
- }
- # 3. C#代码
- $CSource = @"
- using System;
- using System.Runtime.InteropServices;
- public class ShellManager {
- [DllImport("shell32.dll", CharSet = CharSet.Unicode)]
- public static extern void SHSetKnownFolderPath(
- [MarshalAs(UnmanagedType.LPStruct)] Guid rfid,
- uint dwFlags,
- IntPtr hToken,
- string pszPath
- );
-
- [DllImport("shell32.dll")]
- public static extern void SHChangeNotify(
- uint wEventId,
- uint uFlags,
- IntPtr dwItem1,
- IntPtr dwItem2
- );
- }
- "@
- # 检查类型是否已存在
- $typeExists = $false
- try {
- $type = [ShellManager]
- $typeExists = $true
- } catch {
- $typeExists = $false
- }
- if (-not $typeExists) {
- Add-Type -TypeDefinition $CSource -ErrorAction SilentlyContinue
- }
- # 4. 设置文件夹路径
- foreach ($Name in $FolderMap.Keys) {
- $Target = Join-Path $BaseDir $Name
-
- # 创建目录(如果不存在)
- if (-not (Test-Path $Target)) {
- try {
- New-Item -Path $Target -ItemType Directory -Force | Out-Null
- } catch {
- continue
- }
- }
-
- # 设置文件夹路径
- try {
- $guidString = $FolderMap[$Name]
- # Windows 7需要包含大括号的GUID字符串
- $guid = New-Object Guid($guidString)
- [ShellManager]::SHSetKnownFolderPath($guid, 0, [IntPtr]::Zero, $Target)
- } catch {
- # 静默失败,继续处理其他文件夹
- }
- }
- # 5. 通知系统更新
- try {
- [ShellManager]::SHChangeNotify(0x08000000, 0x0000, [IntPtr]::Zero, [IntPtr]::Zero)
- } catch {
- # 忽略错误
- }
复制代码 代码如有不妥的地方请老大们指正,谢谢! |
|