重装Windows后旧文件权限问题的修复

在重装Windows后,非系统盘上遗留的那些目录及文件,有时操作时会出现权限问题。原因是重装后系统认为用户 SID 变了,导致原本属于你的文件现在的“主人”变成了未知账户。

所以应该先 Takeown(拿所有权),再 Icacls(重置权限)。


手工执行

得到计算机名和用户名

1
whoami

记住它,比如:emileitx\emile

然后执行takeown和icacls

1
2
takeown /f D: /r /d y
icacls "D:" /grant "emileitx\emile":(OI)(CI)(F) /t /c

或者把以下代码存为bat脚本,自动化执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

:: ========================================
:: Check for administrator privileges
:: ========================================
@ECHO OFF&(PUSHD "%~DP0")&(REG QUERY "HKU\S-1-5-19">NUL 2>&1)||(
powershell -Command "Start-Process '%~sdpnx0' -Verb RunAs"&&EXIT)

echo ========================================
echo Running with administrator privileges
echo ========================================
echo.

cd /d "%~dp0"

set "LOG_FILE=D:\DiskPermissionFix.log"
set "CURRENT_USER=%USERNAME%"
set "COMPUTER_NAME=%COMPUTERNAME%"

echo Log file will be saved to: %LOG_FILE%
echo.

:MENU
cls
echo ========================================
echo Disk Permission Fix Tool
echo ========================================
echo.
echo Current User = !CURRENT_USER!
echo Computer Name = !COMPUTER_NAME!
echo.
echo 0 - Exit
echo [Press Enter] - Start from D drive
echo Input a drive letter (e.g., e) to start from that drive
echo.

set /p STARTDRV=Enter the starting drive letter and press Enter (default is D):

if "%STARTDRV%"=="0" exit /b
if "%STARTDRV%"=="" set STARTDRV=D

:: Convert to uppercase and get first character only
set STARTDRV=%STARTDRV:~0,1%
for %%Z in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
if /i "%STARTDRV%"=="%%Z" set STARTDRV=%%Z
)

echo.
echo Will start from drive %STARTDRV% for permission fix...
echo.
pause

:: ========================================
:: Enumerate all available fixed drives
:: Only get NTFS drives, exclude C: drive, optical drives, and removable drives
:: DriveType 3 = Local Disk
:: ========================================
echo.
echo Scanning for NTFS drives...
echo.

set DRIVES=
set COUNT=0

:: Simple method: check each drive letter directly
for %%D in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
if exist %%D:\ (
:: Try to get file system type
for /f "tokens=*" %%F in ('fsutil fsinfo volumeinfo %%D: 2^>nul ^| findstr /C:"File System Name"') do (
echo %%D: %%F
echo %%F | findstr /I "NTFS" >nul
if !errorlevel! EQU 0 (
set DRIVES=!DRIVES! %%D:
set /a COUNT+=1
echo Found NTFS drive: %%D:
)
)
)
)

echo.
echo Total NTFS drives found: !COUNT!
echo Drives list: %DRIVES%
echo.

if !COUNT! EQU 0 (
echo No NTFS drives found excluding C drive!
echo.
echo Troubleshooting: Listing all existing drives...
for %%D in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
if exist %%D:\ (
echo Drive %%D: exists
vol %%D:
)
)
pause
goto MENU
)

:: ========================================
:: Start from the selected drive
:: ========================================
set DO_RUN=0
set PROCESSED=0

for %%D in (%DRIVES%) do (
set THIS=%%D
set THISDRV=%%D
set THISDRV=!THISDRV:~0,1!

:: Check if we have reached the selected start drive
if /i "!THISDRV!"=="%STARTDRV%" set DO_RUN=1

if !DO_RUN! == 1 (
set /a PROCESSED+=1
echo.
echo ====================================================
echo Processing drive %%D ...
echo ====================================================
echo ---------------------------------------------------- >> "%LOG_FILE%"
echo Starting processing for %%D ... >> "%LOG_FILE%"
echo [%date% %time%] Processing %%D >> "%LOG_FILE%"

:: Run takeown command
echo.
echo [Step 1/2] Taking ownership of %%D\ ...
echo This may take several minutes, please be patient...
echo.
echo Executing takeown operation... >> "%LOG_FILE%"
takeown /f %%D\ /r /d y >> "%LOG_FILE%" 2>&1
if !errorlevel! neq 0 (
echo [ERROR] takeown failed for %%D! >> "%LOG_FILE%"
echo [ERROR] takeown failed! Check the log!
) else (
echo [SUCCESS] takeown completed for %%D! >> "%LOG_FILE%"
echo [SUCCESS] takeown completed!
)

:: Run icacls command
echo.
echo [Step 2/2] Setting permissions on %%D\ ...
echo This may take several minutes, please be patient...
echo.
echo Executing icacls operation... >> "%LOG_FILE%"
icacls %%D\ /grant "%CURRENT_USER%":^(OI^)^(CI^)^(F^) /t /c >> "%LOG_FILE%" 2>&1
if !errorlevel! neq 0 (
echo [ERROR] icacls failed for %%D! >> "%LOG_FILE%"
echo [ERROR] icacls failed! Check the log!
) else (
echo [SUCCESS] icacls completed for %%D! >> "%LOG_FILE%"
echo [SUCCESS] icacls completed!
)

echo.
echo Drive %%D processing finished.
)
)

if !PROCESSED! EQU 0 (
echo.
echo [WARNING] No drives were processed!
echo Starting drive %STARTDRV% was not found in the available drives list.
echo.
)

echo.
echo *** All processing completed ***
echo Log file: %LOG_FILE%
echo.
pause
goto MENU


参考资料
豆包