1

Each one of the following commands when entered manually in the normal windows command prompt (cmd, not powershell) runs fine one after the other:

cd cmake\windows\rel
"C:\Program Files (x86)\Intel\oneAPI\setvars.bat"
vtune -collect hotspots --app-working-dir=%cd% -- %cd%/CMakeProject
set /p name="Enter Hotspot Folder name: "
vtune-gui %name%/%name%.vtune

Essentially, from within my project folder, I navigate to the folder where the executable is built (first line), folder cmake\windows\rel, then I load the environment variables by running the batch file setvars.bat. This is then followed by running the profiler via the vtune command. Then, after accepting a user input of the folder name, that folder name is provided as an input to the vtune-gui command.

Note that vtune and vtune-gui are not in the Windows path. setvars.bat temporarily for the cmd session creates this path and allows me to use vtune and vtune-gui on the command line.

When I try these commands via a batch file (bfile.bat) from within the default powershell terminal of VSCode, I have the following:

PS MyProjectFolder> .\bfile.bat
MyProjectFolder> cd cmake\windows\rel
MyProjectFolder\cmake\windows\rel> "C:\Program Files (x86)\Intel\oneAPI\setvars.bat"
...output of setvars.bat here...it runs without errors...
PS MyProjectFolder>

The third, fourth and fifth lines of the batch file do not run at all and I do not get any output from powershell indicating some error or so occurred. The last line of the output above tells me that I am back to MyProjectFolder now. Also, note that only the first and last line of the output above are prefixed with PS. The second, third and other lines of output do not have that prefix.

How can I get the batchfile to run and perform all of the five steps which currently I am forced to do manually?

0

1 Answer 1

2

In your batch file (bfile.bat), replace:

"C:\Program Files (x86)\Intel\oneAPI\setvars.bat"

with:

call "C:\Program Files (x86)\Intel\oneAPI\setvars.bat"

This ensures that setvars.bat returns control to your batch file and resumes execution; without call, execution of your batch file ends when setvars.bat ends.
Run cmd /c call /? for details.


Note:

  • Any batch file launched from PowerShell of necessity runs in a (cmd.exe) child process, so that any changes to the working directory and environment variables performed in a batch file are not seen by the calling PowerShell session.

  • Thus, if you wanted to call setvars.bat and have PowerShell's environment variables modified, additional work would be needed; see this answer for details.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for reopening and providing this answer. Now, I am running into a new issue. The vtune line certainly now runs after your suggestion. However, one property of the vtune app is that as it is profiling my CMakeProject.exe app, if I terminate the app prematurely by CtrlC, vtune still continues to function and provides a profile of the app until it was terminated. Now, however, from within the powershell, this gives an error "The terminal process powershell.exe exited with exit code 259". vtune does not complete. The last two and a half lines of the batch file do not run.
I have been able to figure out a work around. From within powershell, I launch start cmd. This spawns an independent cmd. Then, from there, I launch bfile.bat. This allows me to work with CtrlC as needed. Thank you!
One last question. Currently, from powershell, I say start cmd and then from within this cmd I say bfile.bat. Can these two steps be combined into one? From powershell, can I start cmd (bfile.bat) or something like that to automatically have cmd start running bfile.bat instead of needing my manual intervention?
@Tryer: You can do Start-Process bfile.bat, but this will auto-close the window in which the batch file runs when the latter exits; to avoid that, use Start-Process cmd '/k bfile.bat'
This is perfect! A heartfelt appreciation, gratitude and thank you for your inputs!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.