找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 514|回复: 1

[注册表类] 【分享】静默转移用户文件夹

[复制链接]
发表于 2026-2-7 22:35:21 | 显示全部楼层 |阅读模式
使用 DeepSeek 生成自动扫描选择本地磁盘,整合这个帖子的代码,并经过精简优化。
特点:
自动扫描所有本地磁盘:
排除系统盘
根据 Drivers\ 和 Users\ 文件夹的优先级选择最佳驱动器
将所有"反馈"信息都精简掉。
  1. <#*,:
  2. @echo off
  3. cd /d "%~dp0"
  4. set "batchfile=%~f0"
  5. Powershell -ExecutionPolicy Bypass -C "Set-Location ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create([IO.File]::ReadAllText($env:batchfile,[Text.Encoding]::Default)))"
  6. exit /b
  7. #>

  8. # 1. 获取符合条件的驱动器
  9. $selected = $null
  10. $top_priority = 0

  11. # Windows 7的Get-WmiObject写法
  12. try {
  13.     $allDrives = Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" -ErrorAction Stop
  14. } catch {
  15.     # 如果Get-WmiObject失败,尝试备用方法
  16.     $allDrives = @()
  17.     Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Root -like "*:" } | ForEach-Object {
  18.         $driveLetter = $_.Root.Substring(0,1)
  19.         $allDrives += New-Object PSObject -Property @{
  20.             DeviceID = "$driveLetter`:"
  21.         }
  22.     }
  23. }

  24. foreach ($driveObj in $allDrives) {
  25.     $drive_letter = $driveObj.DeviceID.Substring(0,1)
  26.    
  27.     # 跳过C盘
  28.     if ($drive_letter -eq "C") { continue }
  29.    
  30.     $drive_path = "$drive_letter`:"
  31.    
  32.     if (Test-Path $drive_path) {
  33.         # 排除系统盘
  34.         if (-not (Test-Path "$drive_path\Windows\explorer.exe")) {
  35.             $priority = 0
  36.             $has_drivers = 0
  37.             $has_users = 0
  38.             
  39.             if (Test-Path "$drive_path\Drivers") { $has_drivers = 1 }
  40.             if (Test-Path "$drive_path\Users") { $has_users = 1 }
  41.             
  42.             # 计算优先级
  43.             if ($has_drivers -eq 1 -and $has_users -eq 1) {
  44.                 $priority = 3
  45.             } elseif ($has_drivers -eq 1) {
  46.                 $priority = 2
  47.             } elseif ($has_users -eq 1) {
  48.                 $priority = 1
  49.             }
  50.             
  51.             # 选择优先级最高的驱动器
  52.             if ($priority -gt $top_priority) {
  53.                 $top_priority = $priority
  54.                 $selected = "$drive_letter`:"
  55.             } elseif ($priority -eq $top_priority -and $selected -eq $null) {
  56.                 $selected = "$drive_letter`:"
  57.             }
  58.         }
  59.     }
  60. }

  61. if ($selected -eq $null) { exit }

  62. $BaseDir = "$selected$env:HOMEPATH"

  63. # 2. 文件夹GUID映射
  64. $FolderMap = @{
  65.     "Downloads"   = "{374DE290-123F-4565-9164-39C4925E467B}"
  66.     "Saved Games" = "{4C5C32FF-BB9D-43B0-B5B4-2D72E54EAAA4}"
  67.     "Contacts"    = "{56784854-C6CB-462B-8169-88E350ACB882}"
  68.     "Searches"    = "{7D1D3A04-DEBB-4115-95CF-2F29DA2920DA}"
  69.     "Links"       = "{BFB9D5E0-C6A9-404C-B2B2-AE6DB6AF4968}"
  70.     "Desktop"     = "{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}"
  71.     "Favorites"   = "{1777F761-68AD-4D8A-87BD-30B759FA33DD}"
  72.     "Music"       = "{4BD8D571-6D19-48D3-BE97-422220080E43}"
  73.     "Pictures"    = "{33E28130-4E1E-4676-835A-98395C3BC3BB}"
  74.     "Videos"      = "{18989B1D-99B5-455B-841C-AB7C74E4DDFC}"
  75.     "Documents"   = "{FDD39AD0-238F-46AF-ADB4-6C85480369C7}"
  76. }

  77. # 3. C#代码
  78. $CSource = @"
  79. using System;
  80. using System.Runtime.InteropServices;

  81. public class ShellManager {
  82.     [DllImport("shell32.dll", CharSet = CharSet.Unicode)]
  83.     public static extern void SHSetKnownFolderPath(
  84.         [MarshalAs(UnmanagedType.LPStruct)] Guid rfid,
  85.         uint dwFlags,
  86.         IntPtr hToken,
  87.         string pszPath
  88.     );
  89.    
  90.     [DllImport("shell32.dll")]
  91.     public static extern void SHChangeNotify(
  92.         uint wEventId,
  93.         uint uFlags,
  94.         IntPtr dwItem1,
  95.         IntPtr dwItem2
  96.     );
  97. }
  98. "@

  99. # 检查类型是否已存在
  100. $typeExists = $false
  101. try {
  102.     $type = [ShellManager]
  103.     $typeExists = $true
  104. } catch {
  105.     $typeExists = $false
  106. }

  107. if (-not $typeExists) {
  108.     Add-Type -TypeDefinition $CSource -ErrorAction SilentlyContinue
  109. }

  110. # 4. 设置文件夹路径
  111. foreach ($Name in $FolderMap.Keys) {
  112.     $Target = Join-Path $BaseDir $Name
  113.    
  114.     # 创建目录(如果不存在)
  115.     if (-not (Test-Path $Target)) {
  116.         try {
  117.             New-Item -Path $Target -ItemType Directory -Force | Out-Null
  118.         } catch {
  119.             continue
  120.         }
  121.     }
  122.    
  123.     # 设置文件夹路径
  124.     try {
  125.         $guidString = $FolderMap[$Name]
  126.         # Windows 7需要包含大括号的GUID字符串
  127.         $guid = New-Object Guid($guidString)
  128.         [ShellManager]::SHSetKnownFolderPath($guid, 0, [IntPtr]::Zero, $Target)
  129.     } catch {
  130.         # 静默失败,继续处理其他文件夹
  131.     }
  132. }

  133. # 5. 通知系统更新
  134. try {
  135.     [ShellManager]::SHChangeNotify(0x08000000, 0x0000, [IntPtr]::Zero, [IntPtr]::Zero)
  136. } catch {
  137.     # 忽略错误
  138. }
复制代码
代码如有不妥的地方请老大们指正,谢谢!
发表于 2026-2-9 18:42:19 | 显示全部楼层
本帖最后由 aloha20200628 于 2026-2-9 19:10 编辑

回复 1# chishingchan

针对 '获取符合条件的驱动器' 给一个批处脚本如下,运行结果会列出当前全部驱动器的级别(3,2,1,0)...

  1. @echo off &if "%~1"=="" ("%~f0" 1|sort /r &pause&exit/b)
  2. for /f "tokens=1* delims= " %%a in ('fsutil fsinfo drives') do for %%d in (%%b) do if /i "%%d" neq "%SystemDrive%" (
  3.    set "n=0"
  4.    if exist "%%dDrivers" set/a "n+=2"
  5.    if exist "%%dUsers" set/a "n+=1"
  6.    call echo,%%n%% %%d)
  7. exit/b
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 05:42 , Processed in 0.017938 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表