Visual Studio and console window to see Echo from batch file - visual-studio

I'm getting familiar with VS2008. I made a batch file, with some Echo statements.
I have the batch file running after a successful build.
Is there a console window that I may see the echo outputs?

If I'm understanding you correctly, look at the Output window.
To get the output window, Go to View -> Output or Ctrl+W, O

To display the Output window, select Output from the View menu.

Related

Open Multiple Instances of an App from File Explorer

Short version:
On Windows, if I select several .txt files in a File Explorer and then right click on one of the selected files and choose "Open" from the popup menu, all of the selected files will be opened, each in a separate instance of Notepad.
I want the same behavior for our files/editor: select multiple output files, right click one, select Open: -> all files are opened, each in their own instance of our editor.
I have basic click-to-open working (for a single file, double click opens the file in our editor, right click and Open does the same).
Details
We have an launcher (FuBarStudioLauncher.exe) that updates then launches our editor tool (FuBarStudio.exe), which produces resource files with a custom extension (samplefile.fbr)
If you run from the command line, you can fire up the editor, opening a particular resource:
C:\path_to_my_exe\FuBarStudio.exe" -ide samplefile.fbr
I have a windows registry entry set up so that when I double click on a *.fbr resource file, or right-click and select Open from the popup menu, the launcher runs, passing the given resource through to the editor.
The relevant registry entries:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.fbr]
#="FuBar.Place"
[HKEY_CLASSES_ROOT\.fbr\FuBar.Place]
[HKEY_CLASSES_ROOT\.fbr\FuBar.Place\ShellNew]
and
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\FuBar.Place]
#="FuBar Place"
[HKEY_CLASSES_ROOT\FuBar.Place\shell]
[HKEY_CLASSES_ROOT\FuBar.Place\shell\Open]
#="Open"
[HKEY_CLASSES_ROOT\FuBar.Place\shell\Open\command]
#="\"C:\\path_to_my_exe\\FuBarStudioLauncher.exe\" -ide \"%1\""
All of this works fine for single files.
If I select more than one .fbr file and then right click one of the selected files and select "Open", I will only open one instance of FuBar Studio.
I would like to open one Copy of FuBar Studio, one for each file.
So with some help I got a useful answer.
1) I read up on use of MultiSelectModel here:
https://msdn.microsoft.com/en-us/library/windows/desktop/hh127436(v=vs.85).aspx
Set my type to Document.
2) I created a batch script as an intermediary, so I could see better what was going on:
[HKEY_CLASSES_ROOT\FuBar.Place\shell\Open\command]
#="\"C:\\path_to_my_exe\\launcher.bat\" \"%1\""
Toyed around with this script, writing things to tmp files:
echo "interesting things" >> logs.txt
and using timeout to force things to pause/slow down:
timeout 10
Using "echo" and so forth I realized that whether MultiSelectModel is set to Document or Player, when I select multiple files and right click "Open", Windows is executing the business at:
HKEY_CLASSES_ROOT\FuBar.Place\shell\Open\command
once per selected file.
I could not find a way to force Windows to call the command once, passing all the files as arguments to the command.
3) By experimenting with other programs besides my own, and with the script + timeouts giving me a command shell with error spew to look at, I realized that my particular program, FuBarStudioLauncher.exe, always fails if another instance is running.
So here's what I have:
Windows is going to run one instance of <something> per input file.
<something> can't be my FuBarStudioLauncher.exe, because after one is running the next N will fail.
So I need an intermediary script that will do some kind of critical section locking:
- grab lock.
- run launcher to completion.
- release lock.
Hooray for StackOverflow:
Semaphore for CMD instances working in Parallel
Using this as input, my final product:
:lockedAppend
2>nul (
>>%~dp0launcher_script_lockfile.txt (
REM Do any amount of processing within this block.
REM All stdout will safely be appended to the queue
REM You could even call out to a subroutine, or another batch file
%~dp0FuBarStudioLauncher.exe -ide "%1"
REM The CALL below ensures that the block exits with ERRORLEVEL 0 - success
(call )
)
) || goto :lockedAppend
That did it!

How to use a batch file so that results remain visible on completion

I run batch files and they exit immediately. I dont want that to happen so that i can see my output. Can someone tell me how to make this happen ?
I use windows 7.
Put this on the very last line of the Batch:
cmd /k
Adding pause is a good answer. Here are some other ways as well..
Rather than double-clicking on them to execute you can run from a command line:
Press the windows key + r (this opens the "run" window)
Type: cmd into the text input and press enter (or click ok)
Change to the directory that contains the batch file, e.g: cd c:\scripts\foo
Execute the batch file by typing it's name and pressing enter, e.g: somename.bat
If there is a lot of output and it scrolls off the screen you can direct the output to a text file instead like so:
somename.bat > output_filename.txt
Then you can open the 'output_filename.txt' file in any text editor to view/search all of the output. This is better than pause when there more output than what is available in the scrollback.
Add the pause command at the end of your batch file. This waits for you to key something in.
(The nice thing is that if you're running the batch file from a non-interactive process, such as a automated build system or scheduled task, the pause is simply skipped.)
The help message for pause is:
C:\>help pause
Suspends processing of a batch program and displays the message
Press any key to continue . . .
If there is lots of output and you can't scroll far enough back, adjust the screen buffer height of the command window. This can be done via right-click on the c:\ icon go to properties -> layout:

How to run a .bat from inside the IDE

What I want to do is very simple, yet can't find the way to do it.
In my projects I have several .bat files (I know, I'm old) the perform some tasks like running SqlMetal, etc.
I just want to right-click the file and select "run" or something so the bat is executed. How do I do it?
Note: I know I can use Tools -> External Tools, but it's not what I'm looking for
If each project has a few batch files associated with it then why not include them in the project and add a new external tool and link it with a custom menu item to run the tool? This will give you a list of batch files in each project and a context menu command to run them. The only downside is that it isn't file type specific (it will let you run any file as a batch file).
Here's how to do it...
Create an external tool called "Run batch file"
Set the Command to: CMD.EXE
Set the Arguments to: /c "$(ItemPath)"
Set the Initial directory to: $(ItemDir)
Check the "use output window" checkbox and then Apply to create the command
Note where the new command appeared in the list of commands. The external commands are numbered from 1 starting below the divider bar. #1 is usually "Create GUID"
Now go to Tools -> Customize and select the commands tab.
Select the Context menu radio button and select "Project and Solution Context menus | Item" from the dropdown.
Now use "Add Command..." to add a new command
In the Categories list select "Tools"
From the commands select the "External Command #" that corresponds to the position of the "Run Batch file" custom command you noted the number of in step 5 above.
Move it to the correct position in the list add keyboard shortcuts etc.
Close the dialog.
Now right click on the batch file and you should see a "Run batch file" menu item. This will execute the batch file and show its output in the VS Output window.
Right click the batch file in the Solution Explorer
Select “Open With…” from the context menu
Click “Add…”
In the “Program name” textbox, enter powershell.exe
In the “Friendly name” textbox enter “PowerShell”
Select “Set As Default”
Click OK
via
Check out the Open Command Line Visual Studio extension.
As per the description, it features:
Opening of a command line at the root of the project.
Support for all consoles such as CMD, PowerShell, Bash etc.
Syntax highlighting, Intellisense and execution of .cmd and .bat files.
For files in your solution, a context-menu button shows up.
Alternatively, the keyboard shortcut Shift+Alt+5 can be used when editing a batch file. This makes it really easy and fast to execute any batch file - even ones that are not part of your project.
Right-click a file in the Solution Explorer window, Open With, click the Add button to add your .bat files. Use %1 in your .bat to get the path to the selected file.
Couldn't get any of the above to quite work, but this did (VS 2019/2022):
Right click a .bat file in Solution Explorer and Open With...
Add...
Program: CMD.exe
Arguments: /c %1
Friendly name: Command
OK and Set as Default for double clicking of batch files to run from Solution Explorer as the default (or right click Open With... and select Command if you don't want it the default).
Merely a suggestion: This is a scenario which can be done with the VsCommandBuddy extension. The following configuration is an example:
{
"cmdname": "mk",
"title": "Run MK.BAT",
"description": "Run the MK bat file.",
"cwd": "$(SolutionDir)",
"filename": "cmd.exe",
"arguments": "/C mk.bat",
"async": false
}
You can assign shortkeys as well, so you can invoke your batch files with shortcut keys. Output comes in your output pane.

Visual C++ Test project doesn't display standard output

I created a Visual C++ test project which contains printf statements. Unfortunately, I can't find a method to view the output of this. It isn't included in the output window. Any suggestions?
You can redirect standard output to a file and open this file in some auto-refreshing viewer.
UPDATE:
Here are two ways to redirect standard output to a file, that I found:
cmd way: C:\Test>myWinApp.exe > log.txt
powershell way: PS C:\Test> .\myWinApp.exe 2>&1 | out-file log.txt
If you want to view the output in real time, you can open log.txt in some file viewer with auto-refreshing option. see link

How to show linker command line in Build output, in Visual Studio?

This question is related to How to show command line build options in Visual C++ 2008
But in addition to showing the Compiler command line, I'd like to see the linker command line.
I have "unset" the /nologo option in the linker options.
But I still don't get the command line in the output window. What do I need to do? Is this possible?
Did you try viewing the build log? This is probably the closest to what you want to see. You view the output window, and where it says "Build log was saved at "path..."", ctrl-click on "path" and it will open the build log.
I often get burned by the wrong configuration being selected when I change things like suppress startup banner.
Did you try setting the selection when all configurations and platforms were selected?

Resources