I want to write a batch file to delete a file when it appears after a weekly reboot of the system. This is on a cctv system that the file appears and wont allow the system to run once this file appears.
Several things need to be declared in this question first to actually make this work. Assuming you are using Windows 2000+, and that administrative privileges aren't required to delete the culprit file, then all you would have to do is open notepad and type the following:
Without the need of administrative privileges
#echo off
if exist "insert culprit file's path" del "insert culprit file's path"
With the need of an elevated batch/cmd prompt (administrative privileges to delete the file)
#echo off
CLS
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' (shift & goto gotPrivileges)
ECHO.
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation
ECHO **************************************
setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
exit /B
:gotPrivileges
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd
if exist "insert culprit file's path" del "insert culprit file's path"
Whichever one you need, put the code in notepad, and save the file as your.bat, and don't forget to go to where it says choose file type and select all files.
Related
I've spent the last I don't even know how long troubleshooting this, and I finally found it out. I've been trying to run this line of code:
system("C:\\Users\\Public\\automate.bat");
But instead of executing the script once, it repeatedly executed the first line of my script (in an infinite loop). I found that the path it was using for windows CMD was C:\Windows\SysWOW64\cmd.exe while the windows default is C:\Windows\System32\cmd.exe. I went to the SysWOW cmd and ran my script and the looping issue happened, while the same script worked flawlessly on the System32 CMD. (I suspect because of some incompatibility in my code that caused the SysWOW cmd to fail). I would like to know how to change the cmd.exe path on C so that it points to windows' default cmd, and not the one in SysWOW.
Currently, the only thing I've tried is:
system("C:\\Windows\\System32\\cmd.exe /c C:\\Users\\Public\\automate.bat");
Which is not working.
EDIT: The first action of my BAT file is to ask for administrator perms, which is where the window seems to close, and reopen itself:
#echo off
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
)
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params= %*
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params:"=""%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
cd "C:\Users\Public"
:: more stuff here
Finally found a solution to this! After tinkering around with my environment variables, I found that there was a conflict between user and system variables with different ComSpec paths. After changing them both to the system32 folder, they worked flawlessly. Thanks for all the helpful comments everyone!
I have a program that asks for admin privileges, stops some services, deletes a few files that couldn't be deleted if the services were running, restarts the services, and goes to a hard coded address where I have a vbs script I want to run next.
My problem is I'm about to put it on more computers, and I don't want to edit the hard coded address each time I set it up. I know that when you open a batch file, it starts at the directory where its located. I would like to save that in a variable so i can come back to it later to run the vbs script. Is there any way to do this?
Here is my code:
problem is at the bottom of the script
#echo off
REM --> Check for permissions
IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
)
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
#echo off
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params= %*
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params:"=""%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
#echo off
net stop wuauserv
net stop bits
---save directory here--
cd /
cd Windows
echo To clean Windows Update, we need to wipe a folder. Wiping
rd /s SoftwareDistribution
mkdir SoftwareDistribution
echo The folder has been wiped.
pause
echo.
cd /
net start wuauserv
net start bits
---go to saved directory---
popup <-- VBS Script
pause
exit
A big thank you to #Compo for suggesting to me the use of ‘’’%~dp0’’’
It fixed pretty much everything.
I have a bat file that checks if UAC admin is required, if so, prompts UAC and then copies some dll files:
#echo off
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
cd /d %~dp0
if /i "%PROCESSOR_IDENTIFIER:~0,3%"=="X86" (
echo system is x86
copy .\*.dll %windir%\system32\
regsvr32 /s /c %windir%\system32\filetoregister.dll
) else (
echo system is x64
copy .\*.dll %windir%\SysWOW64\
regsvr32 /s /c %windir%\SysWOW64\filetoregister.dll
)
This works fine when run from windows explorer, if UAC is required, it prompts etc, does exactly what I want, however, if I try to run the batch file from a VB6 programme thus:
Dim FilePath As String
FilePath = App.Path & "\setup\install.bat"
ShellVal = Shell(FilePath, vbNormalFocus)
It works fine if UAC not required but if it is, it asks for permission, after selecting an admin account it then goes into a loop of opening and closing the cmd window. Shutting down the computer is often the only way to kill it
I have tried just using the UAC part of the bat file, still same problem
Really stuck on this one, any help appreciated!
RESOLVED
I sorted this problem by removing the admin request from the batch file and using ShellExecute in the
VB6 programme to get admin rights (if required) and then run the batch file
Dim FilePath As String
FilePath = App.Path & "\setup\install.bat"
ShellExecute 0, "runas", FilePath, Command & "/admin", vbNullString, SW_SHOWNORMAL
Hi I have made a batch file which I need to run as administrator. For that purpose I use this script, which I took from here (StackOverflow).
But what I want, is if the user choose not to run as administrator (click NO to UAC), then the program will exit, and the batch will delete it selves automatically.
The command for the batch file to delete it selves is "del %0", but I need help as to where in this script, I can put this command.
I tried to put it with "#exit /B", but then the batch file will delete if you press either YES or NO to UAC, and then the rest of the batch file cannot execute
Anybody can help figure out how to only run the command "del %0", when a user press "NO" to UAC?
#echo off
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' (shift & goto gotPrivileges)
setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
#exit /B
:gotPrivileges
Thanks guys
Rune
If I understand your question, you need a way to detect if the UAC prompt was canceled. You can detect this if you use my Elevate32.exe or Elevate64.exe (download ElevationToolkit1.zip) program with the -w option, which will return an exit code of 1223 (ERROR_CANCELLED) if you cancel the UAC prompt.
Bill
I know it's an old question but it's still useful to have a solution posted.
If you have Powershell installed you don't need to create a new vbs script and the ERRORLEVEL can be checked to detect if the UAC prompt was canceled or not.
Just put this in your script:
#echo off
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges
) else ( powershell "saps -filepath %0 -verb runas" >nul 2>&1)
if NOT '%errorlevel%' == '0' call :deleteSelf
exit /b
:deleteSelf
start /b "" cmd /c del "%~f0"&exit /b
REM No need for this label
::getPrivileges
::if '%1'=='ELEV' (shift & goto gotPrivileges)
::setlocal DisableDelayedExpansion
::set "batchPath=%~0"
::setlocal EnableDelayedExpansion
::ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
::ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
::"%temp%\OEgetPrivileges.vbs"
::#exit /B
:gotPrivileges
The delete method I took from dbenham's post here
I need to run a batch file which needs to register a DLL. The DLL registration is failing because the Batch file is not starting the command prompt as "administrator".
Is there any way to start the "Command Prompt" as administrator through the batch file.
Environment: Win7/Vista
This script does the trick! Just paste it into the top of your bat file. If you want to review the output of your script, add a "pause" command at the bottom of your batch file.
This script is now slightly edited to support command line args.
#echo off
:: BatchGotAdmin
::-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"="
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
::--------------------------------------
::ENTER YOUR CODE BELOW:
You might have to use another batch file first to launch the second with admin rights.
In the first use
runas /noprofile /user:mymachine\administrator yourbatchfile.bat
Upon further reading, you must be able to type in the password at the prompt. You cannot pipe the password as this feature was locked down for security reasons.
You may have more luck with psexec.
Press Ctrl+Shift and double-click a shortcut to run as an elevated process.
Works from the start menu as well.
(This is based on #DarkXphenomenon's answer, which unfortunately had some problems.)
You need to enclose your code within this wrapper:
if _%1_==_payload_ goto :payload
:getadmin
echo %~nx0: elevating self
set vbs=%temp%\getadmin.vbs
echo Set UAC = CreateObject^("Shell.Application"^) >> "%vbs%"
echo UAC.ShellExecute "%~s0", "payload %~sdp0 %*", "", "runas", 1 >> "%vbs%"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
goto :eof
:payload
echo %~nx0: running payload with parameters:
echo %*
echo ---------------------------------------------------
cd /d %2
shift
shift
rem put your code here
rem e.g.: perl myscript.pl %1 %2 %3 %4 %5 %6 %7 %8 %9
goto :eof
This makes batch file run itself as elevated user. It adds two parameters to the privileged code:
word payload, to indicate this is payload call, i.e. already elevated. Otherwise
it would just open new processes over and over.
directory path where the main script was called. Due to the fact that Windows always
starts elevated cmd.exe in "%windir%\system32", there's no easy way of knowing what
the original path was (and retaining ability to copy your script around without
touching code)
Note: Unfortunately, for some reason shift does not work for %*, so if you need
to pass actual arguments on, you will have to resort to the ugly notation I used
in the example (%1 %2 %3 %4 %5 %6 %7 %8 %9), which also brings in the limit of
maximum of 9 arguments
To prevent the script from failing when the script file resides on a non system drive (c:) and in a directory with spaces.
Batch_Script_Run_As_Admin.cmd
#echo off
if _%1_==_payload_ goto :payload
:getadmin
echo %~nx0: elevating self
set vbs=%temp%\getadmin.vbs
echo Set UAC = CreateObject^("Shell.Application"^) >> "%vbs%"
echo UAC.ShellExecute "%~s0", "payload %~sdp0 %*", "", "runas", 1 >> "%vbs%"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
goto :eof
:payload
::ENTER YOUR CODE BELOW::
::END OF YOUR CODE::
echo.
echo...Script Complete....
echo.
pause
You can use a shortcut that links to the batch file. Just go into properties for the shortcut and select advanced, then "run as administrator".
Then just make the batch file hidden, and run the shortcut.
This way, you can even set your own icon for the shortcut.
This Works for me in Windows 7 to 10 with parameters, when kick starting app or file from anywhere (including browser) and also when accessing file from anywhere. Replace (YOUR BATCH SCRIPT HERE anchor) with your code. This solution May Help :)
#echo off
call :isAdmin
if %errorlevel% == 0 (
goto :run
) else (
echo Requesting administrative privileges...
goto :UACPrompt
)
exit /b
:isAdmin
fsutil dirty query %systemdrive% >nul
exit /b
:run
<YOUR BATCH SCRIPT HERE>
exit /b
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %~1", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
Maybe something like this:
if "%~s0"=="%~s1" ( cd %~sp1 & shift ) else (
echo CreateObject^("Shell.Application"^).ShellExecute "%~s0","%~0 %*","","runas",1 >"%tmp%%~n0.vbs" & "%tmp%%~n0.vbs" & del /q "%tmp%%~n0.vbs" & goto :eof
)
As user2549366 suggested before, "You can use a shortcut that links to the batch file." but in the Properties->Compatibility tab of the shortcut, run as administrator may be disabled.
So instead You just right click on your "file.bat - shortcut" then go to ->Properties->Shortcut tab -> Advanced and there you can click Run as administrator. After that, You can execute the shortcut.
Here's a more simple version of essentially the same file.
#echo off
break off
title C:\Windows\system32\cmd.exe
cls
:cmd
set /p cmd=C:\Enter Command:
%cmd%
echo.
goto cmd
Make a text using notepad or any text editor of you choice. Open notepad, write this short command "cmd.exe" without the quote aand save it as cmd.bat.
Click cmd.bat and choose "run as administrator".