Batch file needed for USB write and read only.

jobsp90

New member
Joined
Dec 29, 2024
Messages
1
Reaction score
0
I have a requirement for a office purpose. I want to give a usb pendrive write protected and read only permission, for any system I connect, the data shoudnt get deleted anytime in the pendrive. So I know through dispart from cmd we can do this but can anyone help me to create a batch file so the user (non IT) can do it from their own pc without any fuss just by clicking the batch file shortcut rather than typing the commands from command prompy dispart. So pls help me.
 

Xploit Machine

Well-known member
Joined
Nov 29, 2022
Messages
1,207
Reaction score
102
I have a requirement for a office purpose. I want to give a usb pendrive write protected and read only permission, for any system I connect, the data shoudnt get deleted anytime in the pendrive. So I know through dispart from cmd we can do this but can anyone help me to create a batch file so the user (non IT) can do it from their own pc without any fuss just by clicking the batch file shortcut rather than typing the commands from command prompy dispart. So pls help me.

Just save below file as <DESIREDNAME>.bat and run it .. it has both LOCK and UNLOCK function :)

@echo off
setlocal enabledelayedexpansion

::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: Detect removable drives using DiskPart...
set "diskcnt=0"
for /f "tokens=1-2 delims= " %%A in (
'^(echo list disk^)^|diskpart^|findstr /ic:"Online"'
) do (
for /f "tokens=1-3 delims= " %%X in (
'^(echo select disk %%B^&echo detail disk^)^|diskpart^|findstr /ic:"Removable"'
) do (
set /a diskcnt+=1
set "vol!diskcnt!=%%B_%%Z:"
)
)

::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:: After detecting drives, print a menu...

echo.The script detected %diskcnt% removable drive/s:
echo.
if %diskcnt% equ 0 echo No drives found.
for /l %%X in (1,1,%diskcnt%) do (
echo. %%X. Drive !vol%%X:~2,2!
)
echo.
:choice_loop
set /p "choice=Choose a Number: "
if !choice! geq 1 if !choice! leq %diskcnt% goto :doIt
goto choice_loop


::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:: Get and set the setting...
:doIt
echo.
set "disknum=!vol%choice%:~0,1!"
for /f "tokens=1-2 delims=:" %%F in (
'^(echo select disk %disknum%^&echo attribute disk^)^|diskpart^|findstr /bic:"Read-Only"'
) do (
set "ro_state=%%G"&set "ro_state=!ro_state: =!"
)
if /i "%ro_state%" equ "No" (
(echo select disk %disknum%&echo attribute disk set readonly)|diskpart >nul
echo Drive !vol%choice%:~2,2! is now read-only from being read/write.
) else (
(echo select disk %disknum%&echo attribute disk clear readonly)|diskpart >nul
echo Drive !vol%choice%:~2,2! is now read/write from being read-only.
)
endlocal
pause
 

its-deepti

New member
Joined
Dec 14, 2024
Messages
2
Reaction score
0
To create a batch file that toggles USB drives between write and read-only modes, use the Windows Registry Editor command. Save the following script as a .bat file:

@echo off
echo Select mode:
echo 1. Enable USB Read and Write
echo 2. Enable USB Read-Only
set /p choice=Enter your choice (1 or 2):

if "%choice%"=="1" (
reg add "HKLM\System\CurrentControlSet\Control\StorageDevicePolicies" /v WriteProtect /t REG_DWORD /d 0 /f
echo USB Write enabled.
) else if "%choice%"=="2" (
reg add "HKLM\System\CurrentControlSet\Control\StorageDevicePolicies" /v WriteProtect /t REG_DWORD /d 1 /f
echo USB set to Read-Only.
) else (
echo Invalid choice.
)
pause
 

Xploit Machine

Well-known member
Joined
Nov 29, 2022
Messages
1,207
Reaction score
102
To create a batch file that toggles USB drives between write and read-only modes, use the Windows Registry Editor command. Save the following script as a .bat file:

@echo off
echo Select mode:
echo 1. Enable USB Read and Write
echo 2. Enable USB Read-Only
set /p choice=Enter your choice (1 or 2):

if "%choice%"=="1" (
reg add "HKLM\System\CurrentControlSet\Control\StorageDevicePolicies" /v WriteProtect /t REG_DWORD /d 0 /f
echo USB Write enabled.
) else if "%choice%"=="2" (
reg add "HKLM\System\CurrentControlSet\Control\StorageDevicePolicies" /v WriteProtect /t REG_DWORD /d 1 /f
echo USB set to Read-Only.
) else (
echo Invalid choice.
)
pause

U tested? .. show result ..
 

RasulGreen

Member
Joined
Oct 22, 2024
Messages
15
Reaction score
0
To create a user-friendly batch file that toggles USB write protection, ensure the script runs with administrative rights, as it modifies the registry. Include a check for admin privileges and automatically create the required StorageDevicePolicies key if it doesn’t exist.

The script should display clear success or failure messages, apply the write protection globally to all USB devices, and include an easy-to-understand menu with options. I

t’s also important to test the script on multiple systems to ensure it works as expected. The provided enhanced script handles these points and is simple enough for non-IT users
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top