How to write Batch script to do something based on windows version - excel

I am fairly new to writing batch scripts and our system has just thrown me a slight curve ball.
We are getting new computers with Windows 10 and Office 16. We also still have Windows 7 and Office 14 PCs are the network that have not been upgraded yet.
I currently have a batch file that does an xcopy on execution that checks an excel file on a server against a version of the same file on the users local machine. If the server version is newer, it copies over the local version then opens and runs the excel file.
I need to update my batch to accomodate both versions of excel when it needs to open and run the file.
I can check either on windows or excel version as they are married together, but I'm open to whichever method is more reliable.
I was playing with WMIC which returns the version of windows reliably on our machines. I tried other scripts from my research on the web and they did not function as I would have liked.
Here is my current code:
set "source=\\s001\FOLDER\EXCELFILE TOOL.xlsm"
set "target=C:\Apps\EXCELFILE TOOL PROGRAM\"
xcopy "%source%" "%target%" /y /d /h /k /r
START "" "C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.exe" /e "%target%\EXCELFILE TOOL.xlsm"
EXIT
Before the "START" line, I need to check which version of windows or office is on the machine, then modify the start to be one of these two options:
Option 1
START "" "C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.exe" /e "%target%\EXCELFILE TOOL.xlsm"
Option 2
START "" "C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.exe" /e "%target%\EXCELFILE TOOL.xlsm"
Any help would be really appreciated, thanks!

If you're just wanting to start Excel, searching directories should be unnecessary.
Once installed Excel would have a registry sub-key created under:
"[HKEY_LOCAL_MACHINE|HKEY_CURRENT_USER]\Software\Microsoft\Windows\CurrentVersion\App Paths"
Most likely named:
"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\Excel.exe"
Where the Default value data holds the fully qualified path to the application.
With entries under this key, simply entering the applications name should run it:
Start Excel.exe
The REG_SZ data entry can be used with or without its .exe extension.(If necessary, the ShellExecuteEx function adds the extension when searching App Paths subkey).
You could therefore simply use:
Start Excel
If using the method suggested throughout the majority of the comments then you'd need to use Excel.exe as part of your search mask!
The example below, utilises an additional For loop, to allow for checking in both Program Files and Program Files (x86)
#For %%A In ("%ProgramFiles%" "%ProgramFiles(x86)%" 2^>Nul
) Do #For /F "Delims=" %%B In ('Where /R %%A Excel.exe 2^>Nul'
) Do #Start "" "%%~B"
You could of course ignore the outer loop if you're sure that the installed version is always installed to Program Files (x86).
#For /F "Delims=" %%A In ('Where /R "%ProgramFiles(x86)%" Excel.exe 2^>Nul'
) Do #Start "" "%%~A"
The above two examples could be left as one line, if you're not interseted in maintaining lines of no more than 80 characters.

Related

Signing with full path with spaces to signtool.exe fails with “Sign Tool failed with exit code 0x1” in Inno Script Studio

In Inno Setup, my sign tool is defined as:
cmd /k "C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64\signtool.exe" sign /t http://timestamp.comodoca.com /n "My Company" $f
Prepending cmd /k (as suggested at Inno Setup - Signing fails with "Sign Tool failed with exit code 0x1") reveals that there is not actually a problem with the sign tool itself, but rather how Inno Setup is calling it, generating the following message:
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
Clearly, it's chopping off the path to signtool at the first space, even though I put it in quotes. I even tried replacing the quotes with $q to see if that made any difference. (It didn't.) I would think this is improper behavior for the parser to dishonor the quotes, since the instructions clearly say to paste the exact text you'd use on the command line, and the example itself includes quotation marks (https://revolution.screenstepslive.com/s/revolution/m/10695/l/563371-signing-installers-you-create-with-inno-setup).
Everything works beautifully from the command line. For some reason, Inno Setup just isn't properly calling signtool.
ADDED 2020-07-14:
Inno Setup's example, copied from their website:
"C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Bin\signtool.exe" sign /f "C:\MY_CODE_SIGNING.PFX" /t http://timestamp.comodoca.com/authenticode /p MY_PASSWORD $f
My version, used without the cmd /k:
"C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64\signtool.exe" sign /t http://timestamp.comodoca.com /n "My Company" $f
As you can see, we use quotes in the same manner. I will check for updates - I probably should have done that first. When I get some time, I'll revert the signtool definition and see if maybe the update fixes it.
Also, I should have been more clear. I am new to Inno Setup and it didn't occur to me to clarify this. I am compiling via Inno Script Studio. It occurs to me that there's some possibility this has to do with a behavior in a specific version of Script Studio and not the compiler itself, but I don't know enough about the inner workings to do any more than speculate.
Having done a bit more tinkering, I'm now fairly certain this must be a bug with Inno Setup. I implemented a work-around as follows:
Open a command window with elevated privileges
Change to a directory I created with no space in the name: cd \bin
Create a 'spaceless' symbolic link to the directory where signtool.exe is located: mklink /d "Windows10SDK" "C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64"
Enter new sign tool definition that uses the symbolic link:
C:\bin\Windows10SDK\signtool.exe sign /t http://timestamp.comodoca.com /n "My Company" $f
And that's it! Because there is no longer a space character in the path to signtool.exe, Inno Setup now performs the signing correctly and everything works beautifully.
I'll report this as a bug and update if needed.
I use:
#define SignedDesc "$q" + MSA + "$q"
#define SignedPfx "$qd:\My Certificate\2018\My Certificate.pfx$q"
#define SignedTimeStamp "$qhttp://timestamp.verisign.com/scripts/timestamp.dll$q"
#define SignedPw "$q~~~~~~~$q"
SignTool=SignTool /d {#SignedDesc} /du $q{#AppURL}$q /f {#SignedPfx} /p {#SignedPw} /t {#SignedTimeStamp} /v $f
My sign tool is configured as:
$qC:\Program Files (x86)\Windows Kits\10\bin\10.0.16299.0\x86\Signtool.exe$q sign $p
You have to use $q which is a quote mark.

CMD file to backup external hard drive

I have an i7 processor on a laptop with speeds of approximately 2.8 GHz With 3.5 GHz turbo boost.
I want to create a CMD file to copy 20 directories From 1 external solid-state hard drive to a second external solid-state hard drive.
Additional requirements are:
Output a log with only the final summary from each of the major directories.
Save the output to a text file in C:\data. I want to only output the summary table for each directory copied with column headings total copied skipped mismatch failed extras and row headings dir files bytes and times?
While doing the copy pause between directory copies so all data will be copied and not be fragmented in memory. I plan on running this at night.
I am not sure how to handle the pause nor the time for each pause? For example, some of my copies take almost an hour even on solid-state external drives. Other directories are relatively short.
The code I'm currently running is robocopy "e:\bundle" "f:\bundle" /E /256
Help please! Thank you.
MM
As far as I'm aware, you shouldn't need to add any kind of pause between each robocopy command, I've never had to in any of my personal or business use.
As far as getting logging and simplifying things, you can use the following:
F:
mkdir AcademicSupport bundle mmstat
call :script >"C:\Data\Backup-%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%-%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%.txt"
:script
robocopy "E:\AcademicSupport" "F:\AcademicSupport" /e /256 /tee
robocopy "E:\bundle" "F:\bundle" /e /256 /tee
robocopy "E\mmstat" "F:\mmstat" /e /256 /tee
timeout /t 30 /nobreak > NUL
/tee on robocopy will output the results to the console window, which because of call :script will be logged to the global log file created by call :script >
I am working out the details but the following line of code worked for me.
robocopy "E:\AcademicSupport" "F:\AcademicSupport" /E /256 /NC /NFL /NS /NDL /NP

Start Excel file from Windows batch script in safemode, use default file association

Question Summary:
Can I start Excel file Installer.xlsm from Windows batch script in safemode, without providing EXCEL.EXE installation path?
Details
I have a windows batch script which downloads the latest versions of a family of Excel Add-ins from a remote server, places them in a directory (C:\appname\AddIns) and calls the Excel file Installer.xlsm.
Upon loading, Installer.xlsm executes a VBA macro, which uninstalls older versions of the add-ins and installs their newer version.
Currently I start Installer.xlsm using the command:
start "Launching installer file" /wait "<Path to file>\Installer.xlsm"
What's great about it is that it uses Windows' file association to open Excel, and I don't have to provide the EXCEL.EXE installation path (multiple users with different machine images and MS Office versions).
Now I'd like to load Installer.xlsm in safemode, to make sure that no add-ins are loaded and no other code is run while Installer.xlsm tries to work with the add-ins.
I know I can use "<PathToExcel>excel" /safemode "<PathToXls>Installer.xlsm" as described in this answer, but this method doesn't use Windows' file association and requires that I provide a path.
We have users with various machine images, using different versions of MS Office, so I do not want to get into hardcoding all possible Excel installation locations.
Can I do something of the following form:
start "Launching installer file" /wait "<Path to file>\Installer.xlsm /safemode"
I tried different possible combinations without success. How would you do it?
First I suggest to read the Microsoft documentation page Application Registration. It explains how the installer of an application or an application suite like Microsoft Office should register the installed application(s) so that the executable(s) of the application(s) can be found by other applications.
Recommended is creating under registry key
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
a subkey with name of the executable file like excel.exe with default string value being name of the executable with full path and optionally adding one more string value with name Path containing just the path to the executable. The Path string can but most not exist and it can but must not end with a backslash.
The command START uses also this key to find an application as explained in answer on Where is “START” searching for executables?
The installers of the various versions of Microsoft Office register excel.exe key under this key too.
So the easiest method on Windows Vista and later Windows versions to get installation location of Microsoft Excel is:
#echo off
for /F "skip=1 tokens=2*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe" /ve 2^>nul') do set "ExcelApp=%%~B"
echo ExcelApp=%ExcelApp%
pause
But on Windows XP the output of reg.exe is different and requires for that reason this batch code:
#echo off
for /F "skip=3 tokens=3*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe" /ve 2^>nul') do set "ExcelApp=%%~B"
echo ExcelApp=%ExcelApp%
pause
The different outputs are explained in answer on Read words separated by space & string value also contains space in a batch script in batch code written to get string value of a default string of a registry key containing spaces.
And it is good coding practice to add extra code which handles an error case like registry key does not exist at all because Microsoft Excel is not installed at all.
But is it possible with batch code to do what command START respectively the Windows shell function ShellExecuteEx does on using in a command prompt window the command line?
start "Launching installer file" "C:\Path to file\Installer.xlsm"
Yes, it is possible as the commented batch code below demonstrates.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem First query default string value of HKEY_CLASSES_ROOT\.xlsm from registry.
call :GetDefaultRegValue "HKCR\.xlsm"
rem Is there no key HKEY_CLASSES_ROOT\.xlsm or was the default string empty?
if not defined RegValue goto GetFromAppPaths
SET RegValue
rem Get the shell command line used for opening a *.xlsm file.
call :GetDefaultRegValue "HKCR\%RegValue%\shell\open\command"
rem Could the command line not read successfully from Windows registry?
if not defined RegValue goto GetFromAppPaths
SET RegValue
rem The command line contains as first string usually enclosed in double
rem quotes EXCEL.EXE with full path enclosed in double quotes. And there
rem can be even more arguments on the command line which are not needed
rem here. The command line below is used to get just first string of
rem the command line which should be EXCEL.EXE with full path.
for %%I in (%RegValue%) do set "RegValue=%%~I" & goto CheckExcelExistence
rem It is not good when both registry queries above fail. This means
rem either Microsoft Excel is not installed at all or a version of
rem Excel is installed which does not support *.xlsm files like Excel
rem of MS Office 2003, MS Office 2000 or MS Office 97.
rem However, perhaps just *.xlsm is not correct registered and therefore
rem get full path to excel.exe from application registration key.
:GetFromAppPaths
call :GetDefaultRegValue "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe"
if defined RegValue goto CheckExcelExistence
echo Failed to determine installation location of Microsoft Excel.
echo/
endlocal
pause
goto :EOF
:CheckExcelExistence
SET RegValue
rem Remove surrounding double quotes if the Excel executable file name
rem read from Windows registry is still enclosed in double quotes.
set "RegValue=%RegValue:"=%"
if exist "%RegValue%" goto :RunInstall
echo Registered "%RegValue%" does not exist.
echo/
endlocal
pause
goto :EOF
:RunInstall
SET RegValue
ECHO start "Launching installer file" /wait "%RegValue%" "%~dp0Installer.xlsm" /safemode
endlocal
goto :EOF
rem This subroutine queries from Windows registry the default string value of
rem the key passed to the subroutine as first and only parameter and assigns
rem this value to environment variable RegValue. Environment variable RegValue
rem is deleted and therefore is not defined after subroutine exits on failure
rem to get the registry value or when the default value is an empty string.
rem This subroutine works for Windows XP and all later versions of Windows.
:GetDefaultRegValue
set "TypeToken=2"
:Reg3Run
for /F "skip=1 tokens=%TypeToken%*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "%~1" /ve 2^>nul') do (
if "%%A" == "REG_SZ" (
if not "%%~B" == "" (
set "RegValue=%%B"
goto :EOF
)
) else if "%%A" == "NAME>" (
set "TypeToken=3"
goto Reg3Run
)
)
set "RegValue="
goto :EOF
This batch code is just a demonstration. It does not start Excel when really found. Instead it just outputs the command line which would start Excel because of ECHO left of start ... in block below label RunInstall.
Further this batch code contains 4 lines with just SET RegValue. Those 4 lines output just the string value queried successfully from Windows registry and stored in environment variable RegValue. Those 4 commands help to understand what happens on execution of the batch file. Those four command lines should be deleted finally from batch file and also the single ECHO written in upper case.
Note: It is quite easy to test what happens if an expected registry key does not exist or its default value is an empty string. Just insert a single character like # before last double quote on a line starting with call :GetDefaultRegValue and the modified registry key is not found anymore.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
reg /?
reg query /?
rem /?
setlocal /?
start /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded reg.exe command line with using a separate command process started in background.

Search for a file and open the path in CMD

I would like to achieve the following in CMD:
Will search for a specific filename - This I know how to do, with dir /s filename
Once found it will bring me to that path.
Example: I am now on C:\, if that file was found in C:\test then it will open C:\test in command prompt.
Alternatively, I would like just to copy the found file to a path I will specify. I just don't know how to do it, since I don't know the path where the file will be stored (it's different every time).
Thanks in advance!
I'm a liitle confused with your question but I will try to walk you through a batch code I wrote to help you with this.
dir /b /s "test.txt" > "%userprofile%/Desktop/result.txt"
::find the file path to the file you want to find and insert it in a .txt file
::you made called result.txt (also %userprofile% is a variable that brings you to
::your user directory ex C:/users/admin)
for /F "tokens=*" %%A in (%userprofile%/desktop/result.txt) do (
set var1=%%A
)
::set the variable var1 equal to the first (well... only) line in the file.
::I could explain what that for loop means in detail but just remember that %%A
::is a variable set from what was found when looping through the result.txt.
xcopy /s "%var1%" "C:/wherever/you/want/it/to/go"
did this help??

move files name in excel to a specific folder

I am trying to write a batch file that reads from excel file. then move them to a specique folder.
lets say in the excel , there are:
111.txt
222.txt
333.txt
512.txt
I want a batch file that move files into a specific directory that have the above names.
how can I achieve that ?
(I am using xp , and I the directory is D:)
cd /d "d:\files"
for /f %%i in ("C:\where the file resides\file.csv") do (
echo move "%%i" "D:\New\"
)
remove the echo if output seems ok

Resources