Issue
Lets Consider a lst file "txt_filelist.lst" which has different text files mentioned with different path's, Now After reading the "txt_filelist.lst" file how to identify a specific text file along with its path in CMAKE?
txt_filelist.lst File:
variants\EXX\application\a2l\target_ecu\srcxx_xx_xx\CRCa2lCfgEXX_srcxx_xx_xx.txt
variants\EXX\application\a2l\target_src\srcxx_xx_xx\CRCa2lCfgEXX_srcxx_xx_xx_1.txt
variants\EXX\application\a2l\target\srcxx_xx_xx\CRCa2lCfgEXX_srcxx_xx_xx_2.txt
variants\EXX\application\a2l\target_base\srcxx_xx_xx\CRCa2lCfgEXX_srcxx_xx_xx_3.txt
After reading the lst file txt_filelist.lst
How can I first identify the CRCa2lCfgEXX_srcxx_xx_xx.txt
in the lst and then secondly How can I fetch its entire path as variants\EXX\application\a2l\target_ecu\srcxx_xx_xx\CRCa2lCfgEXX_srcxx_xx_xx.txt
in CMAKE?
Thanks in Advance..!!!
Solution
You can use file(STRINGS)
to read the lines of the file to a list variable. get_filename_component
can be used to separate file names according to your needs. Using file(TO_NATIVE_PATH)
may be necessary, if you use get_filename_component
in a way that yields a path containing separators, but most tools should be able to work with forward slashes, even on Windows:
file(STRINGS txt_filelist.lst FILE_LINES LENGTH_MINIMUM 1)
message("=========================")
foreach(LINE IN LISTS FILE_LINES)
get_filename_component(FILE_NAME "${LINE}" NAME)
get_filename_component(FILE_DIR "${LINE}" DIRECTORY)
file(TO_NATIVE_PATH "${LINE}" FILE_PATH_NATIVE)
message(
"------------------------
LINE = '${LINE}'
FILE_NAME = '${FILE_NAME}'
FILE_DIR = '${FILE_DIR}'
FILE_PATH_NATIVE = '${FILE_PATH_NATIVE}'
"
)
endforeach()
message("=========================")
Note: The LENGTH_MINIMUM 1
option is there to ignore empty lines. This may not be necessary depending on the input you get.
Answered By - fabian Answer Checked By - Cary Denson (WPSolving Admin)