I am trying to run a simple batch file with following line of codes in it but as soon as I trigger this batch file (either running it from command prompt or double clicking on it), it continuously runs. Like forever.
I am just trying to modify existing key in registry using batch file to some specific value.
reg add "hkcu\control panel\desktop" /v ForegroundLockTimeout /d "0x30d40" /f /t REG_DWORD
exit
In order to stop execution when I am giving interrupt (Cntrl + C) it asks whether it should terminate execution of this batch file or not..
Can someone help to understand what is wrong with this code?
I think it should stop execution once it updates registry key but that is not happening. It continuously prints line# 1 on screen.
You've called the batch file reg.bat right?
Try regfile.bat
Try the following, this works for me:
reg add "HKCU\control panel\desktop" /v ForegroundLockTimeout /t REG_DWORD /d "0x30d40" /f
It returns:
The Operation completed successfully.
Obviously try saving that to your bat file and then run the batfile as usual.
Related
I have a NSIS script with RequestExecutionLevel admin set and within this I invoke a .bat script which adds a reg key.
When the batch-file is executed through command-prompt the reg key gets added. But when running the installer, it executes the .bat file but fails to add the reg key.
nsExec::ExecToStack '"$pluginsdir${SETUP_PATH}\UpdateNtpConfiguration.bat" $Ip1'
UpdateNtpConfiguration.bat content
set adds=%1
REM Get the list of ntp servers showing up in System Date & Time->Internet Time dropdown
set "num=0"
for /F %%G in ('reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers ^| findstr "^[^a-z] ^("') do if %%G GTR !num! set "num=%%G"
set /A num=num + 1
REM Add address at the end
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v "%num%" /t REG_SZ /d "%adds%" /f
goto:eof
Just like #Anders said, there are built in functions for that.
https://nsis.sourceforge.io/Docs/Chapter4.html#registry
If an an error happens with this command, it will be a lot easier to debug as it is built-in.
If you're not doing anything else in the batch file, it is better to find out if NSIS has a command already integrated with it....
EDIT:
It also might be that the admin installer is run the bat file without admin privs.
After some debugging found out that NSIS was writing the registry values in 32 bit reg space(HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft). I had to append /reg:64 to make it add to the 64 bit reg space.
Also the reason I had the bat script was to have some logic to look for duplicates before adding the key.
I need to add a subkey in the registry of Windows 10 with a .bat script, but the results are not what is expected. Code does produce a result, but creates an incorrect entry. I need a subkey named EnableLinkedConnections that is of the type: REG_DWORD with a data value of 1
I have already written a .bat to add entries which are correct, but trying to add another in a different registry folder creates an incorrect entry.
#echo off
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\" /f /v "EnableLinkedConnections" /t REG_DWORD d/ 0x1
pause
This creates a folder outside of the System folder, but still in the Policies folder named:
System" /f /v EnableLinkedConnections /t REG_DWORD d/ 0x1
which is not correct. I expected a subkey entry of type REG_DWORD.
Seems that its having an issue with backslashes maybe, but the other .bat I wrote does not do this, so a bit stumped. Any pointers welcome.
Thanks. Thought I'd try writing a batch file to kill another open cmd session that is constantly open churning out lots of scrolling info.
I got a bit carried away and am now outta my league so to speak.
Basically I am trying to search for "£000.00" within a each emerging line of tet that appears in the other running open command window. The running command session is named in the window as C:\windows\system32\cmd.exe but is does have a valid .exe process name in task manager while running and open.
The batch file code below is as far as I've got.
The idea is that when the above string is found in the other process that process/program get closed down them re-launched.
This is as far as I've got.
#echo off
#echo off
title Shut down other process and relaunch
:loop
start /d "C:\Users\Desktop" ActiveDOSprogram.exe
:: #setlocal enabledelayedexpansion
:: #echo off
:: set stringfound=1
find /c "*£000.00*" C:\Windows\system32\cmd.exe && (echo found %date% %time%:~0,-3% >> "%USERPROFILE%\Desktop\Crash_Report.txt"
taskkill /IM ActiveDOSprogram.exe /F
timeout /t 20
start /d "C:\Users\Desktop" ActiveDOSprogram.exe
goto loop
So when I tried this without any variables and in a loop and I think i nearly blew my PC!
Reason I'm stuck is I'm really a novice at this (but trying) and I got as far as I think I need a variable in there somewhere, that only move's to the next line (taskkill+restart) when £000.00 is found in the other process.
Thanks
wingman
I have written a batch file from a VB.NET program I'm creating.
When I double click on the file in Windows XP it brings up a Command Prompt and appears to be running over and over again.
My batch file is as follows
REG ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename" /ve /t REG_SZ /d "Open With Rename" /f
REG ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename\Command" /ve /t REG_SZ /d "P:\Misc\Rename v2.0\Rename v2.0\bin\Debug\Rename v2.0.exe ""%1""" /f
EXIT
I can't understan what I've done wrong, but if I open a command prompt and run it from there, it runs once.
Any help would be gratly appreciated!
Thanks
In windows, if you have a command line executable with the same name of your bat filename, and the batch file contains this command, the batch file keeps looping.
Example:
Create the file net.bat on your desktop.
In your file write this text: net
Double click the file and it will keep looping.
The cause of this behaviour is the order of execution of the commands. The command you want to execute is in one of the folders in your path. But the batch file is in your current folder so it gets executed first, causing the loop.
make sure:
your script is not named like a build-in command or programm
make sure the scripts your script calls are not named like a build-in command or programm
e.g. if your script is called: reeeeeboooot.bat that calls shutdown -t 10 -r, but in the SAME FOLDER resides a shutdown.cmd
reeeeeboooot.bat will actually call shutdown.cmd INSTEAD of the build-in command.
sometimes the easiest things are the hardest. (pretty often actually :-D)
In windows command line if you want to execute a command or a set of commands then we usually put those commands into a .bat file to execute them at any point of time but we must follow some guidelines before doing so.
The file Name and the command name should not be same else it would loop forever.
There should be no file in that directory having same name as the command name.
In Windows Terminal and DOS, to start a program, you only have to specify the filename without extension (such as .bat, .exe, .cmd, .com). Also, it is case-insensitive.
So if you create a batch file and execute REG, the system will first look in the current directory for something like reg.exe or reg.bat (or another executable with that name). Casing is ignored, so it will include REG.exe. If it doesn't find it, then it will look in the directories specified in the %PATH% environment variable.
In your case, you (assumingly) have an exetable named reg.bat in which you specify that it should call REG. So it will try to call itself, because it will first look in the current directory, in which it will find itself with that name.
The easiest fix is to use the full filename+extension instead. So you can simply change
REG ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename" /ve /t REG_SZ /d "Open With Rename" /f
to
REG.exe ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename" /ve /t REG_SZ /d "Open With Rename" /f
How to make .BAT file delete it self after completion? I have a simple bat file that terminates a process. I want that .BAT file to delete itself.
The Merlyn Morgan-Graham answer manages to delete the running batch script, but it generates the following error message: "The batch file cannot be found." This is not a problem if the console window closes when the script terminates, as the message will flash by so fast that no one will see it. But the error message is very undesirable if the console remains open after script termination.
John Faminella has the right idea that another process is needed to cleanly delete the batch file without error. Scheduling a task can work, but there is a simpler way: use START to launch a new delete process within the same console. It takes time for the process to initiate and execute, so the parent script has a chance to terminate cleanly before the delete happens.
start /b "" cmd /c del "%~f0"&exit /b
Update 2015-07-16
I've discovered another really slick way to have a batch script delete itself without generating any error message. The technique depends on a newly discovered behavior of GOTO (discovered by some Russians), described in English at http://www.dostips.com/forum/viewtopic.php?f=3&t=6491
In summary, (GOTO) 2>NUL behaves like EXIT /B, except it allows execution of concatenated commands in the context of the caller!
So all you need is
(goto) 2>nul & del "%~f0"
#ECHO OFF
SETLOCAL
SET someOtherProgram=SomeOtherProgram.exe
TASKKILL /IM "%someOtherProgram%"
ECHO "This script will now self-destruct. Please ignore the next error message"
DEL "%~f0"
Note that the DEL line better be the last thing you intend to execute inside the batch file, otherwise you're out of luck :)
This will print out an ugly error message, but it is benign, and the code is slightly less confusing this way. If you care a lot about getting rid of the error message, see dbenham's answer to get rid of it.
You didn't mention the OS, but if this is on Windows XP Professional and you have the appropriate permissions, you can have the batch file schedule a one-shot Windows Scheduled Task to delete the file at a later time. Use the schtasks command, documented here.
Otherwise, you typically can't delete a file that is being executed, since that has the potential for all sorts of nastiness. Additionally, trying to delete an executable in use is viewed as very suspicious behavior by any number of antivirus programs, so it's likely that you would run afoul of these as well.
Just add this command at the last line of your batch file
Del batch_file_name.bat
batch_file_name.bat is the name of your batch file
Cheers
you could do #Merlyn's aswer
#ECHO OFF
SETLOCAL
SET someOtherProgram=SomeOtherProgram.exe
TASKKILL /IM "%someOtherProgram%"
DEL "%~f0"
Now make a vbscript with this coding and save it as hidden.vbs, this vbscript will hide the batch file's window.
set w = CreateObject(“WScript.Shell”)
W.Run chr(34) & “%userprofile%\desktop\the_batch_file.bat” & chr(34), 0
set w= Nothing
Then have the batch file run this vbscript
#ECHO OFF
SETLOCAL
SET someOtherProgram=SomeOtherProgram.exe
TASKKILL /IM "%someOtherProgram%"
start "path to hidden.vbs"
DEL "%~f0"
This will hide the batch file before deleting it making the error message impossible to see.
Inline next command to do the "last things" as ghost self. It can be the exit command or some other things before exiting.
#echo off
del "%~f0" && echo All's done. I must exit! && pause > nul && exit
You could also direct the output of the DEL "%~f0" to NULL output like so...
#ECHO OFF
SETLOCAL
SET someOtherProgram=SomeOtherProgram.exe
TASKKILL /IM "%someOtherProgram%"
DEL "%~f0" > NUL