How do I run a .bat file from CMake? - batch-file

How do I run a .bat file from CMake in a pre-link or a post-build event?

You could use add_custom_command, e.g.
if(WIN32)
add_custom_command(TARGET <Your target>
POST_BUILD
COMMAND cmd //C <path to .bat file> <ARGS> )
endif()
For full details about add_custom_command run
cmake --help-command add_custom_command

The following also works. In case you read or create a file inside the bat script don't forget to specify the exact path inside the bat script.
ADD_CUSTOM_TARGET(
myCustomTarget
COMMAND cmd /c E:/Myfiles/mytxt.bat
)
ADD_DEPENDENCIES(myTarget myCustomTarget)
myTarget will be executed after myCustomTarget.

Related

Can you use a path with a wildcard in SET PATH=%PATH%

I have a batch file currently which sets the path for Blender 2.91 then changes the directory so that blender can launch, open a desired .blend file and run a python script. This currently works:
SET PATH=%PATH%;"C:\Program Files\Blender Foundation\Blender 2.91"
N:
cd ""N:\R^&D\Product_Design\CAD_Drawings\Renders\Render Templates\Creo Direct Integration""
blender -b Creo_Import.blend --background --python Creo_Import.py
I want this to run for future releases of blender ie. Blender 3.00 so would like to use a wildcard in the path. I have tried this but it does not work:
SET PATH=%PATH%;"C:\Program Files\Blender Foundation\Blender*"
N:
cd ""N:\R^&D\Product_Design\CAD_Drawings\Renders\Render Templates\Creo Direct Integration""
blender -b Creo_Import.blend --background --python Creo_Import.py
The error message is "'blender' is not recognized as an internal or external command, operable program or batch file."
However if I run:
cd "C:\Program Files\Blender Foundation\Blender*"
blender -b Creo_Import.blend --background --python Creo_Import.py
Blender will launch with no issues, but the .blend and .py file can not be found as the directory is incorrect. Any help will be greatly appreciated.

Creating a cmd line shortcut

I am completely new to Windows, as i am an OSX user.
I am having to run a task in the cmd line every day, is there any way i can create a shortcut on my desktop which automates this?
Task example, open cmd, then execute:
atomInstaller npm -view -Xmx8g -Xms3g
Thanks
You could just make a shortcut to cmd.exe and add command line switches:
https://superuser.com/questions/358565/adding-command-line-switches-to-windows-shortcuts
Or you can put commands into a batch file using notepad ( or a better text editor ):
#echo off
atomInstaller npm -view -Xmx8g -Xms3g
save it as npm.bat then double-click it..
You can use shortcutjs.bat:
call shortcutjs.bat -linkfile "%userprofile%\desktop\atominstaller.lnk" -target "C:\atomInstaller.bat" -adminpermissions yes -iconlocation "C:\Windows\System32\compstui.dll,3" -hotkey "Ctrl+Shift+M"
Here's the usage:
shortcutjs.bat -linkfile link -target target [-linkarguments linkarguments] [-description description] [-iconlocation iconlocation] [-hotkey hotkey] [-windowstyle 1|3|7] [-workingdirectory workingdirectory] [-adminpermissions yes|no]
You can add additional parameter with -linkarguments or start location with -workingdirectory

execute command inside cmd application with one command

I installed Linphone application
http://www.linphone.org/technical-corner/linphone/documentation
I am running this application via cmd executing this command
"C:\Program Files (x86)\Linphone\bin\linphonec.exe"
then I make the call through this command
call number#x.x.x.x
I want to join the tow commands in one line so the cmd start the linphone then execute the call command inside the linphone
Output with & operator
C:\Users\Desktop\1>CD "C:\Program Files (x86)\Linphone\bin\"
C:\Program Files (x86)\Linphone\bin>linphonec.exe & call number#x.x.x.x
WARNING: no real random source present!
Ready
Warning: video is disabled in linphonec, use -V or -C or -D to enable.
linphonec>
how i can do that in cmd ?
CD C:\Program Files (x86)\Linphone\bin\
echo call number#x.x.x.x|linphonec.exe
may work.

cmake: check if file exists at build time rather than during cmake configuration

I have a custom build command that needs to check if a certain file exists. I tried using
IF(EXISTS "/the/file")
...
ELSE()
...
ENDIF()
but that test is only evaluated one; when cmake is first run. I need it to perform the test every time a make is done. What's the method to check at make-time? Thanks.
You can use add_custom_command to invoke CMake itself in script mode by using the -P command line option.
So your command would be something like:
set(FileToCheck "/the/file")
add_custom_command(TARGET MyExe
POST_BUILD
COMMAND ${CMAKE_COMMAND}
-DFileToCheck=${FileToCheck}
-P ${CMAKE_CURRENT_SOURCE_DIR}/check.cmake
COMMENT "Checking if ${FileToCheck} exists...")
and your script file "check.cmake" would be something like:
if(EXISTS ${FileToCheck})
message("${FileToCheck} exists.")
else()
message("${FileToCheck} doesn't exist.")
endif()
A similar idea
You're going to need add_custom_command, but if you're willing to be a little Unix specific you can always use test.
set(FileToCheck "/the/file")
add_custom_command( OUTPUT output.txt
COMMAND test -e output.txt || echo "Do something meaningful"
COMMENT "Checking if ${FileToCheck} exists...")
[#Frazers] answer is great if you need to add custom logic inside the if(EXISTS).
However, if you just need your build to fail if a single file is missing you can use the cmake -E command line mode to compare the file with itself.
This will make sure the build always stops if the provided file doesn't exist, it's cross-platform safe and works in every version of cmake since 3.0.
set(_fileToCheck "foo.txt")
add_custom_command(TARGET YourTarget
PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E compare_files ${_fileToCheck} ${_fileToCheck}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Checking if ${_fileToCheck} exists, build will fail if it doesn't"
)

.bat file to open executable within Console2

I am aiming to create a .bat launcher that will execute a command line .exe program within Console2.
My best guess would be that it should go something like:
#echo off
start "" Console.exe program.exe
but all that does it open Console2.
Please note that all .bat, and executables are all in the same folder.
Ok I looked in source for the Console.exe and drilled down into the compiled help.
You need a -r
So: Console.exe -r program.exe
Command line parameters
Console supports these command line parameters:
-c <configuration file>
Specifies a configuration file.
-w <main window title>
Sets main window title. This option will override all other main window title settings (e.g. 'use tab titles' setting)
-t <tab name>
Specifies a startup tab. Tab must be defined in Console settings.
-d <directory>
Specifies a startup directory. If you want to parametrize startup dirs, you need to specify startup directory parameter as "%1"\ (backslash is outside of the double quotes)
-r <command>
Specifies a startup shell command.
-ts <sleep time in ms>
Specifies sleep time between starting next tab if multiple -t's are specified.
I'd never heard of this program, but its source code
else if (wstring(argv[i]) == wstring(L"-r"))
{
// startup cmd
++i;
if (i == argc) break;
startupCmds.push_back(argv[i]);
}
makes it seem like you might want to try:
Console.exe -r program.exe

Resources