Jump to content
coryhays

Folder Cleanup Script

Recommended Posts

I am not too good at scripting but i am looking for a way to cleanup a folder that has child folders that get created in it. I would like to periodically remove all the child folders to free up the space, otherwise it fills the drive. The folders have different names but, they have a specific format for the name so i am hoping this can be done with a script that deletes any folder with that format. Any help would be appreciated.

Thank you

Share this post


Link to post
Share on other sites

Sorry, i should clarify what i am talking about. I have a powershell script that works only if i run it directly from that folder. I am trying to figure out how to convert this to a Automate script that will do the job automatically. Like i say i am not good at scripting. here is the script

Param(
  [int]$days,
  [string]$notusedyet
)
if(!$days){$days = 10}
$days = [int]$days * -1
$limit = (Get-Date).AddDays($days)
$Path = split-path -parent $MyInvocation.MyCommand.Definition

Get-ChildItem -Path $Path -Recurse -Force | Where-Object {$_.Name -Match "........-....-....-............" -and $_.CreationTime -lt $limit} | Remove-Item -Force -Recurse -Confirm:$false

 

Share this post


Link to post
Share on other sites

This line here

$Path = split-path -parent $MyInvocation.MyCommand.Definition

 

Is used to set the current location of where the powershell script is located, which means its the reason why it will work only when run out of the folder you want to clean up. You have a few options.

 

  1. Labtech can write the powershell script to that location using the Script Write File function and then execute and delete it (which is pretty messy).
  2. You can change $Path to $Path = "C:\Location\To\Folder" that way it doesn't matter where the Powershell script is, it'll always run against the folder you specify.
  3. This is really a continuation of 2, you can manually specify the $Path using a labtech variable that you set or pull from an EDF or magically find using other parameters inside the script. Good Luck 

Share this post


Link to post
Share on other sites

Like i said, i am not very good at scripting but, what would i do with the parameters  [int]$days, and  [string]$notusedyet? Would i just add the whole thing to an Execute script function? It makes sense to use a %locationdrive% variable for the path as that is where we have the location set for the folder. I guess i am a bit confused but i will give it a shot. Thnaks for the info.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...