# Run script when a file exists

**URL:** <https://community.simple-help.com/t/run-script-when-a-file-exists/1005>\
**Category:** Alerts and Toolboxes\
**Created:** [March 16, 2022, 2:34pm UTC](https://community.simple-help.com/t/run-script-when-a-file-exists/1005 "2022-03-16T14:34:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rich\_Peters](https://community.simple-help.com/letter_avatar_proxy/v4/letter/r/a3d4f5/32.png) [@Rich\_Peters](https://community.simple-help.com/u/Rich_Peters)\
**Post date:** [March 16, 2022, 2:34pm UTC](https://community.simple-help.com/t/run-script-when-a-file-exists/1005/1 "2022-03-16T14:34:58Z")

</div>

Hi All,

I’m trying to run a script based on whether a file exists or not. I can target it at a specific folder but wondered if it’s possible to search subfolders in the threshold. The box says file name or search string but I’m not sure what to put in to search for a file

![image](https://community.simple-help.com/uploads/default/original/1X/111d607ddd8f9e841b53b63cb7a1b08243bb9a8a.png)

The idea is to automate the moving of a folder and subfolders to another location. The initial idea is to create a specifically named text file to trigger the script. The script then closes any open files and moves the folder.

But there are lots of folders inside the root directory that the text file could be in. Might not be possible but thought I’d ask. Or someone might have a much better idea 🙂

Thanks  
Rich

---

<div class="post-metadata">

**Author:** ![Darrell\_Swafford](https://community.simple-help.com/user_avatar/community.simple-help.com/darrell_swafford/32/82_2.png) [@Darrell\_Swafford](https://community.simple-help.com/u/Darrell_Swafford)\
**Post date:** [March 18, 2022, 4:37pm UTC](https://community.simple-help.com/t/run-script-when-a-file-exists/1005/2 "2022-03-18T16:37:36Z")

</div>

The file acceabiltiy tool is used to check if a specific file exists, can be written to and opened. It doesnt work they way you want it too. At least not yet.( Submit a feature request to check subfolders )

But, you could do this with a custom tool.  
Make a tool in your tool box to check for the file via powershell, then use that in your alert as Custom trigger. [https://simple-help.com/management-guide#Custom-(Scheduled-Tools)](https://simple-help.com/management-guide#Custom-(Scheduled-Tools))

Guide for using powershell if file exists: [https://www.itechguides.com/powershell-check-if-file-exists/](https://www.itechguides.com/powershell-check-if-file-exists/)

Example script for tool:  
$Return = Get-ChildItem E:\reports | Where-Object { ($_.PSIsContainer -ne $true) -and ($_.Extension -eq “.log”)}

---

<div class="post-metadata">

**Author:** ![Rich\_Peters](https://community.simple-help.com/letter_avatar_proxy/v4/letter/r/a3d4f5/32.png) [@Rich\_Peters](https://community.simple-help.com/u/Rich_Peters)\
**Post date:** [March 21, 2022, 2:08pm UTC](https://community.simple-help.com/t/run-script-when-a-file-exists/1005/3 "2022-03-21T14:08:02Z")

</div>

Thanks, that’s better idea than mine 😀

I’ll have a look at the Powershell and attempt to put it all in one script and see if it works!

---

<div class="post-metadata">

**Author:** ![Darrell\_Swafford](https://community.simple-help.com/user_avatar/community.simple-help.com/darrell_swafford/32/82_2.png) [@Darrell\_Swafford](https://community.simple-help.com/u/Darrell_Swafford)\
**Post date:** [March 21, 2022, 4:10pm UTC](https://community.simple-help.com/t/run-script-when-a-file-exists/1005/4 "2022-03-21T16:10:15Z")

</div>

Once you get it working be sure to post your findings and a finished example. I am sure others will appreciate it. 🙂
