How to Check if a File is Read-Only in Batch Script
Before a script attempts to modify or delete a file, it's a crucial best practice to check its attributes. Trying to write to a read-only file will cause a permission error, halting your script unexpectedly. By checking the read-only status beforehand, you can build more intelligent scripts that can anticipate and gracefully handle these permission issues.
This guide will show you how to use the built-in ATTRIB command combined with FIND to reliably determine if a file is read-only, how to use this check in your script's logic, and how to create a practical script that avoids errors when deleting files.
The Core Command: ATTRIB
The ATTRIB command is the standard Windows utility for displaying or changing file attributes. The four main attributes are:
- R: Read-only
- A: Archive
- S: System
- H: Hidden
When you run ATTRIB on a single file, it prints the file's attributes at the beginning of the line.
C:\Temp> ATTRIB "readonly_file.txt"
I the following output, the R at the beginning of the line indicates that this file is read-only.
A R C:\Temp\readonly_file.txt
While, for a file that is not read-only, the R would be absent:
A C:\Temp\writable_file.txt
The Scripting Method: Combining ATTRIB with FIND
For a script to make a decision, it needs an automated way to check for that "R" attribute. We can achieve this by piping the output of ATTRIB into the FIND command. FIND will search the output for a specific string and set the %ERRORLEVEL% based on whether it was found.
ATTRIB "readonly_file.txt" | FIND " R "
ATTRIB "readonly_file.txt": Produces the line with the file's attributes.|: The pipe operator sends that output as input to the next command.FIND " R ": Searches the input for the characterRsurrounded by spaces. This specific pattern reliably identifies the read-only attribute in theATTRIBoutput.
If the file is read-only, FIND will match the line and set %ERRORLEVEL% to 0. If it's not read-only, FIND won't find a match and will set %ERRORLEVEL% to 1.
Using ERRORLEVEL for Script Logic
Now we can use a simple IF statement to check the ERRORLEVEL and determine the file's status.
This script checks a file and reports whether it is read-only.
@ECHO OFF
SET "FILENAME=readonly_file.txt"
REM Pipe the output to FIND and redirect FIND's own output to NUL
REM because we only care about the ERRORLEVEL it sets.
ATTRIB "%FILENAME%" | FIND " R " > NUL
IF %ERRORLEVEL% EQU 0 (
ECHO The file "%FILENAME%" is Read-Only.
) ELSE (
ECHO The file "%FILENAME%" is Writable.
)
Some example
Scenario 1: File is Read-Only
C:\Temp> ATTRIB +R readonly_file.txt
C:\Temp> check_readonly.bat
The file "readonly_file.txt" is Read-Only.
Scenario 2: File is Writable
C:\Temp> ATTRIB -R readonly_file.txt
C:\Temp> check_readonly.bat
The file "readonly_file.txt" is Writable.
Common Pitfalls and How to Solve Them
Problem: Handling "File Not Found" Errors
The biggest issue with this method is that the ATTRIB command will fail if the file doesn't exist. This will stop your script with an error.
Let's see the error:
C:\Temp> check_readonly.bat
File not found - non_existent_file.txt
Solution: Use IF EXIST First
The most robust solution is to always check if the file exists before you attempt to check its attributes.
@ECHO OFF
SET "FILENAME=my_file.txt"
IF NOT EXIST "%FILENAME%" (
ECHO Error: File does not exist.
GOTO :EOF
)
ATTRIB "%FILENAME%" | FIND " R " > NUL
IF %ERRORLEVEL% EQU 0 (
ECHO The file is Read-Only.
) ELSE (
ECHO The file is Writable.
)
This pattern makes your script much more reliable by handling a common failure case gracefully.
Practical Example: A Safe Delete Script
This script attempts to delete a file. Before doing so, it checks if the file is read-only. If it is, the script reports the problem and exits instead of failing on a "Permission Denied" error from the DEL command.
@ECHO OFF
SETLOCAL
SET "FILE_TO_DELETE=important_config.ini"
ECHO Attempting to delete "%FILE_TO_DELETE%"...
IF NOT EXIST "%FILE_TO_DELETE%" (
ECHO [INFO] File does not exist. Nothing to do.
GOTO :End
)
REM --- The Read-Only Check ---
ATTRIB "%FILE_TO_DELETE%" | FIND " R " > NUL
IF %ERRORLEVEL% EQU 0 (
ECHO [ERROR] Cannot delete file. It is marked as Read-Only.
GOTO :End
)
ECHO [SUCCESS] File is writable. Deleting now...
DEL "%FILE_TO_DELETE%"
:End
ECHO Script finished.
ENDLOCAL
An even more advanced version of this script could use ATTRIB -R "%FILE_TO_DELETE%" to automatically remove the read-only attribute before deleting.
Conclusion
Checking for a file's read-only attribute is a fundamental part of writing defensive and robust batch scripts. The ATTRIB command provides the necessary information, and piping its output to FIND allows you to translate that information into a simple ERRORLEVEL check.
For reliable scripting:
- Use the
ATTRIB "filename" | FIND " R " > NULpattern. - Check
%ERRORLEVEL%immediately afterward:0means read-only,1means writable. - Always perform an
IF EXISTcheck on the file first to prevent "File not found" errors.
By incorporating this simple check, you can prevent runtime errors and make your scripts smarter and more resilient.