Forum Discussion

PoorMens_Bravo's avatar
PoorMens_Bravo
Brass Contributor
Aug 01, 2021
Solved

Create a task scheduler that triggers a task if a csv file is created

Hi All,

Many thanks in advance, I have a powershell script that runs based on an input csv file. I have to run the script once a csv file is created, kindly help me by letting me know hos to trigger a task based on csv file creation.

  • PoorMens_Bravo Personally, I would add to the PowerShell code a check for the existence of the CSV file(s) and progress only if it exists, otherwise quit. You would then also need to set the task to run more frequently. 

     

    if (Test-Path -Path C:\SomeFileName.csv -PathType Leaf){"found, do something"}Else{"quit"}

     

    Apologies if I misunderstood requirements. 

  • Elemer_mh's avatar
    Elemer_mh
    Copper Contributor
    Like CLCCW said,
    You could also put your whole code in an endless loop
    While($true){
    if (Test-Path -Path C:\SomeFileName.csv -PathType Leaf){
    "found, do something"
    }
    Else{
    "quit"
    }
    Sleep 5
    }

    With the above it checks every 5 seconds if a file exists and if it does it does whatever you want otherwise sleeps 5 seconds again.
    Not the most elegant way, but it would work.
    I am also looking into a solution where a script is triggered if a file is created in a folder.
    Something a bit more reliable , like your scheduled task.
    Or you and me could set up a scheduled task to run every hour to run this endless script with the option to not run if the task is already running.
  • CLCCW's avatar
    CLCCW
    Copper Contributor

    PoorMens_Bravo Personally, I would add to the PowerShell code a check for the existence of the CSV file(s) and progress only if it exists, otherwise quit. You would then also need to set the task to run more frequently. 

     

    if (Test-Path -Path C:\SomeFileName.csv -PathType Leaf){"found, do something"}Else{"quit"}

     

    Apologies if I misunderstood requirements. 

    • PoorMens_Bravo's avatar
      PoorMens_Bravo
      Brass Contributor
      Hi CLCCW,
      Thanks for the suggestion, I thought about this idea initially, but, was worried about the after effects, like what about the load on the server of these repeated tasks??

Resources