Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在数组Powershell中写入文件夹名_Powershell - Fatal编程技术网

在数组Powershell中写入文件夹名

在数组Powershell中写入文件夹名,powershell,Powershell,我想把几个文件夹的目录名写在一个数组中。但是,只有日期为的所有目录名首先检索所有候选目录,然后使用Where Object提取日期部分并测试它是否描述了今天之前的日期: # Define threshold $Today = (Get-Date).Date # Go through all candidate directories $oldDirectories = Get-ChildItem .\path\to\root\folder -Directory |Where-Object {

我想把几个文件夹的目录名写在一个数组中。但是,只有日期为的所有目录名首先检索所有候选目录,然后使用
Where Object
提取日期部分并测试它是否描述了今天之前的日期:

# 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)

首先检索所有候选目录,然后使用
Where Object
提取日期部分并测试它是否描述了今天之前的日期:

# 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)

这些目录名是否以日期开头? 对 你说的借日期是什么意思?那是今天的日期还是什么? 确定日期,是今天

我相应地读出日期,并将其写入变量:

$Timestamp = ([datetime]::now).tostring("yyyyMMdd")
现在我想读出所有日期小于1天的目录名,并希望在foreach中对其进行进一步处理


可以理解吗?

这些目录名是否以日期开头? 对 你说的借日期是什么意思?那是今天的日期还是什么? 确定日期,是今天

我相应地读出日期,并将其写入变量:

$Timestamp = ([datetime]::now).tostring("yyyyMMdd")
现在我想读出所有日期小于1天的目录名,并希望在foreach中对其进行进一步处理


可以理解吗?

这些目录名是否以日期开头?。你说的借日期是什么意思?那是今天的日期还是什么?kist的文件夹是否都在同一根文件夹中?请回答您的问题并添加示例。这些目录名是否以日期开头?。你说的借日期是什么意思?那是今天的日期还是什么?kist的文件夹是否都在同一根文件夹中?请回答您的问题并添加示例。我无法使用此脚本执行任何操作。它不起作用。@Diamond它不会输出任何内容,但是
$oldDirectoryNames
将包含您的目录名数组,这是肯定的@钻石如果我的答案解决了你的问题,请考虑标记为“接受”点击左边的复选标记:)我不能做任何与此脚本。它不起作用。@Diamond它不会输出任何内容,但是
$oldDirectoryNames
将包含您的目录名数组,这是肯定的@钻石如果我的答案解决了你的问题,请考虑标记它“接受”点击左边的复选标记: