China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-29 03:10
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Hide the drive letters of my computer View 2,352 Replies 15
Original Poster Posted 2009-04-13 06:44 ·  中国 广东 广州 电信
初级用户
Credits 34
Posts 12
Joined 2009-02-18 14:07
17-year member
UID 139172
Gender Male
Status Offline
```
@echo off&setlocal enabledelayedexpansion
title Drive Hiding
:setvar
set needhidedrv=Not entered yet.
set value=0
set str=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

:HIDEDRV
cls
set n=1
set drv=endinput
echo The drives that need to be hidden that have been entered:
echo ===============================================================================

echo.

echo.
echo %needhidedrv%
echo.

echo.
echo ===============================================================================

echo.
echo For example, if you need to hide drive C, please enter c or C, then press Enter. If you need to enter multiple drive letters, continue to enter. If you want to complete the input, just press Enter directly. Enter all to hide all. In the initial state, pressing Enter directly will cancel the hiding
echo.&echo.&echo.&echo.&echo.&echo.&echo.&echo.&echo.
set /p drv=Please enter:
if "%drv%"=="endinput" goto exec
if "%drv%"=="all" set value=0X3FFFFFF&goto exec
if "%needhidedrv%"=="Not entered yet." set needhidedrv=
for %%i in (%str%) do call set drv=%%drv:%%i=%%i%%
for %%i in (%needhidedrv%) do (if "%drv%"=="%%i" goto HIDEDRV)
for %%i in (%str%) do if "%drv%"=="%%i" (goto :next) else (set /a n=n+n)
if "%needhidedrv%"=="" set needhidedrv=Not entered yet.
goto HIDEDRV
:next
set needhidedrv=%needhidedrv%%drv%
set /a value=%value%+%n%
goto HIDEDRV

:exec
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer /f /v NoDrives /t REG_DWORD /d %value%
taskkill /f /im explorer.exe >nul & start explorer &cls
echo Operation completed, press any key to exit
pause>nul
exit&exit
```

Note: The core code is three for statements. The first one is used to convert lowercase to uppercase. The second one is used to judge whether the input is repeated. The third one is used to assign a value to each drive letter.

Special thanks to netbenton and the elder brother who gave an example code.
Thank you very much, netbenton. You made me realize a new step.
Regarding replying with gpupdate, my antivirus software will give an alarm...
============Moderator, please see my follow-up post

[ Last edited by darkkid on 2009-4-15 at 22:07 ]
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
HAT +4 2009-04-13 07:30
netbenton +4 2009-04-13 20:38
Floor 2 Posted 2009-04-13 06:48 ·  中国 湖北 黄石 电信
中级用户
★★
Credits 330
Posts 244
Joined 2006-04-14 14:07
20-year member
UID 53823
Gender Male
From 湖北
Status Offline
Floor 3 Posted 2009-04-13 07:19 ·  中国 北京 联通
银牌会员
★★★★
[b]看你妹啊[/b]
Credits 1,488
Posts 1,357
Joined 2006-05-20 12:00
20-year member
UID 55770
Status Offline
Never did I dream that I could write such a long piece of code

有问题请发论坛或者自行搜索,再短消息问我的统统是SB
Floor 4 Posted 2009-04-13 07:32 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
New user post, add points for encouragement.
It is suggested that the original poster can try to shorten this code of more than 300 lines to within 100 lines.
Floor 5 Posted 2009-04-13 07:49 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
This can save multiple if...goto and multiple for...judgments to see if it has been input before

set "str= a b c d e f g h i j k l m n o p q r s t u v w x y z "
if not "!str: %drv% =!"=="!str!" (
if not "!ain: %drv% =!"=="!ain!" (set "ain=%ain% %drv% "&goto :part%drv%)
)

The correspondence between drive letters and numbers is:
a 1
b 2
c 4
d 8
e 16
f 32
g 64
h 128
...

In this way, the corresponding value can be obtained:
set n=1
for %%a in (%str%) do if "%drv%"=="%%a" (goto :ok) else (set/a n=n+n)
:ok
Then try to convert it to hexadecimal, which can save the practice of multiple branches.

With the above method, the code can be controlled within 50 lines
The 楼主 can give it a try

[ Last edited by netbenton on 2009-4-13 at 06:15 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
darkkid +1 2009-04-13 13:49
精简
=> 个人网志
Floor 6 Posted 2009-04-13 08:08 ·  中国 广东 广州 电信
初级用户
Credits 34
Posts 12
Joined 2009-02-18 14:07
17-year member
UID 139172
Gender Male
Status Offline
Back to netbenton

Got your idea.

At the current stage, it's difficult for me to understand your code. I'll take my time to figure it out.

[ Last edited by darkkid on 2009-4-13 at 08:26 ]
Floor 7 Posted 2009-04-13 10:26 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Collected a long time ago
@echo off

:inp
cls
set Drv=
echo Enter the number "0" to unhide, enter several letters to hide the corresponding disks.
set/p Drv=Enter disk (e.g.: EaF)-

if "%Drv%"=="" goto :inp
if %Drv%==0 set TT=00&goto :hidrv

::Convert lowercase to uppercase and output in order and remove duplicates
set mat=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
for %%d in (%mat%) do (
 call set _%%d=
)
:lp
if not defined _%Drv:~,1% set "_%Drv:~,1%=1"
set "Drv=%Drv:~1%"
if defined Drv goto lp
set n=1
set n10=
set Dnum=
for %%m in (%mat%) do (
 call :setn n
 if defined _%%m (
  call :sshow %%m Drv Dnum n2 n10
 )
)

::Display
echo.
echo The disks to hide are %Drv%, total disks.

::Convert to hexadecimal
:reg16
setlocal enableDelayedExpansion
call :A %n10%

::Registry binary value expression
::T3 to BIN
set TT=
:bi
set/a x+=1
set Tn=0%T3:~-2%
set Tn=!Tn:~-2!
set T3=%T3:~,-2%
set TT=!TT!!Tn!
if not #%T3%==# goto :bi

::Hide disk
::-=Core code=-
:hidrv
set TT=%TT%000000
reg add HKCU\SoftWare\Microsoft\Windows\CurrentVersion\Policies\Explorer /v NoDrives /t REG_BINARY /d %TT:~,8% /f

echo End explorer process to take effect. Press any key to end and open My Computer to view
pause>nul
taskkill /f /im explorer.exe>nul
endlocal
start/min "" explorer "%~dp0"
start explorer ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}

ping -n 3 127.1>nul
goto :eof

::Call label
:sshow
 set %2=%Drv%%1
 set/a %3+=1
 set/a %4=n/2
 set/a %5+=n2
 goto :eof
:setn
 set/a %1*=2
 goto :eof
:A
 set str=0123456789ABCDEF
 set T1=%1
 SET T3=
:B
 set/A T2=T1%%16
 SET T2=!str:~%t2%,1!
 SET/A T1=%T1%/16
 SET T3=%T2%%T3%
 IF %T1%==0 GOTO :EOF
 GOTO B
Floor 8 Posted 2009-04-13 11:00 ·  中国 福建 三明 电信
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
19-year member
UID 59307
Status Offline
Reply to zh159:

It's really an honor that someone has bookmarked this code. Truly, truly honored.
┌───────┐
├→学习→实践→┤
└───────┘
Floor 9 Posted 2009-04-13 20:39 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
Not bad, newbie, add points,

Actually, I'm not that "old" either. Hehe
精简
=> 个人网志
Floor 10 Posted 2009-04-13 23:16 ·  中国 河北 唐山 电信
初级用户
★★
Credits 85
Posts 52
Joined 2009-01-10 18:22
17-year member
UID 136322
Gender Male
From 湘乡
Status Offline
You can try to hide your system disk... It has a very good effect... Anyway, I have tried it... Even opening web pages can't be opened. Friends who are interested can
Floor 11 Posted 2009-04-14 00:14 ·  中国 重庆 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
A rough look-through, and a few questions are raised:
:: Explanation, the function of this batch processing is to hide the disk drive letters in Windows Explorer, which protects personal privacy to a certain extent. Anyone can edit this batch processing with Notepad to make its function more powerful and more efficient. Xiaomin, April 12, 2009, sharing here for learning batch processing well.
@echo off&setlocal enabledelayedexpansion
:setvar
set needhidedrv=Not yet entered.
set value=0
set str=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
set n=1

:HIDEDRV
::::::::::::Besides the @ before echo off, so many @ are used. Is it for fun? Or something else?
@cls
set drv=endinput
@echo The drive letters that have been entered to hide:
@echo ===============================================================================
@echo.
@echo.
@echo %needhidedrv%
@echo.
@echo.
@echo ===============================================================================
@echo.
@echo For example, if you need to hide C drive, enter c or C, press Enter. If you need to enter multiple drive letters, continue input. If you want to complete input, just press Enter. Enter all to hide all, press Enter to cancel hide.
@echo.&@echo.&@echo.
set /p drv=Please input:
::::::::::::If user input a double quote, code abnormal.
if "%drv%"=="endinput" goto exec
if "%drv%"=="endinput" goto exec
if "%needhidedrv%"=="Not yet entered." set needhidedrv=
for %%i in (%str%) do call set drv=%%drv:%%i=%%i%%
::::::::::::Add /i to below if to ignore case. Above for redundant?
for %%i in (%needhidedrv%) do (if "%drv%"=="%%i" goto HIDEDRV)
for %%i in (%str%) if "%drv%"=="%%i" (set needhidedrv's %%drv) set /a value=value+n
goto next
:next
set needhidedrv=%needhidedrv%%drv%
exec@cls
reg delete HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer /f /v NoDrives
::::::::::::reg add overwrite. Above reg delete redundant?
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer /f /v NoDrives /t REG_DWORD /d %value%
taskkill /f /IM explorer.exe >nul
start explorer.exe
@echo Operation complete, press any key exit
pause>nul
exit&exit
Floor 12 Posted 2009-04-14 02:14 ·  中国 江西 赣州 电信
银牌会员
★★★★
Credits 2,025
Posts 1,122
Joined 2007-09-05 20:15
18-year member
UID 96653
Gender Male
Status Offline
You can use gpupdate.exe /force instead of restarting the explorer.exe process.
Floor 13 Posted 2009-04-14 10:23 ·  中国 广东 广州 电信
初级用户
Credits 34
Posts 12
Joined 2009-02-18 14:07
17-year member
UID 139172
Gender Male
Status Offline
Fixed a small error by moving set n=1 outside of :hidedrv, which caused incorrect assignments for all but the first drive when hiding multiple drives. Thanks to tireless here as well.

The following is in response to moderator HAT:

First, regarding the @ symbol, it's optional in my code. You can delete it if you find it obtrusive since I already have echo off on the first line. Adding @ before each line is equivalent to echo off.

The input anomaly you mentioned should be when entering for the first time, showing echo message as off? Actually, it's not a big deal. All strings other than abcdefg... will have this issue, and it's completely fine to ignore it. It's mainly a legacy from my initial display of "No input yet". When I clear the display list, echo can't display an empty string.

Regarding my case conversion, well, I said I'm a bit of a newbie. I didn't know about this parameter in if. Thanks to the moderator for the instruction. If I want to use this parameter, changing my str's uppercase A B C D... to lowercase a b c d... will make it run, but it won't look as nice when running, showing all lowercase. I won't change it; I prefer it to be nicer looking.


Finally, the last "no it" is just because I didn't know.

Now I need to go to school, and internet access is not convenient. I'll work on it again when I get home on the weekend
Floor 14 Posted 2009-04-15 02:36 ·  中国 重庆 沙坪坝区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Regarding the issue of users entering a double quote, come back to discuss it when you encounter a situation where you can't ignore it in the future. For now, just use it as you insist.
Floor 15 Posted 2009-04-15 02:52 ·  中国 广东 广州 电信
初级用户
Credits 34
Posts 12
Joined 2009-02-18 14:07
17-year member
UID 139172
Gender Male
Status Offline
Entering double quotes is at least okay here, analyze specific problems specifically, we'll calculate then.
Forum Jump: