Set USB flash drive to READONLY in Windows

By raccoon on Sep 27, 2016

Here are two Windows .cmd batch file scripts that will enable you to make your removable flash drives read-only and writeable again.

Reasons for doing this?:

  • Prevent a virus infected computer from infecting your files.
  • Prevent a public computer's antivirus program from deleting your files.
  • Prevent some idiot from deleting or corrupting your porn. Your porn should corrupt them!
  • Prevent a smart tv, photo kiosk, car stereo, android, etc from adding folders and shit to your drive.

These scripts basically perform a simple series of DISKPART commands, namely ATTRIBUTES DISK SET READONLY and ATTRIBUTES DISK CLEAR READONLY, upon the drive which the scripts are executed from.
They must be Run As Administrator -- Right-click the files in Windows Explorer.

ProTip: You obviously can't create or modify files when your drive is set to read-only. So, um, copy both files before testing this.
Additionally, some drives may appear like files are being written to them, but you're probably just seeing Windows write caching. Eject and reinsert the drive to confirm that changes are impossible and discarded while the drive is set to read-only.


file: disk-readonly.cmd

@rem disk-readonly.cmd script by Raccoon 2016
@rem DISKPART: ATTR DISK SET/CLEAR READONLY
@echo off
fltmc >nul 2>&1 && ( goto admin ) || ( goto noadmin )
:noadmin
echo This script must be 'Run As Administrator'.
echo Exiting...
echo.
pause
exit

:admin
echo Setting drive %~d0 to READONLY...
echo ^>^> ARE YOU SURE? ^<^<
echo.
pause
(echo sel vol %~d0 & echo list vol & echo attr disk set readonly & echo detail disk) | diskpart
echo.
echo.
if %ERRORLEVEL% == 0 (
  echo SUCCESS! Drive %~d0 should now be READONLY.
) else (
  echo Failure setting %~d0 to READONLY.
)
echo.
pause end-of-script

file: disk-writeable.cmd

@rem disk-writeable.cmd script by Raccoon 2016
@rem DISKPART: ATTR DISK SET/CLEAR READONLY
@echo off
fltmc >nul 2>&1 && ( goto admin ) || ( goto noadmin )
:noadmin
echo This script must be 'Run As Administrator'.
echo Exiting...
echo.
pause
exit

:admin
echo Setting drive %~d0 to WRITEABLE...
echo ^>^> ARE YOU SURE? ^<^<
echo.
pause
(echo sel vol %~d0 & echo list vol & echo attr disk clear readonly & echo detail disk) | diskpart
echo.
echo.
if %ERRORLEVEL% == 0 (
  echo SUCCESS! Drive %~d0 should now be WRITEABLE.
) else (
  echo Failure setting %~d0 to WRITEABLE.
)
echo.
pause end-of-script

Enjoy!

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.