Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- title Advanced File Attribute Manager
- :: Check for admin privileges
- net session >nul 2>&1
- if %errorlevel% neq 0 (
- echo Please close this window, right-click the batch file, and select "Run as administrator".
- pause
- exit /b 1
- )
- :: Initialize log file with timestamp (adjusted for locale-specific date format)
- for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (
- set "logDate=%%c-%%a-%%b"
- )
- set "logFile=attribute_log_%logDate%.txt"
- call :GetCurrentTime
- echo Log initialized at %currentTime% > "%logFile%"
- :MainMenu
- cls
- echo ======================================
- echo Advanced File Attribute Manager
- echo ======================================
- echo 1. Set attributes (Read-Only and Hidden)
- echo 2. Undo attributes
- echo 3. Batch process (Multiple files with filters)
- echo 4. Backup and Restore
- echo 5. Exit
- echo ======================================
- set /p choice=Choose an option [1-5]:
- if "%choice%"=="1" goto SetAttributes
- if "%choice%"=="2" goto UndoAttributes
- if "%choice%"=="3" goto BatchProcess
- if "%choice%"=="4" goto BackupRestore
- if "%choice%"=="5" goto ExitMessage
- echo Invalid choice! Please select a valid option.
- pause
- goto MainMenu
- :SetAttributes
- cls
- set /p filePath=Please enter the full file path:
- :: Validate file existence
- if not exist "%filePath%" (
- call :GetCurrentTime
- echo %currentTime% ERROR: File not found - "%filePath%" >> "%logFile%"
- echo File not found!
- pause
- goto MainMenu
- )
- :: Take ownership and grant current user full control temporarily
- call :TakeOwnership "%filePath%"
- if %errorlevel% neq 0 goto MainMenu
- :: Change the file owner to SYSTEM so that normal users cannot alter its attributes
- icacls "%filePath%" /setowner "SYSTEM" >nul 2>&1
- :: Set the file attributes to Read-Only and Hidden
- attrib +r +h "%filePath%"
- if %errorlevel% neq 0 (
- call :GetCurrentTime
- echo %currentTime% ERROR: Failed to set attributes - "%filePath%" >> "%logFile%"
- echo Failed to set attributes.
- pause
- goto MainMenu
- )
- :: Remove modification permissions:
- :: - Remove inheritance
- :: - Grant SYSTEM and Administrators full control
- :: - Grant the current user only read permission
- icacls "%filePath%" /inheritance:r >nul 2>&1
- icacls "%filePath%" /grant "SYSTEM":F >nul 2>&1
- icacls "%filePath%" /grant "Administrators":F >nul 2>&1
- icacls "%filePath%" /grant "%username%":R >nul 2>&1
- if %errorlevel%==0 (
- call :GetCurrentTime
- echo %currentTime% SUCCESS: Attributes set and locked - "%filePath%" >> "%logFile%"
- echo File is now set to Read-Only and Hidden.
- echo Only read access is allowed—any attempt to modify attributes will result in "access denied".
- ) else (
- call :GetCurrentTime
- echo %currentTime% ERROR: Failed to set permissions - "%filePath%" >> "%logFile%"
- echo Failed to lock file permissions.
- )
- pause
- goto MainMenu
- :UndoAttributes
- cls
- set /p filePath=Enter the full file path to undo attributes:
- if not exist "%filePath%" (
- call :GetCurrentTime
- echo %currentTime% ERROR: File not found - "%filePath%" >> "%logFile%"
- echo File not found!
- pause
- goto MainMenu
- )
- :: Take ownership so we can modify ACLs and remove the restrictions
- call :TakeOwnership "%filePath%"
- if %errorlevel% neq 0 goto MainMenu
- :: Restore full control to the current user and re-enable inheritance
- icacls "%filePath%" /grant "%username%":F >nul 2>&1
- icacls "%filePath%" /inheritance:e >nul 2>&1
- :: Remove the Read-Only and Hidden attributes
- attrib -r -h "%filePath%"
- if %errorlevel% neq 0 (
- call :GetCurrentTime
- echo %currentTime% ERROR: Failed to remove attributes - "%filePath%" >> "%logFile%"
- echo Failed to remove attributes.
- pause
- goto MainMenu
- )
- call :GetCurrentTime
- echo %currentTime% SUCCESS: Attributes and permissions unlocked - "%filePath%" >> "%logFile%"
- echo File attributes and permissions have been restored.
- pause
- goto MainMenu
- :BatchProcess
- cls
- set /p dirPath=Please enter the directory path containing files to process:
- if not exist "%dirPath%" (
- call :GetCurrentTime
- echo %currentTime% ERROR: Directory not found - "%dirPath%" >> "%logFile%"
- echo Directory not found!
- pause
- goto MainMenu
- )
- set /p fileExt=Enter file extension to filter (e.g., .txt), or press Enter for all files:
- for %%F in ("%dirPath%\*%fileExt%") do (
- call :TakeOwnership "%%F"
- if %errorlevel% neq 0 goto MainMenu
- icacls "%%F" /setowner "SYSTEM" >nul 2>&1
- attrib +r +h "%%F"
- if %errorlevel% neq 0 (
- call :GetCurrentTime
- echo %currentTime% ERROR: Failed to set attributes - "%%F" >> "%logFile%"
- echo ERROR: Failed to set attributes for "%%F".
- goto MainMenu
- )
- icacls "%%F" /inheritance:r >nul 2>&1
- icacls "%%F" /grant "SYSTEM":F >nul 2>&1
- icacls "%%F" /grant "Administrators":F >nul 2>&1
- icacls "%%F" /grant "%username%":R >nul 2>&1
- if %errorlevel%==0 (
- call :GetCurrentTime
- echo %currentTime% SUCCESS: Processed "%%F" >> "%logFile%"
- ) else (
- call :GetCurrentTime
- echo %currentTime% ERROR: Failed to set permissions - "%%F" >> "%logFile%"
- echo ERROR: Failed to set permissions for "%%F".
- )
- )
- call :GetCurrentTime
- echo %currentTime% Batch processing complete for "%dirPath%". >> "%logFile%"
- echo Batch processing complete! Check the log for details.
- pause
- goto MainMenu
- :BackupRestore
- cls
- echo ======================================
- echo Backup and Restore Menu
- echo ======================================
- echo 1. Backup files before attribute changes
- echo 2. Restore files from backup
- echo 3. Return to main menu
- echo ======================================
- set /p backupChoice=Choose an option [1-3]:
- if "%backupChoice%"=="1" goto BackupFiles
- if "%backupChoice%"=="2" goto RestoreFiles
- if "%backupChoice%"=="3" goto MainMenu
- echo Invalid choice! Returning to Backup and Restore Menu.
- pause
- goto BackupRestore
- :BackupFiles
- cls
- set /p dirPath=Enter the directory path to back up files from:
- if not exist "%dirPath%" (
- call :GetCurrentTime
- echo %currentTime% ERROR: Directory not found for backup - "%dirPath%" >> "%logFile%"
- echo Directory not found!
- pause
- goto BackupRestore
- )
- set "backupDir=Backup"
- if not exist "%backupDir%" mkdir "%backupDir%"
- xcopy "%dirPath%\*" "%backupDir%\" /s /e /y >nul
- if %errorlevel%==0 (
- call :GetCurrentTime
- echo %currentTime% SUCCESS: Backup created from "%dirPath%" >> "%logFile%"
- echo Backup successfully created in "%backupDir%".
- ) else (
- call :GetCurrentTime
- echo %currentTime% ERROR: Backup failed from "%dirPath%" >> "%logFile%"
- echo Backup failed.
- )
- pause
- goto BackupRestore
- :RestoreFiles
- cls
- set /p restorePath=Enter the directory path to restore files to:
- if not exist "Backup" (
- call :GetCurrentTime
- echo %currentTime% ERROR: Backup directory not found - "Backup" >> "%logFile%"
- echo Backup directory not found!
- pause
- goto BackupRestore
- )
- xcopy "Backup\*" "%restorePath%\" /s /e /y >nul
- if %errorlevel%==0 (
- call :GetCurrentTime
- echo %currentTime% SUCCESS: Files restored to "%restorePath%" >> "%logFile%"
- echo Files restored.
- ) else (
- call :GetCurrentTime
- echo %currentTime% ERROR: Restore failed to "%restorePath%" >> "%logFile%"
- echo Restore failed.
- )
- pause
- goto BackupRestore
- :TakeOwnership
- :: Take ownership and grant current user full control
- takeown /f "%~1" >nul 2>&1
- if %errorlevel% neq 0 (
- call :GetCurrentTime
- echo %currentTime% ERROR: Failed to take ownership - "%~1" >> "%logFile%"
- echo Failed to take ownership of "%~1".
- exit /b 1
- )
- icacls "%~1" /grant %username%:F >nul 2>&1
- if %errorlevel% neq 0 (
- call :GetCurrentTime
- echo %currentTime% ERROR: Failed to grant permissions - "%~1" >> "%logFile%"
- echo Failed to grant permissions for "%~1".
- exit /b 1
- )
- exit /b 0
- :GetCurrentTime
- for /f "tokens=1-2 delims= " %%i in ('echo %time%') do (
- set currentTime=[%date% %%i %%j]
- )
- exit /b
- :ExitMessage
- cls
- echo Thanks for using the Advanced File Attribute Manager.
- echo Log saved to "%logFile%".
- timeout /t 5 >nul
- exit /b 0
Advertisement
Add Comment
Please, Sign In to add comment