batch file to run office build in uninstaller - batch-file

I am writing a batch script which I am having trouble with.
First, it checks for Admin permissions.
Second, it checks to see what version of Microsoft office is installed on the system.
Third, it goes to the build in unstabler.
The problem is with the third part. I get admin right. I can find what version of office is installed, setting it in %office-version%. But I can't check the %office_version% correctly in my if statements. Here is my code:
#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"
::------------------------------------------------------------------------------
:: This part of the cod4e found what office is install on the computer
::-----------------------------------------------------------------------------
setlocal enableDelayedExpansion
for /f "tokens=2 delims==" %%O in ('ftype ^|findstr /r /I "\\OFFICE[0-9]*" 2^>nul') do (
set "verp=%%~O"
goto :end_for
)
:end_for
for %%P in (%verp%) do (
set "off_path=%%~dpP"
for %%V in ("!off_path:~0,-1!") do (
set "office_version=%%~nV"
goto :end_for2
)
)
:end_for2
echo %office_version%
Pause
::-----------------------------------------------------------------------------
:: That the var of %Offfice_Verson% then running the build in remove of that office software.
::-------------------------------------------------------------------------------
if %office_version%== office12 goto 2007
If %office_version%== office14 goto 2010
If %office_version%== office15 goto 2013
:2007
Echo Unstailling office 2007
cd "%ProgramFiles%\Common Files\microsoft shared\OFFICE12\Office Setup Controller"
Goto remove
:2010
Echo Unstailling office 2010
cd "%ProgramFiles%\Common Files\microsoft shared\OFFICE14\Office Setup Controller"
Goto remove
:2013
Echo Unstailling 2013
cd "%ProgramFiles%\Common Files\microsoft shared\OFFICE15\Office Setup Controller"
Goto Remove
:Remove
%homedrive%
SETUP.EXE
Echo Done!!!!!
Pause
Exit
So any help you can send me will be a great help. thank you.

The problem seems to be with your if statements:
If %office_version%== office12 goto 2007
The space after == is the problem. You are checking if the string outputted by %office_version% is literally equal to " office12" including the space character.
If "%office_version%"=="office12" goto :2007
You should probably also have something after the if statements, in case none of your if conditions are met. Something like:
echo Unable to identify Office version
goto :EOF

Related

Store the current directory in a variable to be used later

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.

Zip separately each folder using 7-zip

I wrote this batch file to zip files each day separately.
However, I wanted to do it for folders.
the problem is that for each day I have a different name for the folder. like:
20170530.daily
20170529.daily
and on...
I've attached the batch I already wrote, can you look into this?
`#echo off
:: BatchGotAdmin (Run as Admin code starts)
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"
:: BatchGotAdmin (Run as Admin code ends)
:: Your codes should start from the following line
#echo off
for %%A in ("E:\Logs\SmartLogger\*") do (if /I not "%%~xA" == ".zip" 7za.exe
a -tzip -mx5 -y -- "%%~dpnA.zip" "%%~A" >nul && del /Q /F "%%~A")`

Editing hosts with batch file windows 10

Hello i've been looking for a batch file that is able to edit my hosts file in windows 10 but apparently none is working. I've been trying to use these two codes combined found in this forum.
REM --add the following to the top of your bat file--
#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"
SET NEWLINE=^& echo.
FIND /C /I "ns1.intranet.de" %WINDIR%\system32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^62.116.159.4 ns1.intranet.de>>%WINDIR%\System32\drivers\etc\hosts
FIND /C /I "ns2.intranet.de" %WINDIR%\system32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^217.160.113.37 ns2.intranet.de>>%WINDIR%\System32\drivers\etc\hosts
FIND /C /I "ns3.intranet.de" %WINDIR%\system32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^89.146.248.4 ns3.intranet.de>>%WINDIR%\System32\drivers\etc\hosts
FIND /C /I "ns4.intranet.de" %WINDIR%\system32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^74.208.254.4 ns4.intranet.de>>%WINDIR%\System32\drivers\etc\hosts
What i get when i try to run is that windows defender blocked the file because of the security reasons. How to make it work?
It's painful to change hosts file manually two days a week in normal way.
Download the hostsfileeditor it makes it a snap to change.

Error 53 - Network Path was not found

first post of mine on this website, used to lurk but I've recently had a bit of a pickle with a batch-file
I've been working as a small little simple auto connect batch-command which auto-connects my laptop to my schools shared network server but for some odd reason I am unable to connect as the command prompt prints "System Error 53 has occurred The network path was not found."
If anyone could shed some light as to what I'm doing wrong would be much appreciated.
#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"
:--------------------------------------
setlocal
:PROMPT
SET /P AREYOUSURE="Are you sure you want to add School's shared documents? (Y/[N]):"
IF /I "%AREYOUSURE%" NEQ "Y" GOTO END
echo Please enter the Username you use to login to school computers
set /p username=Enter your Username(SCHOOL USERNAME):
echo Please enter the Password you use to login to school computers
set /p password=Enter your Password(SCHOOL PASSWORD):
net use Z: "\\server1\shared_documents$\" "%password%" /user:\Students\"%username%" /persistent:yes
TIMEOUT /T 5 /NOBREAK
:END
endlocal

batch file for copying files with admin rights

im new to batch files and im trying to write a file which gets admin rights and copies a file.
This is the code I found for getting admin rights, but it seems that it is not working for Windows 8;
#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"
:--------------------------------------
<my code>
Then i´ve tried to write code for copying the files which didnt work neither:
xcopy "%1" "%2"
xcopy "%3" "%4"
echo "succesfull"
Try getting rid of "%temp%" throughout the script, I tried the same code under windows 8, but I keep getting access denied errors until I edit every line that accesses the systems temp folder.

Resources