I have a list of paths and a list of executables files that I need to concatenate together and look for each file under each said path. However, when I run my code, I am seeing that the executables are not being concatenated, just added as a new line. Additionally, it is only being added to the last object in the list. See code, output, and desired output below:
Code:
$final_paths = #();
$paths = #("C:\Program Files\Microsoft Office\Office15", "C:\Program Files\Microsoft
Office Servers\OFFICE15");
$exes = #("MSOCF.DLL", "access.exe", "word.exe", "wordCnv.exe", "WordViewer.exe", "Excel.exe", "ExcelCnv.exe", "ExcelViewer.exe", "PowerPoint.exe",
"PowerPointViewer.exe", "PowerPointCnv.exe", "Publisher.exe", "Project.exe", "OneNote.exe", "InfoPath.exe Groove.exe", "FrontPage.exe",
"SharePointDesigner.exe", "Visio.exe", "VisioViewer.exe", "Lync.exeOutlook.exe", "WINPROJ.EXE")
foreach ($exe in $exes)
{
$final_paths += $paths"\$exe";
write-output $final_paths;
}
foreach ($found_path in $final_paths)
{
$file = Get-Item -Path $found_path -ErrorAction Ignore;
Write-Output $file
Sample Output:
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
Excel.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
ExcelCnv.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
ExcelViewer.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
PowerPoint.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
PowerPointViewer.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
PowerPointCnv.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
Publisher.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
Project.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
OneNote.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
InfoPath.exe Groove.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
FrontPage.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
SharePointDesigner.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
Visio.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
VisioViewer.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
Lync.exeOutlook.exe
C:\Program Files\Microsoft Office\Office15
C:\Program Files\Microsoft Office Servers\OFFICE15
WINPROJ
...
...
...
you get the idea
Desired Output:
I simply want nothing but the file name returned as a string, like so:
WIINPROJ
Why try and concatenate paths into full names and then try to figure out using Get-Item if that file exists or not?
Using Get-ChildItem things would be a lot easier, because
it can handle an array of folder paths in the -Path parameter
you get only FileInfo objects returned for the specific files you filter through a Where-Object clause so you don't need to check their existance afterwards
you get a lot more information than just the file name, so you can decide later what it is exactly you want in the output
$paths = 'C:\Program Files\Microsoft Office\Office15', 'C:\Program Files\Microsoft Office Servers\OFFICE15'
$exes = 'MSOCF.DLL', 'access.exe', 'word.exe', 'wordCnv.exe', 'WordViewer.exe', 'Excel.exe', 'ExcelCnv.exe', 'ExcelViewer.exe', 'PowerPoint.exe',
'PowerPointViewer.exe', 'PowerPointCnv.exe', 'Publisher.exe', 'Project.exe', 'OneNote.exe', 'InfoPath.exe Groove.exe', 'FrontPage.exe',
'SharePointDesigner.exe', 'Visio.exe', 'VisioViewer.exe', 'Lync.exeOutlook.exe', 'WINPROJ.EXE'
$files = Get-ChildItem -Path $paths -File | Where-Object { $exes -contains $_.Name }
Now you decide what property you want to retrieve from the $files collection
# just the Name?
$files.Name | Select-Object -Unique # unique in case you found the same exe in both folders
# the Fullname perhaps?
$files.FullName
You can use the Path.Combine method from System.IO for this:
$finalPaths = foreach($path in $paths)
{
foreach($exe in $exes)
{
Get-Item ([System.IO.Path]::Combine($path, $exe)) -EA Ignore
}
}
$finalPaths.Name # => Should be the Names of the files found
Alternatively, as Lee_Dailey suggested in his comment, you can use Join-Path to achieve the same result:
$finalPaths = foreach($path in $paths)
{
foreach($exe in $exes)
{
Get-Item (Join-Path $path -ChildPath $exe) -EA Ignore
}
}
Related
I installed powershell 7, which set itself as the default shell. It messed up some apps, and I removed it. However, it's still registered as the default shell in some places. One of them is VS2022. When I build my project:
2>Target PostBuildEvent:
2> The system cannot find the path specified.
2> C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(155,5): error MSB3073: The command "setlocal
2> C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(155,5): error MSB3073: "C:\Program Files\PowerShell\7\pwsh.exe" -noprofile -executionpolicy Bypass -file C:/prj-external-libs/vcpkg/scripts/buildsystems/msbuild/applocal.ps1 -targetBinary C:/8/_tmp/cpp/build/src/Debug/proj.exe -installedDir C:/prj-external-libs/vcpkg/installed/x64-windows/debug/bin -OutVariable out
2> C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(155,5): error MSB3073: if %errorlevel% neq 0 goto :cmEnd
2> C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(155,5): error MSB3073: :cmEnd
2> C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(155,5): error MSB3073: endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
2> C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(155,5): error MSB3073: :cmErrorLevel
2> C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(155,5): error MSB3073: exit /b %1
2> C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(155,5): error MSB3073: :cmDone
2> C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(155,5): error MSB3073: if %errorlevel% neq 0 goto :VCEnd
2> C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(155,5): error MSB3073: :VCEnd" exited with code 3.
2>Done building target "PostBuildEvent" in project "proj.vcxproj" -- FAILED.
2>
2>Done building project "proj.vcxproj" -- FAILED.
I think in general it's done via: options> environment> terminal> restore default.
However, in my case, the issue was with cmake vcpkg, which created a post build event using power shell 7. Need to delete (or just ignore) this event, or rebuild a fresh cmake.
My system path is:
C:\Users\angus\algs4\java\bin;C:\binaryTools\SMLNJ\bin\;C:\Program Files (x86)\Python36-32\Scripts\;C:\Program Files (x86)\Python36-32\;C:\Program Files\Java\jdk1.8.0_66\bin;C:\ProgramData\Oracle\Java\javapath;C:\binaryTools;C:\Program Files (x86)\ActiveState Komodo IDE 8\;C:\PHP\;C:\Program Files (x86)\Haskell\bin;C:\Program Files (x86)\Haskell Platform\2012.4.0.0\lib\extralibs\bin;C:\Program Files (x86)\Haskell Platform\2012.4.0.0\bin;C:\MinGW\msys\1.0\bin;C:\MinGW\bin;C:\Program Files\Java\jdk1.8.0_66\bin;c:\Program Files (x86)\AMD APP\bin\x86_64;c:\Program Files (x86)\AMD APP\bin\x86;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Services\IPT\;c:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\NTRU Cryptosystems\NTRU TCG Software Stack\bin\;C:\Program Files\NTRU Cryptosystems\NTRU TCG Software Stack\bin\;c:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\TortoiseHg\;C:\Program Files (x86)\Haskell Platform\2012.4.0.0\mingw\bin;C:\Python33;C:\Python33\Scripts;C:\Program Files\Microsoft Platform SDK\Bin\.;C:\Program Files\Microsoft Platform SDK\Bin\WinNT\.;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\;C:\Python27;C:\Python27\Scripts;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files\Git\cmd;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Collaborator Client;C:\Program Files\doxygen\bin;C:\Program Files\TortoiseGit\bin;
And I am trying to update a registry key (the system path) using a script but am getting a wierd error:
>myscript.cmd
\Python36-32\Scripts\ was unexpected at this time.
How can I fix the script error?
Here is the script:
echo off
SET PROGPATH=C:\dell\mypath
for /f "tokens=2*" %%a in ('REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path') do set "SysPath=%%~b"
echo system path is: %SysPath%
echo %SysPath%>tmppath.txt
echo %PATH% | findstr /i %PROGPATH%
if %ERRORLEVEL% EQU 1 (
echo %PROGPATH% path not found in system path
REM we now add to syspath and then write back to registry
set UPDATEDPATH=%PROGPATH%;%SysPath%
REM we now just write UPDATEDPATH back to registry
REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /f /v "Path" /t REG_SZ /d "%UPDATEDPATH%"
echo %PROGPATH% should now be added to the system path
) else (
echo %PROGPATH% already in path
)
The problem line is: echo %PATH% | grep %PROGPATH%
you get the error wherever there is a
C:\Program Files (x86)\Java\jre1.8.0_45\bin;
or something like that.
ie the (x86)\J or (x86)\P etc
So how to fix that?
Was asked for more details. So here.
Before running script, my env is like this:
keyname:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
Path key has value:
C:\Users\angus\algs4\java\bin;C:\binaryTools\SMLNJ\bin\;C:\Program Files (x86)\Python36-32\Scripts\;C:\Program Files (x86)\Python36-32\;C:\Program Files\Java\jdk1.8.0_66\bin;C:\ProgramData\Oracle\Java\javapath;C:\binaryTools;C:\Program Files (x86)\ActiveState Komodo IDE 8\;C:\PHP\;C:\Program Files (x86)\Haskell\bin;C:\Program Files (x86)\Haskell Platform\2012.4.0.0\lib\extralibs\bin;C:\Program Files (x86)\Haskell Platform\2012.4.0.0\bin;C:\MinGW\msys\1.0\bin;C:\MinGW\bin;C:\Program Files\Java\jdk1.8.0_66\bin;c:\Program Files (x86)\AMD APP\bin\x86_64;c:\Program Files (x86)\AMD APP\bin\x86;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Services\IPT\;c:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\NTRU Cryptosystems\NTRU TCG Software Stack\bin\;C:\Program Files\NTRU Cryptosystems\NTRU TCG Software Stack\bin\;c:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\TortoiseHg\;C:\Program Files (x86)\Haskell Platform\2012.4.0.0\mingw\bin;C:\Python33;C:\Python33\Scripts;C:\Program Files\Microsoft Platform SDK\Bin\.;C:\Program Files\Microsoft Platform SDK\Bin\WinNT\.;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\;C:\Python27;C:\Python27\Scripts;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files\Git\cmd;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Collaborator Client;C:\Program Files\doxygen\bin;C:\Program Files\TortoiseGit\bin;
>echo %PATH%
C:\Users\angus\algs4\java\bin;C:\binaryTools\SMLNJ\bin\;C:\Program Files (x86)\Python36-32\Scripts\;C:\Program Files (x86)\Python36-32\;C:\Program Files\Java\jdk1.8.0_66\bin;C:\ProgramData\Oracle\Java\javapath;C:\binaryTools;C:\Program Files (x86)\ActiveState Komodo IDE 8\;C:\PHP\;C:\Program Files (x86)\Haskell\bin;C:\Program Files (x86)\Haskell Platform\2012.4.0.0\lib\extralibs\bin;C:\Program Files (x86)\Haskell Platform\2012.4.0.0\bin;C:\MinGW\msys\1.0\bin;C:\MinGW\bin;C:\Program Files\Java\jdk1.8.0_66\bin;c:\Program Files (x86)\AMD APP\bin\x86_64;c:\Program Files (x86)\AMD APP\bin\x86;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Services\IPT\;c:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\NTRU Cryptosystems\NTRU TCG Software Stack\bin\;C:\Program Files\NTRU Cryptosystems\NTRU TCG Software Stack\bin\;c:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\TortoiseHg\;C:\Program Files (x86)\Haskell Platform\2012.4.0.0\mingw\bin;C:\Python33;C:\Python33\Scripts;C:\Program Files\Microsoft Platform SDK\Bin\.;C:\Program Files\Microsoft Platform SDK\Bin\WinNT\.;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\;C:\Python27;C:\Python27\Scripts;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files\Git\cmd;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Collaborator Client;C:\Program Files\doxygen\bin;C:\Program Files\TortoiseGit\bin;C:\Users\angus\algs4\bin;C:\Users\angus\algs4\java\bin;C:\Users\angus\AppData\Roaming\cabal\bin;C:\Program Files\Microsoft Platform SDK\Bin\.;C:\Program Files\Microsoft Platform SDK\Bin\WinNT\.;C:\binaryTools\emacs-24.5-bin-i686-mingw32\bin;C:\windows;C:\windows\system32;C:\Users\angus\AppData\Roaming\Dashlane\4.7.1.28771\bin\Firefox_Extension\{442718d9-475e-452a-b3e1-fb1ee16b8e9f}\components;C:\Users\angus\AppData\Roaming\Dashlane\4.7.1.28771\ucrt
now if I run the batch file contents above which I named path3.cmd I get:
>path3.cmd
\Python36-32\Scripts\ was unexpected at this time.
>
Trying
echo %PATH% | findstr /i /c:"%PROGPATH%"
or
echo "%PATH%" | findstr /i /c:"%PROGPATH%"
makes no difference.
The bug seems to be in this line:
set UPDATEDPATH=%PROGPATH%;%SysPath%
When I run the command set programfiles in the command prompt, I get
ProgramFiles=C:\Program Files (x86)
ProgramFiles(x86)=C:\Program Files (x86)
However, the following code in python
import os
print os.getenv("programfiles")
or
msgbox %A_ProgramFiles% and %ProgramFiles%
in Autohotkey
or
$env:ProgramFiles
in PowerShell
all results in C:\Program Files
I cannot understand why I'm getting different results for the Program Files Environment Variables in Windows
What you see is the difference between a 32-bit and 64-bit application.
32-bit cmd.exe (%SystemRoot%\SysWOW64\cmd.exe)
C:\>set programfiles
ProgramFiles=C:\Program Files (x86)
ProgramFiles(x86)=C:\Program Files (x86)
64-bit cmd.exe (%SystemRoot%\System32\cmd.exe)
C:\>set programfiles
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)
32-bit powershell.exe (%SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe)
PS C:\> dir env:\programfiles*
Name Value
---- -----
ProgramFiles(x86) C:\Program Files (x86)
ProgramFiles C:\Program Files (x86)
64-bit powershell.exe (%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe)
PS C:\> dir env:\programfiles*
Name Value
---- -----
ProgramFiles(x86) C:\Program Files (x86)
ProgramFiles C:\Program Files
So I am running several instances of my program and I have to be able to distinguish between the instances in a good way from powershell. Since every instance is installed in a separate folder I thought that i could use:
get-process -Name MyProgram* | Select-Object name, path
and get a list showing me the folder from which the process was started. Unfortunately this returns with the path empty.
So I tried a bunch of other properties that might be unique but all of them come back empty.
What am I doing wrong? Or can I do something else perhaps?
I am using windows 8 btw.
Could you use PID? Or does your program start child-processes. Path works for me, like:
Get-Process iexplore | Select-Object ID, Name, Path
Id Name Path
-- ---- ----
10792 iexplore C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE
13928 iexplore C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE
17144 iexplore C:\Program Files\Internet Explorer\iexplore.exe
17772 iexplore C:\Program Files\Internet Explorer\IEXPLORE.EXE
20896 iexplore C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE
Does ExecutablePath or CommandLine return anything?
Get-WmiObject -Class Win32_Process -Filter "Name LIKE 'iexplore%'" | Select-Object ProcessID, Name, ExecutablePath, Commandline
ProcessID Name ExecutablePath Commandline
--------- ---- -------------- -----------
17144 iexplore.exe C:\Program Files\Internet Explorer\iexplore.exe "C:\Program Files\Internet Explorer\iexplore.exe" ...
32016 iexplore.exe C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE "C:\Program Files (x86)\Internet Explorer\IEXPLORE...
36744 iexplore.exe C:\Program Files\Internet Explorer\IEXPLORE.EXE "C:\Program Files\Internet Explorer\IEXPLORE.EXE" ...
If the process doesn't belong to you, then you need to run it as admin/elevated.
You have to be running with elevated permissions to see the path information of processes you're not the owner of.
I have tried something like this but did not work:
#echo off
call "c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC"
mstest /testcontainer: mytest.dll
save as .bat file and when i double click it does nothing.
So, I am trying to open command prompt located in visual studio and execute.
Are you trying to change the directory to "c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC"?
Try this:
#echo off
CD "c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC"
mstest /testcontainer: mytest.dll
PAUSE
Your batch file seems to be a little out of shape there. I'm no expert, but you have:
call "C:\Program Files\Microsoft Visual Studio 2008\VC\"
...but without a batch file name. See this for info.
If mstest is your batch file name then the call should be more like:
call "C:\Program Files\Microsoft Visual Studio 2008\VC\mstest.bat " <arguments>