User646972
User646972

Reputation: 107

CMD executes powershell command to continuously log (tail) a file but does not return back to cmd or execute next command

I am using CMD to execute PowerShell command for logging (tailing a data file "trun.out" that updates from a simulation run). > See the "Call start Poweshell" portion of the code below.

if %therm%==y (del /f trun.out
    fsutil file createnew trun.out 8192
    CALL start powershell -noninteractive -File "trun_tail_powershell.ps1"
    if %dist_solve%==1 (%ans_path% -b -dis -np %num_proc% -j %job% -run %run% -i therm.inp -o trun.out) else (%ans_path% -b -dis -np %num_proc% -f -m 30000 -db 5000 -j $job -run %run% -i therm.inp -o trun.out)   
    REM END 
    )
dir 

After the if statement is executed, a new powershell window opens (it is done purposefully so that the data logging can be seen separately). Logs the trun.out data file.

However, CMD does not execute "dir" command right after the if statement.

I first thought that CMD opening a new powershell window for data logging does not returns the authority (not sure if this is the correct word to use) back to CMD to move on to the next command, so I tried commenting the data logging section and not creating any powershell window. But I am still having the issue. It does not execute anything after the if statement.

But if I place the "dir" command inside if statement, it will show the directory.

Would appreciate any help/guidance as I am quite new using CMD and powershell altogatehr.

Upvotes: 0

Views: 86

Answers (1)

Richard
Richard

Reputation: 177

if "%therm%"=="y" (
    del /f trun.out
    fsutil file createnew trun.out 8192
    start /wait powershell -noninteractive -File "trun_tail_powershell.ps1"
    if "%dist_solve%"=="1" (
        "%ans_path%" -b -dis -np "%num_proc%" -j "%job%" -run "%run%" -i therm.inp -o trun.out
    ) else (
        "%ans_path%" -b -dis -np "%num_proc%" -f -m 30000 -db 5000 -j "%job%" -run "%run%" -i therm.inp -o trun.out
    )
    REM END 
)
dir
  • Compo is right, CALL shouldn't be in your script. it should be used when you want to call another batch file from within your script.

  • I think the issue is the start command, you want to use the start/wait command instead. The start command opens a new cmd prompt window without waiting for the window to close but with start/wait, the new window closes before the next command executes

  • Also i noticed inside your else clause, you have $job instead of %job%.

Hopefully, this helps.

To modify the code to running ansys through cmd with the following code as discussed in the comment. %ans_path% -b -dis -np %num_proc% -j %job% -run %run% -i therm.inp -o trun.out. try this

if "%therm%"=="y" (
    del /f trun.out
    fsutil file createnew trun.out 8192
    start powershell -noninteractive -File "trun_tail_powershell.ps1"
    if "%dist_solve%"=="1" (
        cmd /c "%ans_path% -b -dis -np %num_proc% -j %job% -run %run% -i therm.inp -o trun.out"
    ) else (
        cmd /c "%ans_path% -b -dis -np %num_proc% -f -m 30000 -db 5000 -j %job% -run %run% -i therm.inp -o trun.out"
    )
    REM END 
)
dir

Upvotes: 0

Related Questions