How to limit files found by Get-ChildItem (or limiting recursion depth)?

Background

There is a directory that is automatically populated with MSI files during the day. I plan to use the Task Scheduler to run the script shown below every 15 minutes. The script will search the directory and copy all the new MSIs created in the last 15 minutes to the network share.

In this folder C:\ProgramData\flx\Output\<APP-NAME>\_<TIME_STAMP>\<APP-NAME>\ there are two other folders: Repackaged and MSI Package . The Repackaged folder Repackaged not need to be Repackaged because it does not contain MSI. I also found that it needs to be excluded in some way to prevent this error:

 Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters. At line:14 char:32 +$listofFiles=(Get-ChildItem <<<< -Recurse -Path $outputPath -Include "*.msi" -Exclude "*.Context.msi" | where {$_.LastAccessTime -gt $time.AddMinutes($minutes)}) + CategoryInfo : ReadError: C:\ProgramData\...xcellence\Leg 1:String) [Get-ChildItem], PathTooLongException + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand 

Limitations

  • I am stuck using Powershell v1.0
  • I have no control over the directory structure in the source location

Updated:

  • I do not know the name of the application or what will be the timestamp. This is something that I can not do.

Current plans

I read about using -Filter , and I know about filters similar to functions, but I could not figure out how to use them. My only thought at the moment would be to do something like:

 $searchList=Get-ChildItem "all instances of the MSI Package folder" foreach($folder in $searchList){ $listofFiles=Get-ChildItem "search for *.msi" foreach($file in $listofFiles){"Logic to copy MSI from source to destination"} } 

However ... I thought there might be a more efficient way to do this.

Questions

  • How to limit the search depth of Get-ChildItem?
  • How can I limit the search for Get-ChildItem to C:\ProgramData\flx\Output\<APP-NAME>_<TIME_STAMP>\<APP-NAME>\MSI Package
  • How can I only search for folders that you accessed in the last 15 minutes? I do not want to waste time drilling into folders when I know that MSI is already copied.

Additional recommendations on how to make this script more efficient in general will also be greatly appreciated.

Script

My current script can be found here . I continued to receive: "Your message seems to contain code that was not correctly formatted as code," and after the fourth attempt I refused to reformat it.

+4
source share
3 answers

With CB, this is my new search that fixes the problems that I had.

  • Changed -Path to C:\ProgramData\flx\Output\*\*\*\* to limit the depth of the search.
  • Use -Filter instead of -Include and put the -Exclude logic in the where clause.

Get-ChildItem -Path C:\ProgramData\flx\Output\*\*\*\* -Filter "*.msi" | where {$_.Name -notlike "*.Context.msi" -and $_.LastAccessTime -gt (Get-Date).AddMinutes(-15)}

+1
source

You can try this

 dir C:\ProgramData\flx\Output\*\*\*\*\* -filter *.msi 

Search all .msi files at this level

 C:\ProgramData\flx\Output\<APP-NAME>\_<TIME_STAMP>\<APP-NAME>\Repackaged or 'MSI Package' or whatever else present folder 

without recursion, avoid a folder too deep that gives you an error.

Produce the result:

 Where {$_.LastAccessTime -gt (Get-Date).AddMinutes(-15)} #be sure no action on file is taken before the dir command 

or

 Where {$_.LastWriteTime -gt (Get-Date).AddMinutes(-15)} #some file can be re-copied maybe 
+1
source

You cannot limit the recursion depth for Get-ChildItem, except that not using -Recurse ie Get-ChildItem is either depth = 0 or N.

Set variables for application name and timestamp, for example:

 $appName = "foo" $timestamp = Get-date -Format HHmmss Get-ChildItem "C:\ProgramData\flx\Output\${appName}_$timestamp\$appName\MSI Package" -force -r 

You can filter the results as follows:

 Get-ChildItem <path> -R | Where {$_.LastWriteTime -gt (Get-Date).AddMinutes(-15)} 
0
source

All Articles