JBin
JBin

Reputation: 491

Batch file - Switch USB from read/write to read-only and vice versa

I was wondering if you can write a Batch file to make a USB from read/write to read-only and visa versa?

What the program should do is, if you double click on the batch file it searches for the USB drive, check if it is read/write or just read-only then swaps it to the other setting.

EDIT I know the name of the USB and also the drive letter. EDIT

I need this for work where we use USB drives to install programs on peoples laptops, we used CD's before, but they get lost inside.

Code would be appreciated, but directions to sites will help as well.

Upvotes: 2

Views: 4275

Answers (2)

Poypoyan
Poypoyan

Reputation: 466

It is possible to do this is Batch (via Diskpart command), but it will be VERY convoluted.

Anyways, this code detects removable drives (so floppies, I think, are included), creates a menu, then swaps the setting (read-only/read-write). It makes flash drives "write-protected", but I tested my code...

@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

EDIT: My only concern here is that this is language dependent, so this will not work if the language is not English.

EDIT #2: Forgive me from misunderstanding the problem. If you already have a USB name to be modified, then try this: (Make sure there are no 2 flash drives with the same name that are plugged in at the same time.)

@echo off

::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::Put the name of flash drive here... (Case-sensitive)
set "name=[PUT NAME HERE]"

::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::Detect that removable drive using DiskPart...
set "disk_num="
for /f "tokens=1-2 delims= " %%A in (
   '^(echo list disk^)^|diskpart^|findstr /ic:"Online"'
) do (
   for /f "tokens=1-4 delims= " %%W in (
      '^(echo select disk %%B^&echo detail disk^)^|diskpart^|findstr /ic:"Removable"'
   ) do (
      if "%%Z"=="%name%" set "disk_num=%%B"
   )
)
if not defined disk_num (
   echo Flash drive '%name%' not found.
   goto end_this
)

::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::Get and set the setting...
for /f "tokens=1-2 delims=: " %%F in (
   '^(echo select disk %disk_num%^&echo attribute disk^)^|diskpart^|findstr /bic:"Read-Only"'
) do (
   set "ro_state=%%G"
)

if /i "%ro_state%" equ "No" (
   (echo select disk %disk_num%&echo attribute disk set readonly)|diskpart >nul
   echo Flash drive '%name%' is now read-only from being read/write.
) else (
   (echo select disk %disk_num%&echo attribute disk clear readonly)|diskpart >nul
   echo Flash drive '%name%' is now read/write from being read-only.
)

:end_this
pause
exit /b

EDIT #3: For Windows 8 and up, Run the Batch Files as Administrator for this to work.

Upvotes: 0

Drew
Drew

Reputation: 4030

Tested and works flawlessly with the 4 drives I have tested.

I have the below for you if you would like. It requires 3 files though. The batch then 1 text file per each Read only or Read Write command. Simple setup but requires you to have an input, it will overwrite what ever it currently is to what ever you select.

Thanks JBin for advising that it needs to be run as Administrator, I forgot that was a thing.

cls
@Echo OFF
@Echo Toggle between read only and Read write
:choice
set /P c=Change to [R]ead Only or Read/[W]rite?
if /I "%c%" EQU "R" goto :read
if /I "%c%" EQU "W" goto :write

:read
DISKPART /s Readonly.txt
cls
@Echo  -----------------------
@Echo ^| Disk is now READ ONLY ^|
@Echo  -----------------------
@Echo.
goto :choice

:write
DISKPART /s Readwrite.txt
cls
@Echo  ------------------------
@Echo ^| Disk is now READ WRITE ^|
@Echo  ------------------------
@Echo.
goto :choice

Make two text files with the below in them. Name them what ever you like but dont forget to change the *.txt in the batch. This is also assuming you only have 1 usb device plugged into the computer

Readonly.txt file:

Sel disk 1
att dis set readonly
exit

Readwrite.txt file:

sel disk 1
att dis clear readonly
exit

Upvotes: 2

Related Questions