Echoing path variable in windows displaying twice - windows-7

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.

Related

How to get each string in a list of strings?

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%

autocomplete path in cmd batch

Windows CMD prompt's auto-complete, (similar feature in other terminals), comes very handy sometimes when you are not sure of the right path or file name.
Bottom line is, how to use this feature in batch scripting.
Example: the script "C:\Program Files\Java\jre1.8.0_92\bin\javaw.exe" -jar post.jar
the thing here is that java version is not always the same, so it needed to be something like this
"C:\Program Files\Java\jre*\bin\javaw.exe" -jar post.jar
you can't use wildcards in the middle of a path, but you can at the end (the last element). Because you need it in between, split it up:
for /f "delims=" %%a in ('dir /b /ad /on "C:\Program Files\Java\jre*"') do set ver=%%a
set "exec=C:\Program Files\Java\%ver%\javaw.exe"
"%exec%" -jar post.jar
This will get you the path with the highest version number, if there are more than one.
Wildcards are supported by only some commands. Moreover, cmd restricts wildcards in a file path only in a path leaf i.e. in token behind last backslash…
On an unknown Windows system: if you do not have control on environment variables then you need to find a file path e.g. as follows (note the _checkPath variable assignment is changed to get reasonable output as I do not have java installed):
#ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
rem assign path with a wildcard * not in path leaf
set "_checkPath=C:\Program Files\Java\jre*\bin\javaw.exe"
rem delete or comment-up next line i.e. my test data set "_checkPath=..."
set "_checkPath=%ProgramFiles%*\Microsoft SQL Server\1*\LocalDB\Binn\sqlservr%1.exe"
set "_itemFirst="
rem previous line: delete variable; next line: show setting
set _checkPath
echo(---
rem next two lines: ensure that a wildcard * not in path leaf is not allowed
echo dir /B /S "%_checkPath%"
dir /B /S "%_checkPath%"
echo(---
rem find all files by parsing powershell output ( note proper escaping ^^^| )
for /F "usebackq tokens= 1* delims=: " %%G in (`
powershell -C Get-ChildItem '"%_checkPath%"' -Recurse ^^^| Format-List -Property FullName
`) do (
rem assign first found item
if not defined _itemFirst set "_itemFirst=%%~H"
rem assign every found item thus last found item
set "_item_Last=%%~H"
rem show every found item
echo .exe found %%~H
)
echo(---
rem show setting found
set _item
echo(---
if defined _itemFirst (
echo success: "%_checkPath%"
rem commands are merely ECHOed and (my test data) commented up
rem use found items (choose any)
rem ECHO "%_itemFirst%" -jar post.jar
rem ECHO "%_item_Last%" -jar post.jar
) else (
echo NOT FOUND "%_checkPath%"
)
Above code snippet is richly commented where necessary for better understanding. Output shows both variants:
==> D:\bat\SO\38281447.bat XXX
_checkPath=C:\Program Files*\Microsoft SQL Server\1*\LocalDB\Binn\sqlservrXXX.exe
---
dir /B /S "C:\Program Files*\Microsoft SQL Server\1*\LocalDB\Binn\sqlservrXXX.exe"
The filename, directory name or volume label syntax is incorrect.
---
---
Environment variable _item not defined
---
NOT FOUND "C:\Program Files*\Microsoft SQL Server\1*\LocalDB\Binn\sqlservrXXX.exe"
==> D:\bat\SO\38281447.bat
_checkPath=C:\Program Files*\Microsoft SQL Server\1*\LocalDB\Binn\sqlservr.exe
---
dir /B /S "C:\Program Files*\Microsoft SQL Server\1*\LocalDB\Binn\sqlservr.exe"
The filename, directory name or volume label syntax is incorrect.
---
.exe found C:\Program Files\Microsoft SQL Server\100\LocalDB\Binn\sqlservr.exe
.exe found C:\Program Files\Microsoft SQL Server\110\LocalDB\Binn\sqlservr.exe
.exe found C:\Program Files\Microsoft SQL Server\120\LocalDB\Binn\sqlservr.exe
---
_itemFirst=C:\Program Files\Microsoft SQL Server\100\LocalDB\Binn\sqlservr.exe
_item_Last=C:\Program Files\Microsoft SQL Server\120\LocalDB\Binn\sqlservr.exe
---
success: "C:\Program Files*\Microsoft SQL Server\1*\LocalDB\Binn\sqlservr.exe"
==>

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.

Running dir against multiple directories

If I run, for example:
dir /s /b /o:gn "c:\Program Files\TrueCrypt" | findstr .sys
I comfortably get
c:\Program Files\TrueCrypt\truecrypt.sys
c:\Program Files\TrueCrypt\truecrypt-x64.sys
..in return. But if I add a folder that doesn't exist (I won't go into the reasons why this would be the case, but it will be), I just get an error:
dir /s /b /o:gn "c:\Program Files\TrueCrypt" "c:\non folder\non subfolder" | findstr .sys
The system cannot find the file specified.
Unlike in the UNIX world where I can use "find" and it returns files found in any directories without only barfing on the directories that don't exist.
You can use a loop and call dir for each of the folders:
for %F in ("c:\Program Files\TrueCrypt" "c:\non folder\non subfolder") do (
dir /s /b /o:gn "%F"
) | findstr .sys
However, depending on what you need exactly you can probably do better by ditching dir and findstr here and opt for a loop over the files. But that depends a bit on what your overall goal is.

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.

Resources