如何在单个脚本中修改多个文件的创建时间。

huangapple go评论77阅读模式
英文:

How to modify the file creation time of multiple files in single script

问题

我卡在这个问题上。我正在进行从文件共享迁移到SharePoint的迁移工作。出现了错误,提示“不支持项目创建时间或修改时间”。没问题,我找到了一个PowerShell脚本来编辑这个:

cd "目录"

Get-ChildItem -force | Select-Object Mode, Name, CreationTime, LastAccessTime, LastWriteTime | ft

$modifyfiles = Get-ChildItem -force | Where-Object {! $_.PSIsContainer}
foreach($object in $modifyfiles)
{
$object.CreationTime=("1/3/2023 12:00:00")
$object.LastAccessTime=("1/3/2023 12:01:00")
$object.LastWritetime=("1/3/2023 12:02:00")
}

我的问题是,如何运行这个脚本,以便每次不必cd到每个新目录。我有很多文件位于不同的文件夹中,都需要编辑。我有需要更改的路径列表,希望有一种方法可以“传递”这些路径或以某种方式在循环中运行此脚本。

英文:

I'm stumped on this. I am making my way through a file share migration to SharePoint. There have been errors stating the "The item created time or modified time is not supported". No worries as I found a script to edit this in PowerShell:

cd "Directory"

Get-ChildItem -force | Select-Object Mode, Name, CreationTime, LastAccessTime, LastWriteTime | ft

$modifyfiles = Get-ChildItem -force | Where-Object {! $\_.PSIsContainer}
foreach($object in $modifyfiles)
{
$object.CreationTime=("1/3/2023 12:00:00")
$object.LastAccessTime=("1/3/2023 12:01:00")
$object.LastWritetime=("1/3/2023 12:02:00")
}

My question is how do I run this so I don't have to cd to each new directory every time. I have quite a few files in different folders that all need editing. I have the list of paths I need changed and I was hoping there would be a way to "pass" those paths in or somehow run this script in a loop.

答案1

得分: 1

假设您的文件夹列表如下,并可以放在单独的文本文件中:

C:\folderpath\folder1
C:\folderpath\folder2
C:\folderpath\folder3

然后您可以像这样操作:

get-content -Path "C:\folderpath\FileContainingFolderPaths.txt" | ForEach-Object {
    
    $folderpath = $_

    Get-ChildItem -Path $folderpath -force | Select-Object Mode, Name, CreationTime, LastAccessTime, LastWriteTime | ft

    $modifyfiles = Get-ChildItem -Path $folderpath -force | Where-Object {! $_.PSIsContainer}
    foreach($object in $modifyfiles)
    {
        $object.CreationTime=("1/3/2023 12:00:00")
        $object.LastAccessTime=("1/3/2023 12:01:00")
        $object.LastWritetime=("1/3/2023 12:02:00")
    }

}

另外,我需要质疑一下$\\_.PSIsContainer,这是一个错误吗?我认为应该是$_.PSIsContainer才对。

英文:

Assuming your list of folders looks like this and can be placed in a seperate text file:

C:\folderpath\folder1
C:\folderpath\folder2
C:\folderpath\folder3

Then you could just do something like this:

get-content -Path "C:\folderpath\FileContainingFolderPaths.txt" | ForEach-Object {
    
    $folderpath = $_

    Get-ChildItem -Path $folderpath -force | Select-Object Mode, Name, CreationTime, LastAccessTime, LastWriteTime | ft

    $modifyfiles = Get-ChildItem -Path $folderpath -force | Where-Object {! $\_.PSIsContainer}
    foreach($object in $modifyfiles)
    {
        $object.CreationTime=("1/3/2023 12:00:00")
        $object.LastAccessTime=("1/3/2023 12:01:00")
        $object.LastWritetime=("1/3/2023 12:02:00")
    }

}

Also I'm gonna have to question you on the $\_.PSIsContainer, is that a mistake? I would think it should be $_.PSIsContainer instead?

huangapple
  • 本文由 发表于 2023年1月6日 12:53:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定