How to get each string in a list of strings? - windows

I have a List of String separated by a space and enclosed in double quotes.
"D:\oracle\product\10.2.0\db_1\bin" "C:\ORACLE_HOME\bin" "C:\Program Files (x86)\Common Files\Oracle\Java\javapath" "C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\" "C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\" "C:\WINDOWS\system32" "C:\WINDOWS" "C:\WINDOWS\System32\Wbem" "C:\WINDOWS\System32\WindowsPowerShell\v1.0\" "C:\WINDOWS\System32\OpenSSH\" "C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL" "C:\Program Files\Intel\Intel(R) Management Engine Components\DAL" "C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT" "C:\Program Files\Intel\Intel(R) Management Engine Components\IPT" "C:\ProgramData\chocolatey\bin" "C:\Program Files\Git\cmd" "C:\Program Files\nodejs\" "C:\Program Files\OpenVPN\bin" "C:\Program Files\dotnet\" "C:\Users\admin\Desktop\hello\tasm\lib" "C:\Program Files\Java\jdk1.8.0_212\bin" "C:\MinGW\bin" "C:\src\flutter\bin" ""D:\oracle\product\10.2.0\db_1\bin" "C:\ORACLE_HOME\bin" "C:\Program Files (x86)\Common Files\Oracle\Java\javapath" "C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\" "C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\" "C:\WINDOW"" "C:\Users\admin\AppData\Local\Android\sdk\tools" "C:\Users\admin\AppData\Local\Android\sdk\platform-tools" "C:\Program Files\Java\jdk1.8.0_212\bin" "C:\Users\admin\anaconda3\condabin" ""
I want to get each one of them using a for loop in batch file.
I was trying to debug an batch file from another software (since it crashed because of the space between \Common Files\)
rmpath.cmd (Code)
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
if _%1==_ goto USAGE
set fqElement=%~fs1
set fpath="%PATH:;=" "%"
echo %fpath%
for %%p in (%fpath%) do (
set p2=%%~fsp
if /i NOT "!p2!"=="%fqElement%" (
if _!tpath!==_ (set tpath=%%~p) else (set tpath=!tpath!;%%~p)
)
set path=!tpath!
ENDLOCAL & set path=%tpath%

Related

Can a Windows batch file determine the file it opens

I have a file named USER_PREM_HOL_AMT.bat.
The code inside is:
set datename=%date% %username% %~n0
:CheckOS
IF EXIST "C:\Program Files\Microsoft Office\Office14\EXCEL.exe" (GOTO OFFICE2010) ELSE (GOTO OFFICE2013)
:OFFICE2010
ECHO %datename%>>"O:\Holiday.log"
START "" "C:\Program Files\Microsoft Office\Office14\EXCEL.exe" /e "file://///Log_files\Holiday.xlsm"
EXIT
GOTO END
This currently gives the log:
Tue 09/11/2018 7098703 USER_PREM_HOL_AMT
I want it to give me
Tue 09/11/2018 7098703 Holiday.xlsm
Is there a way to do this? Get the file name it's opening instead of its own name.
You can change the first line to specify the filename.
set "datename=%date% %username% Holiday.xlsm"

How can I find the latest Visual Studio Developer Command Prompt in a batch file?

I need to write a batch file to complete a pre-build step in Visual Studio. As part of it, I need to call the Visual Studio Developer Command Prompt.
I know that for VS2015, the developer command prompt is located in C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat.
Unfortunately, our build server has a different version of VS. Is there a simple way to find the latest VSDevCmd.bat for all (or most) versions of Visual Studio in a batch file, so the pre-build step will work on both environments?
You should use vswhere provided by the Microsoft Visual Studio Installer. You copy vswhere.exe to a known location. The wiki has a description on how to start the developer command prompt:
#if not defined _echo echo off
for /f "usebackq delims=" %%i in (`vswhere.exe -prerelease -latest -property installationPath`) do (
if exist "%%i\Common7\Tools\vsdevcmd.bat" (
%comspec% /k "%%i\Common7\Tools\vsdevcmd.bat" %*
exit /b
)
)
rem Instance or command prompt not found
exit /b 2
The thing that I do for CI-CD machines is use vcvarsall for non 2017 machines and vsdevcmd for 2017 ones. Script snippet below:
rem vsvarsall.bat does not work if there are quoted paths on %PATH%
set path=%path:"=%
rem this will work for non VS 2017 build machines
if exist "c:\progra~2\Micros~1.0\vc\vcvarsall.bat" (
call c:\progra~2\Micros~1.0\vc\vcvarsall.bat && goto :SetVSEnvFinished
)
echo vcvarsall.bat not found, looking for vsdevcmd.bat
rem Find and run vsdevcmd.bat
set "VS_PATH=C:\Program Files (x86)\Microsoft Visual Studio\2017"
rem The 2017 folder will not be present in Visual Studio 2017 Preview machines (such as 15.8 preview)
if not exist "%VS_PATH%" (
set "VS_PATH=C:\Program Files (x86)\Microsoft Visual Studio"
)
if not exist "%VS_PATH%" (
echo "%VS_PATH%" not found. Is Visual Studio installed? && goto :ErrorExit
)
for /f "delims=" %%F in ('dir /b /s "%VS_PATH%\vsdevcmd.bat" 2^>nul') do set VSDEVCMD_PATH=%%F
echo ********Executing %VSDEVCMD_PATH%********
call "%VSDEVCMD_PATH%"
goto :SetVSEnvFinished
:ErrorExit
exit /b 1
:SetVSEnvFinished
So for non 2017 ones, it will execute vcvarsall.bat (which sets up the VS environment). For 2017 versions, it will search for vsdevcmd.bat (in specific folder to reduce search time) and run it.
Hope this helps.
I've managed to cobble together a batch file that looks at parameters, then at the filesystem to try to figure out what version of VSDevCmd should be used. $(DevEnvDir) is sometimes *Undefined*, so we need to check for that too.
Tested on VS2015 Professional, VS2017 Professional, and VS2017 Enterprise.
REM Usage in VS build event: call "$(SolutionDir)\find_vsdevcmd.bat" "$(DevEnvDir)"
SET vsversion=
REM Get Visual Studio version, either from command prompt, or newest on filesystem
if [%1] == [] (
if exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat" (
SET vsversion="VS2017 Enterprise"
) else if exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\Tools\VsDevCmd.bat" (
SET vsversion="VS2017 Professional"
) else if exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat" (
SET vsversion="VS2017 Community"
) else if exist "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat" (
SET vsversion="VS2015"
) else if exist "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat" (
SET vsversion="VS2013"
) else goto :eof
) else if [%1] == ["*Undefined*"] (
if exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat" (
SET vsversion="VS2017 Enterprise"
) else if exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\Tools\VsDevCmd.bat" (
SET vsversion="VS2017 Professional"
) else if exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat" (
SET vsversion="VS2017 Community"
) else if exist "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat" (
SET vsversion="VS2015"
) else if exist "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat" (
SET vsversion="VS2013"
) else goto :eof
) else (
if [%1] == ["C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\"] (
SET vsversion="VS2017 Enterprise"
) else if [%1] == ["C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\"] (
SET vsversion="VS2017 Professional"
) else if [%1] == ["C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\"] (
SET vsversion="VS2017 Community"
) else if [%1] == ["C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\" (
SET vsversion="VS2015"
) else if [%1] == ["C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\" (
SET vsversion="VS2013"
) else goto :eof
)
if %vsversion% == "VS2017 Enterprise" (
SET vsdevcmd="C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat"
ECHO VS2017 Enterprise
) else if %vsversion% == "VS2017 Professional" (
SET vsdevcmd="C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\Tools\VsDevCmd.bat"
ECHO VS2017 Professional
) else if %vsversion% == "VS2017 Community" (
SET vsdevcmd="C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat"
ECHO VS2017 Community
) else if %vsversion% == "VS2015" (
SET vsdevcmd="C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat"
ECHO VS2015
) else if %vsversion% == "VS2013" (
SET vsdevcmd="C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat"
ECHO VS2013
) else goto :eof
call %vsdevcmd%

Include multiple directories in RCFLAGS

I am using nmake to compile a makefile.msc file
I have to include the below directories
c:\Program Files (x86)\Windows Kits\8.1\Include\um
C:\Program Files (x86)\Windows Kits\8.1\Include\shared
C:\Program Files (x86)\Windows Kits\8.1\Include\winrt
in the /I atrribute of the RCFLAGS
What I have tried are below :
1st try:
RCFLAGS = /I "c:\Program Files (x86)\Windows Kits\8.1\Include\um" /I "C:\Program Files (x86)\Windows Kits\8.1\Include\shared" /I "C:\Program Files (x86)\Windows Kits\8.1\Include\winrt"
2nd try:
RCPATH = "'c:\Program Files (x86)\Windows Kits\8.1\Include\um':'C:\Program Files (x86)\Windows Kits\8.1\Include\shared':'C:\Program Files (x86)\Windows Kits\8.1\Include\winrt'"
RCFLAGS = /I $(RCPATH)
Everytime, RC complaints about different missing headers.
Please give a guideline on how to achieve what I am trying to do.
Many thanks in advance.
Edit : Many resources elaborate how to do this with CFLAGS but are
not specific about RCFLAGS. Moreover, there is little help if I do
a rc /?.
Finally resolved it by the below format :
-I "c:\Program Files (x86)\Windows Kits\8.1\Include\shared" -I "c:\Program Files (x86)\Windows Kits\8.1\Include\winrt" -I "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include"
Sounds silly though since the delimiter / is accepted for the first RCFLAGS attribute which is /dWIN32.

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

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

Echoing path variable in windows displaying twice

When i am trying to echo the system path variable it is showing the same thing twice.
My system path variable:
C:\oraclexe\app\oracle\product\10.2.0\server\bin;C:\Program Files
(x86)\PC Connectivity Solution\;C:\Program Files\Common
Files\MicrosoftShared\Windows Live;C:\Program Files
(x86)\CommonFiles\MicrosoftShared\WindowsLive;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program
Files\Dell\DW WLAN Card;C:\Program Files\WIDCOMM\Bluetooth
Software\;C:\Program Files\WIDCOMM\Bluetooth
Software\syswow64;C:\Program Files (x86)\Windows Live\Shared;
And when i echo it on cmd
echo %Path% it displays this
C:\oraclexe\app\oracle\product\10.2.0\server\bin;C:\Program Files
(x86)\PC Connectivity Solution\;C:\Program Files\Common
Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common
Files\Microsoft
Shared\WindowsLive;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program
Files\Dell\DW WLAN Card;C:\ProgramFiles\WIDCOMM\Bluetooth
Software\;C:\Program Files\WIDCOMM\Bluetooth
Software\syswow64;C:\Program
Files(x86)\WindowsLive\Shared;C:\oraclexe\app\oracle\product\10.2.0\server\bin;C:\Program
Files (x86)\PC ConnectivitySolution\;C:\Program Files\Common
Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common
Files\Microsoft Shared\Windows
Live;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\System32\WindowsPowerShell\v1.0\;C:\Program
Files\Dell\DW WLAN Card;C:\Program Files\WIDCOMM\Bluetooth
Software\;C:\Program Files\WIDCOMM\Bluetooth
Software\syswow64;C:\Program Files
(x86)\WindowsLive\Shared;F:\Java\jdk1.6.0_38\bin\
Can anybody help why is it displaying same values twice? And is there side effects of this?
P.S: I have created a local Path variable as
%Path%F:\Java\jdk1.6.0_38\bin\
Sometime between WindosXP and Windows7 the interpretation of the user level PATH variable changed. Now it automatically appends the path to the system defined path rather than replacing it the way it previously did.
Thus your local path ends up being %PATH%;%PATH%;F:\Java\jdk1.6.0_36\bin
The good news is it works -- you find the desired files. The bad news is it takes slighly longer to find your java bin files.
Edit: The annoying news is that you can no longer override system defined commands. Defining user level PATH as mybin;%PATH% does not produce the desired results.
If you're doing this in the console you can create a batch script with the following content:
for /F "tokens=2* delims= " %%f IN ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path ^| findstr /i path') do set OLD_SYSTEM_PATH=%%g
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_SZ /d "%OLD_SYSTEM_PATH%;F:\Java\jdk1.6.0_36\bin"
Which basically takes the Path value from the registry and add your path to it. Note that also there is a limit of around 1024 characters in the Path length if you set it in the console with the Set command, and this code workaround this limit.

Resources