powershell - 从文件夹名称更改文件日期的脚本
问题描述
我需要帮助,希望这是它的地方。或者,如果您能推荐我应该在哪里寻求帮助,我将不胜感激。
障碍:我有很多文件需要将日期更改为它们所在文件夹的名称。我想要一个可以循环遍历每个文件夹并自动执行此操作的脚本。让我知道可以做些什么,并对建议持开放态度。
解决方案
一种解决方案:
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}
}
}
推荐阅读
- multithreading - 如何在一个线程中更新并从多个线程中读取?
- reactjs - 将 Id 属性添加到多个复选框
- php - 从 Laravel 上的表中获取数据的最快方法
- r - 如何在忽略R中的值的同时对df求和?
- django - 在 Django update-view 实例中没有预先填写表单
- postgresql - 显示在另一个表中只有一个匹配行的记录
- android - 基于 Android API 级别有条件地声明依赖项 - Gradle
- java - 单击“收藏夹”按钮时停止recyclerview尝试更新
- java - SceneBuilder 无法创建具有给定属性集的自定义组件的实例
- excel - Excel:在多个 ID 中按日期排序