How can I make bat file open Excel files with quotes? - windows

Excel is installed at C:\Program Files\Microsoft Office 15\root\office15\EXCEL.EXE
Folder where the files is at C:\Heffalump files
The name of a example file is Auto the heff.xlsx
Been toying with a bat file to run that excel file but cant get it to work. The above should have been
"C:\Program Files\Microsoft Office 15\root\office15\EXCEL.EXE" "C:\Heffalump\Auto the heff.xlsx"
when running the bat:
"C:\MyAwesomeBat.bat" "Auto the heff.xlsx"
But it keeps the quotes so it gets all messed up.
cls
#SET ScheduledExcel=true
#SET ScheduledExcelFolder="C:\Heffalump files"
#SET ScheduledExcelFilePath=%ScheduledExcelFolder%\%1
"C:\Program Files\Microsoft Office 15\root\office15\EXCEL.EXE" %ScheduledExcelFilePath%

You need to enclose the entire argument into quotes, not only parts of it. Your 2nd SET statement includes the quotes into the value; after appending %1, you'll have a quote in the value.
The following should work:
CLS
#SET ScheduledExcel=true
#SET "ScheduledExcelFolder=C:\Heffalump files"
#SET "ScheduledExcelFilePath=%ScheduledExcelFolder%\%~1"
"C:\Program Files\Microsoft Office 15\root\office15\EXCEL.EXE" "%ScheduledExcelFilePath%"
I enclosed the entire SET expression in quotes, so they do not become part of the value.
Note also the ~ in %~1 which removes surrounding quotes if there are any.

Whenever you set a variable to a string, it's generally good practice to quote the set "variable=value" pair. That way the value doesn't contain the quotes, but you also avoid evaluating tricky characters like &. Then you can explicitly quote at retrieval time when needed.
Also, aschipfl correctly points out that you need to include a tilde in %~1 to strip the surrounding quotes from the command-line argument.
#echo off
setlocal
cls
SET "ScheduledExcel=true"
SET "ScheduledExcelFolder=C:\Heffalump files"
SET "ScheduledExcelFilePath=%ScheduledExcelFolder%\%~1"
"C:\Program Files\Microsoft Office 15\root\office15\EXCEL.EXE" "%ScheduledExcelFilePath%"
For what it's worth, unless you intentionally installed Office to that folder, isn't Office typically located in C:\Program Files (x86)\Microsoft Office\Office15 or similar? Regardless, as Mahran hinted, you don't actually need to specify the full path to excel.exe. If Windows has .xlsx files associated with Excel, then you can either
start "" "%ScheduledExcelFilePath%"
or
call "%ScheduledExcelFilePath%"
or most simply
"%ScheduledExcelFilePath%"

Simply on you bat file write this code
call C:\Heffalump\heff.xlsx

Related

Bat: Set command not working inside parenthesis

I've come across this strange behavior of bat commands:
Set path=%path%;C:\Myfolder;
works perfectly fine. But when I put this inside parenthesis it behaves strangely:
if defined WINDIR (set path=%path%;C:\Myfolder)
\NVIDIA was unexpected at this time.
or even
(set path=%path%;C:\Myfolder)
\NVIDIA was unexpected at this time.
Please note that
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common
is part of my Path environment variable.
What's wrong with using () here?
If the set command is placed inside parentheses, then the %path% expansion puts a right parentheses that closes the original; that is:
set path=C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common
(set path=%path%;C:\Myfolder)
When the %path% variable is expanded in the second line, this is the result:
(set path=C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Myfolder)
You may see that the right paren in (x86) part closes the original left paren, so the next part in the line is \NVIDIA Corporation\PhysX\Common;C:\Myfolder) that is the cause of the error.
To avoid this error, just enclose in quotes the value in set command:
(set "path=%path%;C:\Myfolder")

Command to copy a file to another directory

Please have a look at the following commands and what was the error in syntax? i wanted to copy the dll file to my project bin directory
C:\WINDOWS\assembly\GAC_MSIL\Microsoft.ReportViewer.WinForms\10.0.0.0__b03f5f7f1
1d50a3a>dir /s /b /o:gn
C:\WINDOWS\assembly\GAC_MSIL\Microsoft.ReportViewer.WinForms\10.0.0.0__b03f5f7f1
1d50a3a\Microsoft.ReportViewer.WinForms.dll
C:\WINDOWS\assembly\GAC_MSIL\Microsoft.ReportViewer.WinForms\10.0.0.0__b03f5f7f1
1d50a3a>copy Microsoft.ReportViewer.WinForms.dll C:\Documents and Settings\mural
i.madhava\My Documents\Visual Studio 2008\Projects\Rdclrender\Rdclrender\bin
The syntax of the command is incorrect.
Quite simply you need to place quotes around any path that has a space in it:
copy Microsoft.ReportViewer.WinForms.dll "C:\Documents and Settings\murali.madhava\My Documents\Visual Studio 2008\Projects\Rdclrender\Rdclrender\bin"
If you have spaces in your paths (such as C:\Documents and Settings), you'll need to surround them with quotes to stop them being treated as multiple arguments:
copy blah.WinForms.dll "C:\Documents and Settings\blah\bin"
This gives you the three-argument command:
copy
blah.WinForms.dll
C:\Documents and Settings\blah\bin
Without the quotes, you get the five-argument command which is not what you want:
copy
blah.WinForms.dll
C:\Documents
and
Settings\blah\bin

Windows batch file syntax

The following windows 7 batch file script returns the error:
#ECHO OFF
if exist C:\Program Files (x86)\ E1\P45V goto WIN7
ren /s /c "c:\Program Files\ E1\P45V\P45Login.bmp" "c:\Program Files\E1\P45V\P45Login_OLD.bmp"
copy "\\locattion14\temp\E1\P45Login.bmp" "c:\Program Files\ E1\P45V\P45Login.bmp"
goto END
:WIN7
ren /s /c "c:\Program Files (x86)\ E1\P45V\P45Login.bmp" "c:\Program Files (x86)\E1\P45V\P45Login_OLD.bmp"
copy "\\locattion14\temp\E1\P45Login.bmp" "c:\Program Files (x86)\ E1\P45V\P45Login.bmp"
:END
The syntax of the command is incorrect
Using PSTOOLs to push out a change to computers, and will add the list when the syntax error is corrected.
The desired result:
If the pc is an XP machine, rename the P45login.bmp file to same name_OLD.bmp, then copy the file from loaction 14 into the directory noted.
If the PC is a Win 7 machine, skip the first part, go to the second part, and commit the same changes.
close the session.
I have moved quotes, added/subtracted switches, but arrive at the same error.
Surely it is just a simple syntax particularity that I am not catching.
Hoping someone will take a look, see the obvious I am missing, and point me in the right direction,.
Thank you for any help or suggestions.
Your ren syntax is wrong. ren does not support any switches and also rename_to needs to be name only, not full path. See full details here: http://technet.microsoft.com/en-us/library/cc754276%28v=ws.10%29.aspx
Additionally:
- if exists needs quotes around path
- you use both \ E1\ or \E1\ (with or without space). While both could be valid, I would double check if that's not an error.
- if the paths above are actually different, you need to use move (with full paths) instead of ren.

Windows x64 & "parenthesis in path" batch file problem

Windows x64 versions contain folders named with parenthesis like "\Program Files (x86)" and this breaks a batch file I use. An example of a problem line:
for %%c in (%path%) do if exist "%%c\xyz.exe" set xyz=OK
i.e. when it reaches ")" in "(x86)" it puts out an error message and exits...
Any ideas on how to fix this?
This is a rather large batch file, and atm I don't have the time to rewrite it in a better language...
Many thanks :)
Doesn't directly answer your question, but if you are trying to do what I thinking you are trying (which is make sure a file exists in the path) you can use something like the following in a batch file.
#echo off
for %%i in (xyz.exe) do set xyz=%%~$PATH:i
if "%xyz%" == "" Goto NotFound
Echo "Found"
Goto TheEnd
:NotFound
Echo "Not found"
:TheEnd
Normally quoting should work, but in this case you want to iterate over all elements seperated by ;.
But you can replace the ; to a " " combination, so the brackets are quoted and you can iterate over the elements.
sample: path=C:\temp;C:\windows;C:\Program Files (x86)
The for-loop will search in
"C:\temp" "C:\windows" "C:\Program Files (x86)"
As code it looks like
setlocal EnableDelayedExpansion
set "searchPath=!path:;=" "!"
for %%c in ("!searchPath!") do (
if exist "%%~c\xyz.exe" set xyz=OK
)
You can use the short names of the folder for this purpose. This is how you do it.
Open command promt in Windows.
Go to C drive (or the drive in which you have the Program Folder)
Type the following and
c:\> dir /x <Hit Enter>
This will return the short forms of all folders.
You will notice now that "\Program Files (x86)" will be represented as "PROGRA~2" (or an equivalent short name).
This is what I use to prevent any errors while creating Batch scripts.
For more options see here.
http://www.computerhope.com/dirhlp.htm
Exlpanation for "dir /x"
"This displays the short names generated for non-8dot3 file names. The format is that of /N with the short name inserted before the long name. If no short name is present, blanks are displayed in its place."

Windows Batch Scripting Issue - Quoting Variables containing spaces

So here's my issue:
I want to use %cd% so a user can execute a script anywhere they want to place it, but if %cd% contains spaces, then it will fail (regardless of quotes). If I hardcode the path, it will function with quotes, but if it is a variable, it will fail.
Fails: (if %cd% contains spaces)
"%cd%\Testing.bat"
Works:
"C:\Program Files\Testing.bat"
Any ideas?
%CD% is not the right way to do it, as it indicates the directory where the user was located when invoking the script, not the directory where the script resides.
Use %~dp0 instead to extract the drive and path information from %0:
REM C:\Program Files\test_caller.bat
#echo I am the caller and I reside in: "%~dp0"
#"%~dp0\test.bat"
...
REM C:\Program Files\test.bat
#echo Yippeee!
...
C:\>"\Program Files\test_caller.bat"
I am the caller and I reside in: "C:\Program Files\"
Yippeee!
C:\>e:
E:\>"C:\Program Files\test_caller.bat"
I am the caller and I reside in: "C:\Program Files\"
Yippeee!

Resources