-1

I have been meaning to make a .bat that turns on or off some windows settings so I could bind it to a keyboard shortcut. I made a .bat file that attempts to add a registry key NoDesktop so desktop icons could be hidden, if it runs a check in reg and is unable to find the key value, or delete the registry key if it is able to find the key value.

@echo off

REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" 
/v NoDesktop 

if %errorlevel% equ 0 ((
REG DELETE 
"HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" /v 
NoDesktop /f 
)& echo Registry key deleted, now the desktop icons should be displayed.
(
pause)
 )
else (REG add 
"HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" /v 
NoDesktop /t REG_DWORD /d 1 /f (
& echo Registry key added, now the desktop icons should be hidden.
))

pause

but when I run it, the command prompt pops up and closes itself immediately. I tried then deleting all the parentheses but it shows a message saying the registry key is not found, which is good and should trigger the else command but it doesn't and it prompts me to exit.

what have I done wrong? I have also tried deleting all echo commands since I am worried that if a then b & c else d & e doesn't work in cmd. but it still shows that the registry key is not found and prompts me to exit. I have also tried tampering with parentheses and make sure they are all closed but its still not working.

UPDATE: Thank you I have managed to fix the syntax and get the bat working well after some googling....but i still have a few problems: @echo off

REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" 
/v NoDesktop 2>nul

if %errorlevel% equ 0 (
REG DELETE 
"HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" 
/v NoDesktop /f & echo Registry key deleted, now the desktop icons should be 
displayed. & pause
) else (
REG add "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" 
/v 
NoDesktop /t REG_DWORD /d 1 /f & echo Registry key added, now the desktop 
icons should be hidden. & pause
)
  1. is 2>nul absolute necessary? I found out from here https://stackoverflow.com/questions/35458001/check-if-registry-key-value-exists-and-if-so-log-it that he attaches 2>nul at the end of reg query but for this person https://community.spiceworks.com/topic/203455-bat-file-if-reg-key-exists-than-goto he does not do that. Why doesnt his program report error and stop running like mine did?

  2. Even as the program runs smoothly and does add and remove the registry key as intended.... still the desktop icons wont become hidden or unhidden. Adding and removing Nodesktop registry entry is the only method I found on the net that enables and disables desktop icons from cmd. And therefore only way i know that could make a .bat file and make a keyboard shortcut of so i can toggle the setting by keyboard input.

  3. writing programs with cmd hasnt been the most pleasant experience. Are there other ways to write simple programs that could be run on windows but without having to cope with the aged cmd language or installing some other language runtimes such as c++ or java? and how do you make an .exe with basic GUI for simple functions like this one's?

  • 1
    Try running the batch file directly from Command Prompt until you get it working. This way you can see the output without the window closing on you. – Twisty Impersonator Oct 17 '18 at 19:20
  • 1
    It closes immediately because you have errors in your syntax. The way to write an IF statement with bracket comes very precise. I don't think (( is supported at all. – LPChip Oct 17 '18 at 20:12
0

I suggest you

  • use a variable for the key to avoid error prone redundancy
  • use conditional execution on success && or fail ||
  • redirect unwanted output >NUL 2>&1
  • restrict the usage of parentheses to the absolut neccessary

Untested:

:: Q:\Test\2018\10\17\SU_1367690.cmd
@echo off
Set "Key=HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer"

REG QUERY "%Key%" /v NoDesktop >NUL 2>&1 && (
    REG DELETE "%Key%" /v NoDesktop /f >NUL 2>&1
    echo Registry key deleted, now the desktop icons should be displayed.
) || (
    REG Add "%Key%" /v NoDesktop /t REG_DWORD /d 1 /f  >NUL 2>&1
    echo Registry key added, now the desktop icons should be hidden.
)
pause
  • Thank you. Also could you tell me in general when you learn a programming language how do you first get a basic grasp? I have been using this site to try to learn to write simple programs ss64.com/nt but I find it hard to understand since i have close to no coding experience and knowledge for computers at all. Should I start with a textbook instead or try learning some more modern programming languages with those 101 tutorials online? Or do I try reading from wiki and learning bit by bit? Or do you know a good place to get familiar with the basics of computers and os? – Jack the Ripper Oct 17 '18 at 21:17
  • All programming/script languages have a learning curve, which way suits you best you have to find out yourself. Knowledge of batch will always be useful - but I'd use the more modern and feature rich powershell to begin with. – LotPings Oct 17 '18 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.