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

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

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

问题

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

  1. cd "目录"
  2. Get-ChildItem -force | Select-Object Mode, Name, CreationTime, LastAccessTime, LastWriteTime | ft
  3. $modifyfiles = Get-ChildItem -force | Where-Object {! $_.PSIsContainer}
  4. foreach($object in $modifyfiles)
  5. {
  6. $object.CreationTime=("1/3/2023 12:00:00")
  7. $object.LastAccessTime=("1/3/2023 12:01:00")
  8. $object.LastWritetime=("1/3/2023 12:02:00")
  9. }

我的问题是,如何运行这个脚本,以便每次不必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:

  1. cd "Directory"
  2. Get-ChildItem -force | Select-Object Mode, Name, CreationTime, LastAccessTime, LastWriteTime | ft
  3. $modifyfiles = Get-ChildItem -force | Where-Object {! $\_.PSIsContainer}
  4. foreach($object in $modifyfiles)
  5. {
  6. $object.CreationTime=("1/3/2023 12:00:00")
  7. $object.LastAccessTime=("1/3/2023 12:01:00")
  8. $object.LastWritetime=("1/3/2023 12:02:00")
  9. }

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

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

  1. C:\folderpath\folder1
  2. C:\folderpath\folder2
  3. C:\folderpath\folder3

然后您可以像这样操作:

  1. get-content -Path "C:\folderpath\FileContainingFolderPaths.txt" | ForEach-Object {
  2. $folderpath = $_
  3. Get-ChildItem -Path $folderpath -force | Select-Object Mode, Name, CreationTime, LastAccessTime, LastWriteTime | ft
  4. $modifyfiles = Get-ChildItem -Path $folderpath -force | Where-Object {! $_.PSIsContainer}
  5. foreach($object in $modifyfiles)
  6. {
  7. $object.CreationTime=("1/3/2023 12:00:00")
  8. $object.LastAccessTime=("1/3/2023 12:01:00")
  9. $object.LastWritetime=("1/3/2023 12:02:00")
  10. }
  11. }

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

英文:

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

  1. C:\folderpath\folder1
  2. C:\folderpath\folder2
  3. C:\folderpath\folder3

Then you could just do something like this:

  1. get-content -Path "C:\folderpath\FileContainingFolderPaths.txt" | ForEach-Object {
  2. $folderpath = $_
  3. Get-ChildItem -Path $folderpath -force | Select-Object Mode, Name, CreationTime, LastAccessTime, LastWriteTime | ft
  4. $modifyfiles = Get-ChildItem -Path $folderpath -force | Where-Object {! $\_.PSIsContainer}
  5. foreach($object in $modifyfiles)
  6. {
  7. $object.CreationTime=("1/3/2023 12:00:00")
  8. $object.LastAccessTime=("1/3/2023 12:01:00")
  9. $object.LastWritetime=("1/3/2023 12:02:00")
  10. }
  11. }

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:

确定