0

I would like to write the directory names of several folders in an array. However, only all directory names with a date <today should be read. The directory names contain a date in this form * YYYYMMDD *

So I would have to do the following:

  1. Borrow the date
  2. Write the date in the form of YYYYMMDD in a variable
  3. Read out directory names and check against the variable Write data to an array ... do something ...

Can someone tell me how I can solve this with Powershell please?

Thank you

1
  • Do these directorynames start with the date?. What do you mean by Borrow the date? Is that the date of today or what? Are the folders to kist all inside the same root folder? Please edit your question and add examples. Commented Oct 26, 2020 at 10:12

2 Answers 2

2

Start by retrieving all the candidate directories, then use Where-Object to extract the date part and test that it describes a date prior to today:

# Define threshold
$Today = (Get-Date).Date

# Go through all candidate directories
$oldDirectories = Get-ChildItem .\path\to\root\folder -Directory |Where-Object {
  $dt = 0
  # Test if directory name contains 8 consecutive digits describing a valid date 
  if($_.Name -match '(\d{8})' -and [datetime]::TryParseExact($Matches[1], 'yyyyMMdd', , $null, 'None', [ref]$dt)){
      # We only want the ones with a date prior to today
      $dt.Date -lt $today
  }
  else{
      # Not containing a properly formatted date, we're not interested
      $false
  }
}

# Now we can extract the names
$oldDirectoryNames = @($oldDirectories.Name) # or @($oldDirectories |Select -Expand Name)
Sign up to request clarification or add additional context in comments.

4 Comments

I can not do anything with this script. It does nocht work.
@Diamond It will not output anything, but $oldDirectoryNames will contain your array of directory names afterwards
Yes, thats korrekt yes!
@Diamond If my answer solved your problem, please consider marking it "Accepted" by clicking the checkmark on the left :)
0

Do these directorynames start with the date? yes What do you mean by Borrow the date? Is that the date of today or what? Determine the Date, Yes of today.

I read out the date accordingly and wrote it in a variable:

$Timestamp = ([datetime]::now).tostring("yyyyMMdd")

Now I want to read out all directory names which have got a Date < 1 Day and would like to process it in a foreach for further processing

Understandable?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.