File Check Loop Help

New and partially inept does not describe my knowledge of PowerShell. But I’ve been working for 2 weeks on an AutoCAD dwg processor. I have it working but I could not figure out how to loop knowing when a dwg has been completed and closed to start on the next dwg.

My workaround was to use a 25 second timer. Which if the dwg is complete in 10 seconds, PS waits 25 seconds before processing the next dwg.

I thought I had a good idea to look for a .txt file with the open files name to trigger PS to close and jump to the next week. So I’m creating the .txt file but still have no idea to loop looking for it.

Below is my code. If anyone can help with the looping to find the .txt file then kill the process, it would be greatly appreciated.

# Launch AutoCAD
try {
    $acadProcess = Start-Process -FilePath $AutoCADPath `
        -ArgumentList "/i `"$filePath`" /nologo /b  `"$ScriptFile`"" `
        -PassThru 
		#-NoNewWindow

    # Wait with timeout
    if ($acadProcess.WaitForExit($timeoutMs)) {
        $exitCode = $acadProcess.ExitCode
    }
    else {
        # Timeout reached – force close AutoCAD
        $acadProcess.Kill()
        $exitCode = -1
    }

    # End time & duration
    $endTime = Get-Date
    $duration = $endTime - $startTime

    # Check for .txt completion file
    $txtExists = Test-Path $txtFilePath
    $txtStatus = if ($txtExists) { "Yes" } else { "No" }
    if ($txtExists) { $txtFoundCount++ }

Hi, welcome to the forum :wave:

Something like this:

while (-not (Test-Path $txtFilePath)) {
    Start-Sleep -Seconds 3
}

Stop-Process -InputObject $acadProcess

The while loop will block the script until the Test-Path cmdlet returns true.

You will likely need additional checks. If the text file is never created, perhaps because the AutoCAD process hangs so neither exits or creates the file, the script will never continue.

1 Like