首页 > 解决方案 > 从文件夹名称更改文件日期的脚本

问题描述

我需要帮助,希望这是它的地方。或者,如果您能推荐我应该在哪里寻求帮助,我将不胜感激。

障碍:我有很多文件需要将日期更改为它们所在文件夹的名称。我想要一个可以循环遍历每个文件夹并自动执行此操作的脚本。让我知道可以做些什么,并对建议持开放态度。

标签: powershellbatch-file

解决方案


一种解决方案:

function Convert-DateString ([String]$Date, [String[]]$Format)
{
   $result = New-Object DateTime
 
   $convertible = [DateTime]::TryParseExact(
      $Date,
      $Format,
      [System.Globalization.CultureInfo]::InvariantCulture,
      [System.Globalization.DateTimeStyles]::None,
      [ref]$result)
 
   if ($convertible) { $result }
}

#take all dir with correct format
Get-ChildItem "C:\temp\tmp2" -directory | where Name -like "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]" | %{

$CurrentDir=$_

#try to convert name directoy to date
$NewDate=Convert-DateString -Date $CurrentDir.Name -Format "yyyy-MM-dd"

#if date is correct
if($NewDate)
{
    #modify all creation date file with new date
    Get-ChildItem $_.FullName -file | %{$_.CreationTime=$NewDate}
} 

}

推荐阅读