Search code examples
batch-filetensorboard

How to check if a certain type of file exists with batch file?


I'm studying TensorFlow now, and often write event files likeevents.out.tfevents.%some number%.%PC name%.

I want to write a batch file to check if there exists one or more event files. (1)If such files exist, then let user decide whether to overwrite previous files (2)If not, run the python script to generate a new event file, then activate TensorBoard.

Suppose I have defined a user variable %TF_DIR%

With that in mind, I write something like:

set pyName=%1
cd %TF_DIR%/graphs/%pyName%
if exist *.tfevents.* 
(
    # ask if user want to overwrite, then decided whether to run the script again
)
else
( 
    echo No previous event files detected, run the script to create a new event...
    python %tfDIR%/%pyName%.py
)

tensorboard --logdir=%cd%
cd %TF_DIR%

Strangely the file is not working and returns something like "invalid command syntax" at the if exist if exist *.tfevents.*. I cannot find what goes wrong. Can someone help me? Thanks!


Solution

  • The syntax is incorrect; in the construction if ... ( ... ) else ( ... ) the ( must be of the same line as if, and ) else ( must be on the same line.

    As a supplementary note, unless you have something against labels and goto, I would suggest using them instead of fancy modern tricks like if ... ( ... ) else ( ... ). The syntax inside ( ) is not necessarily what you'd expect.