locked
batch script to delete a file after the above process completed. RRS feed

  • Question

  • Hi 

    I have a batch script run.bat running with the following command

    start /wait c:\*\*\*\doscan.exe c:\scanfolder

    how do i delete the item in scanfolder after this command end?

    delete c:\scanfolder  doesn't seems to work and i want to ensure the process completed then run the delete.

    Please advise.

    Thursday, February 2, 2017 2:14 AM

Answers

  • Does this "doscan.exe" program return any kind of output when it's finished?  If so, you could do this:   This will start the program and wait until the process is finished providing output to the shell.

    start-job -ScriptBlock {C:\folder1\folder2\folder3\doscan.exe}
    
    get-job | wait-job | receive-job
    
    remove-item C:\scanfolder -force
    

    If "doscan.exe" doesn't actually return any output, but just runs in the background, then you could do this:

    start-process -filepath C:\f1\f2\f3\doscan.exe
    
    while ((get-process -Name doscan -ErrorAction SilentlyContinue) -ne $NULL)
    {
        write-host "Still running..." 
        start-sleep -Seconds 2
    }
    write-host "DONE"
    remove-item C:\scanfolder -force
    It isn't super elegant, but it will check for the doscan.exe process every two seconds, and when the process finishes running and kills itself, it'll delete that folder.



    • Proposed as answer by SYN_ACK_87 Thursday, February 2, 2017 4:19 PM
    • Edited by SYN_ACK_87 Thursday, February 2, 2017 4:20 PM added explanatory text to first script
    • Marked as answer by Noobpie Friday, February 3, 2017 12:57 AM
    Thursday, February 2, 2017 4:19 PM

All replies

  • Some processes cannot be waited on.  They just trigger another process.

    If the program actually allows wait then you can delete the file on the next line.


    \_(ツ)_/

    Thursday, February 2, 2017 4:30 AM
  • try to use star-process command in powershell and use some looping logic to achieve this result.
    Thursday, February 2, 2017 5:17 AM
  • try to use star-process command in powershell and use some looping logic to achieve this result.

    No different from start /wait


    \_(ツ)_/

    Thursday, February 2, 2017 5:19 AM
  • Does this "doscan.exe" program return any kind of output when it's finished?  If so, you could do this:   This will start the program and wait until the process is finished providing output to the shell.

    start-job -ScriptBlock {C:\folder1\folder2\folder3\doscan.exe}
    
    get-job | wait-job | receive-job
    
    remove-item C:\scanfolder -force
    

    If "doscan.exe" doesn't actually return any output, but just runs in the background, then you could do this:

    start-process -filepath C:\f1\f2\f3\doscan.exe
    
    while ((get-process -Name doscan -ErrorAction SilentlyContinue) -ne $NULL)
    {
        write-host "Still running..." 
        start-sleep -Seconds 2
    }
    write-host "DONE"
    remove-item C:\scanfolder -force
    It isn't super elegant, but it will check for the doscan.exe process every two seconds, and when the process finishes running and kills itself, it'll delete that folder.



    • Proposed as answer by SYN_ACK_87 Thursday, February 2, 2017 4:19 PM
    • Edited by SYN_ACK_87 Thursday, February 2, 2017 4:20 PM added explanatory text to first script
    • Marked as answer by Noobpie Friday, February 3, 2017 12:57 AM
    Thursday, February 2, 2017 4:19 PM