ForeverTheOtherGuy66

FileAttributeTool.bat

Jan 12th, 2025 (edited)
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 8.16 KB | Source Code | 0 0
  1. @echo off
  2. title Advanced File Attribute Manager
  3.  
  4. :: Check for admin privileges
  5. net session >nul 2>&1
  6. if %errorlevel% neq 0 (
  7.     echo Please close this window, right-click the batch file, and select "Run as administrator".
  8.     pause
  9.     exit /b 1
  10. )
  11.  
  12. :: Initialize log file with timestamp (adjusted for locale-specific date format)
  13. for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (
  14.     set "logDate=%%c-%%a-%%b"
  15. )
  16. set "logFile=attribute_log_%logDate%.txt"
  17. call :GetCurrentTime
  18. echo Log initialized at %currentTime% > "%logFile%"
  19.  
  20. :MainMenu
  21. cls
  22. echo ======================================
  23. echo Advanced File Attribute Manager
  24. echo ======================================
  25. echo 1. Set attributes (Read-Only and Hidden)
  26. echo 2. Undo attributes
  27. echo 3. Batch process (Multiple files with filters)
  28. echo 4. Backup and Restore
  29. echo 5. Exit
  30. echo ======================================
  31. set /p choice=Choose an option [1-5]:
  32.  
  33. if "%choice%"=="1" goto SetAttributes
  34. if "%choice%"=="2" goto UndoAttributes
  35. if "%choice%"=="3" goto BatchProcess
  36. if "%choice%"=="4" goto BackupRestore
  37. if "%choice%"=="5" goto ExitMessage
  38. echo Invalid choice! Please select a valid option.
  39. pause
  40. goto MainMenu
  41.  
  42. :SetAttributes
  43. cls
  44. set /p filePath=Please enter the full file path:
  45.  
  46. :: Validate file existence
  47. if not exist "%filePath%" (
  48.     call :GetCurrentTime
  49.     echo %currentTime% ERROR: File not found - "%filePath%" >> "%logFile%"
  50.     echo File not found!
  51.     pause
  52.     goto MainMenu
  53. )
  54.  
  55. :: Take ownership and grant current user full control temporarily
  56. call :TakeOwnership "%filePath%"
  57. if %errorlevel% neq 0 goto MainMenu
  58.  
  59. :: Change the file owner to SYSTEM so that normal users cannot alter its attributes
  60. icacls "%filePath%" /setowner "SYSTEM" >nul 2>&1
  61.  
  62. :: Set the file attributes to Read-Only and Hidden
  63. attrib +r +h "%filePath%"
  64. if %errorlevel% neq 0 (
  65.     call :GetCurrentTime
  66.     echo %currentTime% ERROR: Failed to set attributes - "%filePath%" >> "%logFile%"
  67.     echo Failed to set attributes.
  68.     pause
  69.     goto MainMenu
  70. )
  71.  
  72. :: Remove modification permissions:
  73. :: - Remove inheritance
  74. :: - Grant SYSTEM and Administrators full control
  75. :: - Grant the current user only read permission
  76. icacls "%filePath%" /inheritance:r >nul 2>&1
  77. icacls "%filePath%" /grant "SYSTEM":F >nul 2>&1
  78. icacls "%filePath%" /grant "Administrators":F >nul 2>&1
  79. icacls "%filePath%" /grant "%username%":R >nul 2>&1
  80.  
  81. if %errorlevel%==0 (
  82.     call :GetCurrentTime
  83.     echo %currentTime% SUCCESS: Attributes set and locked - "%filePath%" >> "%logFile%"
  84.     echo File is now set to Read-Only and Hidden.
  85.     echo Only read access is allowed—any attempt to modify attributes will result in "access denied".
  86. ) else (
  87.     call :GetCurrentTime
  88.     echo %currentTime% ERROR: Failed to set permissions - "%filePath%" >> "%logFile%"
  89.     echo Failed to lock file permissions.
  90. )
  91.  
  92. pause
  93. goto MainMenu
  94.  
  95. :UndoAttributes
  96. cls
  97. set /p filePath=Enter the full file path to undo attributes:
  98.  
  99. if not exist "%filePath%" (
  100.     call :GetCurrentTime
  101.     echo %currentTime% ERROR: File not found - "%filePath%" >> "%logFile%"
  102.     echo File not found!
  103.     pause
  104.     goto MainMenu
  105. )
  106.  
  107. :: Take ownership so we can modify ACLs and remove the restrictions
  108. call :TakeOwnership "%filePath%"
  109. if %errorlevel% neq 0 goto MainMenu
  110.  
  111. :: Restore full control to the current user and re-enable inheritance
  112. icacls "%filePath%" /grant "%username%":F >nul 2>&1
  113. icacls "%filePath%" /inheritance:e >nul 2>&1
  114.  
  115. :: Remove the Read-Only and Hidden attributes
  116. attrib -r -h "%filePath%"
  117. if %errorlevel% neq 0 (
  118.     call :GetCurrentTime
  119.     echo %currentTime% ERROR: Failed to remove attributes - "%filePath%" >> "%logFile%"
  120.     echo Failed to remove attributes.
  121.     pause
  122.     goto MainMenu
  123. )
  124.  
  125. call :GetCurrentTime
  126. echo %currentTime% SUCCESS: Attributes and permissions unlocked - "%filePath%" >> "%logFile%"
  127. echo File attributes and permissions have been restored.
  128. pause
  129. goto MainMenu
  130.  
  131. :BatchProcess
  132. cls
  133. set /p dirPath=Please enter the directory path containing files to process:
  134.  
  135. if not exist "%dirPath%" (
  136.     call :GetCurrentTime
  137.     echo %currentTime% ERROR: Directory not found - "%dirPath%" >> "%logFile%"
  138.     echo Directory not found!
  139.     pause
  140.     goto MainMenu
  141. )
  142.  
  143. set /p fileExt=Enter file extension to filter (e.g., .txt), or press Enter for all files:
  144.  
  145. for %%F in ("%dirPath%\*%fileExt%") do (
  146.     call :TakeOwnership "%%F"
  147.     if %errorlevel% neq 0 goto MainMenu
  148.  
  149.     icacls "%%F" /setowner "SYSTEM" >nul 2>&1
  150.     attrib +r +h "%%F"
  151.     if %errorlevel% neq 0 (
  152.         call :GetCurrentTime
  153.         echo %currentTime% ERROR: Failed to set attributes - "%%F" >> "%logFile%"
  154.         echo ERROR: Failed to set attributes for "%%F".
  155.         goto MainMenu
  156.     )
  157.     icacls "%%F" /inheritance:r >nul 2>&1
  158.     icacls "%%F" /grant "SYSTEM":F >nul 2>&1
  159.     icacls "%%F" /grant "Administrators":F >nul 2>&1
  160.     icacls "%%F" /grant "%username%":R >nul 2>&1
  161.     if %errorlevel%==0 (
  162.         call :GetCurrentTime
  163.         echo %currentTime% SUCCESS: Processed "%%F" >> "%logFile%"
  164.     ) else (
  165.         call :GetCurrentTime
  166.         echo %currentTime% ERROR: Failed to set permissions - "%%F" >> "%logFile%"
  167.         echo ERROR: Failed to set permissions for "%%F".
  168.     )
  169. )
  170. call :GetCurrentTime
  171. echo %currentTime% Batch processing complete for "%dirPath%". >> "%logFile%"
  172. echo Batch processing complete! Check the log for details.
  173. pause
  174. goto MainMenu
  175.  
  176. :BackupRestore
  177. cls
  178. echo ======================================
  179. echo Backup and Restore Menu
  180. echo ======================================
  181. echo 1. Backup files before attribute changes
  182. echo 2. Restore files from backup
  183. echo 3. Return to main menu
  184. echo ======================================
  185. set /p backupChoice=Choose an option [1-3]:
  186.  
  187. if "%backupChoice%"=="1" goto BackupFiles
  188. if "%backupChoice%"=="2" goto RestoreFiles
  189. if "%backupChoice%"=="3" goto MainMenu
  190. echo Invalid choice! Returning to Backup and Restore Menu.
  191. pause
  192. goto BackupRestore
  193.  
  194. :BackupFiles
  195. cls
  196. set /p dirPath=Enter the directory path to back up files from:
  197. if not exist "%dirPath%" (
  198.     call :GetCurrentTime
  199.     echo %currentTime% ERROR: Directory not found for backup - "%dirPath%" >> "%logFile%"
  200.     echo Directory not found!
  201.     pause
  202.     goto BackupRestore
  203. )
  204.  
  205. set "backupDir=Backup"
  206. if not exist "%backupDir%" mkdir "%backupDir%"
  207. xcopy "%dirPath%\*" "%backupDir%\" /s /e /y >nul
  208. if %errorlevel%==0 (
  209.     call :GetCurrentTime
  210.     echo %currentTime% SUCCESS: Backup created from "%dirPath%" >> "%logFile%"
  211.     echo Backup successfully created in "%backupDir%".
  212. ) else (
  213.     call :GetCurrentTime
  214.     echo %currentTime% ERROR: Backup failed from "%dirPath%" >> "%logFile%"
  215.     echo Backup failed.
  216. )
  217. pause
  218. goto BackupRestore
  219.  
  220. :RestoreFiles
  221. cls
  222. set /p restorePath=Enter the directory path to restore files to:
  223. if not exist "Backup" (
  224.     call :GetCurrentTime
  225.     echo %currentTime% ERROR: Backup directory not found - "Backup" >> "%logFile%"
  226.     echo Backup directory not found!
  227.     pause
  228.     goto BackupRestore
  229. )
  230. xcopy "Backup\*" "%restorePath%\" /s /e /y >nul
  231. if %errorlevel%==0 (
  232.     call :GetCurrentTime
  233.     echo %currentTime% SUCCESS: Files restored to "%restorePath%" >> "%logFile%"
  234.     echo Files restored.
  235. ) else (
  236.     call :GetCurrentTime
  237.     echo %currentTime% ERROR: Restore failed to "%restorePath%" >> "%logFile%"
  238.     echo Restore failed.
  239. )
  240. pause
  241. goto BackupRestore
  242.  
  243. :TakeOwnership
  244. :: Take ownership and grant current user full control
  245. takeown /f "%~1" >nul 2>&1
  246. if %errorlevel% neq 0 (
  247.     call :GetCurrentTime
  248.     echo %currentTime% ERROR: Failed to take ownership - "%~1" >> "%logFile%"
  249.     echo Failed to take ownership of "%~1".
  250.     exit /b 1
  251. )
  252. icacls "%~1" /grant %username%:F >nul 2>&1
  253. if %errorlevel% neq 0 (
  254.     call :GetCurrentTime
  255.     echo %currentTime% ERROR: Failed to grant permissions - "%~1" >> "%logFile%"
  256.     echo Failed to grant permissions for "%~1".
  257.     exit /b 1
  258. )
  259. exit /b 0
  260.  
  261. :GetCurrentTime
  262. for /f "tokens=1-2 delims= " %%i in ('echo %time%') do (
  263.     set currentTime=[%date% %%i %%j]
  264. )
  265. exit /b
  266.  
  267. :ExitMessage
  268. cls
  269. echo Thanks for using the Advanced File Attribute Manager.
  270. echo Log saved to "%logFile%".
  271. timeout /t 5 >nul
  272. exit /b 0
  273.  
Advertisement
Add Comment
Please, Sign In to add comment