How to stop a cmd file from showing Network error messages - cmd

Error Message I want to remove:
I get this error when I run a code that runs a shortcut that's shared over network.
How can I stop cmd script from showing Network Errors like these, even if I know there is an error?

#echo off
echo.
echo 1. Add
echo 2. Remove
echo.
set /p var= Type option number here -
if %var%==1 (goto :add)
if %var%==2 (goto :remove)
if else (goto :EOF)
:add
reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\NetworkProvider\ /v RestoreConnection /d 0
echo.
echo SUCESSFULLY ADDED!
timeout /t 2 /nobreak >nul &exit
:remove
reg delete HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\NetworkProvider\ /v RestoreConnection
echo.
echo SUCESSFULLY REMOVED!
timeout /t 2 /nobreak >nul &exit
This .bat script could come handy btw. Atleast it's easier than changing the registry manually. You can add and delete the value any time you want, so its convenient. Save this as a (.bat) file and use it...
Thanks #FiddlingAway for giving reference for the script : )

Related

Is it possible to rename a USB flash drive and transfer files onto that drive?

I am using a batch (below) found elsewhere to facilitate moving the files onto the USB flash drives, but want to also rename the flash drive in the process. How would I incorporate the renaming in Win10?
#echo off
cls
:start
set choice=
xcopy /e /y C:\Users\asimpson\Desktop\transfer\*.* E:
goto wait
:check
timeout /t 1 /nobreak >nul
echo waiting
if exist E: (goto start) else goto check
:wait
timeout /t 1 /nobreak >nul
echo Waiting for removal
if exist E: (goto wait) else goto removed
:removed
echo removed
goto check
The label command can be used in batch files to set the the volume label of a disk.

Batch file that deletes itself and folder that contains it

I'm trying to do the following but deleting the downloaded folder which contains the batch file fails:
NOTE: All exe's, apps, batch file etc. are contained in file.zip.
User downloads file.zip to any directory and unzips.
User runs an exe which is located in the unzipped folder.
This in turn runs two portable apps and some other things.
Once duties are performed, I remote in and run the same exe but this time I select an option that runs a batch file (located in unzipped folder) that starts a 30 second timer then is supposed to stop the apps and delete file.zip and the unzipped folder including the batch file itself.
Below is the batch file:
#echo off
mode con: cols=32 lines=7
color 4f
title
echo 30 Second Delay
echo Close window to abort
echo/
echo/
echo 0%% 100%%
SET /P var= <NUL
set count=0
:loop
PING -n 2 127.0.0.1 >NUL 2>&1
call :printline _
set /a count=count+1
if %count%==30 goto finish
goto loop
:printline
REM Print text passed to sub without a carriage return.
REM Sets line variable in case %1 intereferes with redirect
set line=%1
set /p var=%line%<NUL
exit /b
:finish
cls
color 0f
title Finished
mode con: cols=80 lines=25
echo Do NOT close this window!
echo/
echo Killing processes...
echo/
echo/
echo/
taskkill /t /f /im app1mainprocess.exe >nul
timeout /t 5 >nul
taskkill /t /f /im app2mainprocess.exe >nul
timeout /t 5 >nul
echo Do NOT close this window!
echo/
rem echo Restarting Windows Explorer...
rem timeout /t 10 >nul
rem taskkill /f /im explorer.exe >nul
rem start explorer.exe
echo Do NOT close this window!
echo/
echo Deleteing files and folders...
echo/
rem timeout /t 10 >nul
Set "Folder2Del=%~dp0"
cd ..
IF EXIST "file.zip" DEL "file.zip" /s /q >nul
rem echo %scrptDir%
echo Do NOT close this window!
echo/
echo Still working...
timeout /t 10 >nul
rd %Folder2Del% /s /q
(goto) 2>Nul & RD /S /Q "%Folder2Del%" & exit
The problem I encounter is that the folder never gets deleted. I realize my code is not correct but another reason is because one of the dll files in the unzipped folder is sometimes still in use by the dllhost.exe process.
I'm not sure if it is safe to add a line that kills the dllhost.exe process or not but my code still won't work because I have something wrong with how it deletes the batch file itself and the folder that contains it.
What lines do I need to edit and is it safe to kill dllhost.exe?
According to a link from dbenham
This does the trick:
#Echo off
Echo Ref: "http://www.dostips.com/forum/viewtopic.php?f=3&t=6491"
Set "Folder2Del=%~dp0"
cd "%~d0"
pause
(goto) 2>Nul & RD /S /Q "%Folder2Del%"
Take care the folder containing the batch is deleted
including any other files/folders without any further question!
Ok... I THINK I figured out how to do what I want by trying to delete the dll file, first, before trying to delete the entire directory. The code below looks for the problem dll and tries to delete it. If it still exists, it will try to delete the file every 30 seconds for up to 15 minutes. As soon as the dll gets deleted, the entire folder will also be deleted. If after 15 minutes the dll cannot be deleted, the remaining files in the folder will be deleted.
I still have a small issue. If I add code that kill/restarts Windows Explorer, the folder does not get deleted. Why and is there a workaround?
Below is the latest code:
#echo off
mode con: cols=32 lines=7
color 4f
title
echo 30 Second Delay
echo Close window to abort
echo/
echo/
echo 0%% 100%%
SET /P var= <NUL
set count=0
:loop
PING -n 2 127.0.0.1 >NUL 2>&1
call :printline _
set /a count=count+1
if %count%==30 goto finish
goto loop
:printline
REM Print text passed to sub without a carriage return.
REM Sets line variable in case %1 intereferes with redirect
set line=%1
set /p var=%line%<NUL
exit /b
:finish
cls
color 0f
title Uninstall
mode con: cols=80 lines=25
echo Do NOT close this window!
echo/
echo Killing processes...
tasklist /fi "imagename eq app1mainprocess.exe" |find ":" > nul
if errorlevel 1 taskkill /t /f /im "app1mainprocess.exe" > nul
tasklist /fi "imagename eq app2mainprocess.exe" |find ":" > nul
if errorlevel 1 taskkill /t /f /im "app2mainprocess.exe" > nul
timeout /t 5 >nul
rem echo Do NOT close this window!
rem echo/
rem echo Restarting Windows Explorer...
rem timeout /t 10 >nul
rem taskkill /f /im explorer.exe >nul
rem start explorer.exe
echo/
echo Deleteing file.zip if it exists...
timeout /t 5 >nul
Set "Folder2Del=%~dp0"
cd ..
IF EXIST "file.zip" DEL "file.zip" /s /q >nul
rem echo %Folder2Del%
rem Loops for 30 times in 30 second intervals (Total 15 minutes) to confirm deletion. Loop will exit after 30 loops and move on if dll cannot be deleted.
for /l %%i in (1,1,30) do (
del "%Folder2Del%name*.dll"
if not exist "%Folder2Del%name*.dll" goto Folder2Del
echo/
echo File locked! May take up to 15 minutes to delete.
echo Will stop trying 15 minutes after first attempt.
timeout /t 30 >nul
)
:Folder2Del
echo/
echo Attempting to delete the Connector folder and it's contents...
timeout /t 5 >nul
rd "%~dp0" /s /q & exit

Batch file - how to kill process if there is only one instance of it running?

I'm trying to write a batch that checks how many instances of the process "example.exe" are running, and if there are two or more instances, leave it running. But if there is only one instance running, end the process. Here's what I have:
#echo off
wmic process where name="example.exe" | find "example" /c > %temp%\variable.txt
set /p value=<%temp%\variable.txt
if %value% lss 2 goto endprocess
if %value% gtr 1 goto continue
:endprocess
start taskkill /f /im example.exe
:continue
ECHO continue
#echo off
My issue is this: It always thinks value is lss 2 (it thinks there are less than 2 instances of the process running). However, in my task manager, I can see that there is obviously 2 instances running. I think it's an issue with defining the value maybe? I don't know, I'm quite new to this. Any help? Thanks!
UPDATE
Okay I've now changed it to this (suggested by Magoo)
#echo off
wmic process where name="example.exe" | find "example" /c > "%temp%\variable.txt"
set /p value=<"%temp%\variable.txt"
if %value% equ 1 goto endprocess
if %value% neq 1 goto continue
:endprocess
start taskkill /f /im example.exe
:continue
ECHO continue
#echo off
This still doesn't exactly work, but i changed the number of instances from 1 to 0 and it ended the process. In other words, 1 process was running, but this batch file thought that 0 were running. Any ideas now?
This uses tasklist in XP Pro and higher:
#echo off
tasklist /fi "imagename eq example.exe" /nh |find /i /c "example.exe" > "%temp%\variable.txt"
set /p value=<"%temp%\variable.txt"
if %value% equ 1 taskkill /f /im example.exe
ECHO continue
#echo off
You can do it with one line and no temp file also - this uses another findstr filter to check if the number is a single 1 on a line and then && is a conditional operator that will launch taskkill if it does find 1.
#echo off
tasklist /fi "imagename eq example.exe" /nh |find /i /c "example.exe"|findstr "^1$" >nul && taskkill /f /im example.exe
ECHO continue
#echo off
I'd suggest that you have a fault with your logic.
The code should go to endprocess if the number found is <2 - that is, 0 or 1. If the lss 2 test is failed, then the count must be 3+, so the gtr 1 test will always succeed.
I've no idea why you don't use simply
if %value% neq 1 goto continue
or even
if %value% equ 1 start taskkill /f /im example.exe
But probably you've not told us that you want to be able to detect other instance-counts - as well as concealing the name of the executable for which you are checking.
Now - it may have been really useful to show us the content of the file. Are you sure the file is actually being generated? What happens if you try using "%temp%\variable.txt" instead of %temp%\variable.txt - that is, "quote the filename" ?

Batch File Skipping If Statement

It seems that whenever I run my Batch file, everything will run, and it'll go into checkfiles, but it doesn't operate the if statement. Nothing is returned, it just skips right over unto the last part of the code.
:file_check
if exist "%psychedelia%\nhc.exe" (goto file_exists) else (timeout /t 1 /nobreak > output)
goto file_check
:file_exists
copy /Y "%~dp0version.txt" "%psychedelia%"
:checkfiles
echo in checkfiles
if exist "%psychedelia%\wa.exe" if exist "%psychedelia%\readme.txt" if exist "%psychedelia%\HD.BLB" if exist "%psychedelia%\smackw32.dll" if exist "%psychedelia%\setup95.exe" if exist "%psychedelia%\WAVistaWin7.exe" (
echo MSGBOX "Thank you for installing the Neverhood. You may now go to your desktop and click on the Orpheus shortcut to play!" > %temp%\TEMPxmessage.vbs
call %temp%\TEMPxmessage.vbs
del %temp%\TEMPxmessage.vbs /f /q
rename "%psychedelia%\nhc.exe" wa.exe
timeout /t 1 /nobreak > output
taskkill.exe /F /IM setup95.exe /T
) else (
echo nonexistent
pause
timeout /t 1 /nobreak > output
goto checkfiles
)
All help is greatly appreciated.
Only the last if exist on that line has parentheses, so the else only applies to that last one. If any of the first ones evaluate to false, it will just skip the whole thing.
Try:
...
echo in checkfiles
for %%f in (wa.exe
readme.txt
HD.BLB
smackw32.dll
setup95.exe
WAVistaWin7.exe
) do if not exist "%psychedelia%\%%f" (
echo %%f nonexistent
pause
timeout /t 1 /nobreak > output
goto checkfiles
)
:: all required files found
echo MSGBOX "Thank you for installing the Neverhood. You may now go to your desktop and click on the Orpheus shortcut to play!" > %temp%\TEMPxmessage.vbs
call %temp%\TEMPxmessage.vbs
del %temp%\TEMPxmessage.vbs /f /q
rename "%psychedelia%\nhc.exe" wa.exe
timeout /t 1 /nobreak > output
taskkill.exe /F /IM setup95.exe /T
It might be easier to maintain.
In fact, you could even write
set requiredfiles=wa.exe readme.txt HD.BLB smackw32.dll setup95.exe WAVistaWin7.exe
for %%f in (%requiredfiles%) do if not exist "%psychedelia%\%%f" (
...
which would be even easier.

Resume batch script after computer restart

I have a bunch of old machines running Windows 2000 Pro and IE 5.0 which I want to upgrade to IE 6 with Silverlight. I downloaded the IE6 and Silverlight installers from Microsoft's web sites and fortunately they both have command line options to allow them to run in "silent mode".
I put the two commands in a DOS batch script and ran it, but the IE6 installer requires makes an automatic computer restart so the question is how to resume the script and run the 2nd command (install Silverlight).
My batch file is very simple right now:
ie6setup.exe /Q
silverlight.exe /q
From what I know, batch files can't resume execution after restarting the computer. Is there a way to make them do that? of is there another way to accomplish what I need.
Thank you
Based on Tim's post which, when tested, appended "two" to the batch file resulting in a failure to find the batch label "onetwo", so amended to read & write the "current" variable from a seperate text file, leaving the batch file untouched;
#echo off
call :Resume
goto %current%
goto :eof
:one
::Add script to Run key
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v %~n0 /d %~dpnx0 /f
echo two >%~dp0current.txt
echo -- Section one --
pause
shutdown -r -t 0
goto :eof
:two
echo three >%~dp0current.txt
echo -- Section two --
pause
shutdown -r -t 0
goto :eof
:three
::Remove script from Run key
reg delete HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v %~n0 /f
del %~dp0current.txt
echo -- Section three --
pause
goto :eof
:resume
if exist %~dp0current.txt (
set /p current=<%~dp0current.txt
) else (
set current=one
)
You could put the second command in a exclusive batch file, and add an entry to regedit to execute this batch file automatically upon Windows' start, making silverlight be executed after the computer restarts.
Have you heard of msconfig? On some systems the regedit PATH you are looking for is:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
But you may want to check that. If you want to make a batch file to write that key on the registry, you probably should take a look at this tutorial.
If you do the IE6 installation with the command ie6setup.exe /q /r:n then it won't reboot automatically (see this page for details). Then theoretically you could install SilverLight immediately, and then reboot afterwards; but there is a chance that the SL install will refuse due to the need of a reboot, but it won't hurt to try it...
I know its a bit old but this works amazingly:
#echo off
call :Resume
goto %current%
goto :eof
:one
echo two >>myfile.cmd
::REBOOT HERE
goto :eof
:two
echo resumed here
goto :eof
:resume
rem THIS PART SHOULD BE AT THE BOTTOM OF THE FILE
set current=one
#echo off
set "_RunOnce=HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce"
rem./ :: if no argument was passed, this line will be ignored, but if so, it will be executed here == ^> & %~1
:1st_command
ie6Setup.exe /Q
shutdown -r -t 0 | reg add "%_RunOnce%" /v "%~n0" /d "\"%~f0\" \"goto :2nd_command\"" /f & goto :eof
:2nd_command
SilverLight.exe /Q
timeout -1 | shutdown -r -t 0 | reg add "%_RunOnce%" /v "%~n0" /d "\"%~f0\" \"goto :3rd_command\"" /f & goto :eof
:3rd_command
Skype-8.92.0.401.exe /VerySilent /NoRestart /SuppressMsgBoxes /DL=1 & goto :eof
It is possible to do it without creating or manipulating readings in additional files, just writing and reading in the key and using arguments passed in the execution to control the command necessary for the relevant execution, using goto command as an argument %1
#echo off
set "_RunOnce=HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce"
rem./ if no argument was passed, below will be ignored, but if so, it will be executed here == ^> & %~1
:1st_command
mode con cols=50 lines=1 | title starting %~1
start "google" /wait "c:\program files (x86)\Google\Chrome\Application\chrome.exe" "stackoverflow.com" /new-tab
timeout -1 | shutdown -r -t 0 | reg add "%_RunOnce%" /v "%~n0" /d "\"%~f0\" \"goto :2nd_command\"" /f & goto :eof
:2nd_command
mode con cols=50 lines=1 | title starting %~1
start "google" /wait "c:\program files (x86)\Google\Chrome\Application\chrome.exe" "meta.stackexchange.com" /new-tab
timeout -1 | shutdown -r -t 0 | reg add "%_RunOnce%" /v "%~n0" /d "\"%~f0\" \"goto :3rd_command\"" /f & goto :eof
:3rd_command
mode con cols=50 lines=1 | title %~1
start "google" /wait "c:\program files (x86)\Google\Chrome\Application\chrome.exe" "www.amazon.com" /new-tab
goto :eof

Resources