【发布时间】:2019-06-02 14:09:55
【问题描述】:
我正在尝试自动重命名某些文件,但还需要文件更新它的“上次修改”时间,因为我在 Word 文档中插入了一个字段,该字段会动态更新上次编辑文件的时间。
copy C:\path\to\file\test\test.docx "C:\path\to\file\test2\test-%date:~-7,2%-%date:~-10,2%-%date:~-4,4% %time:~-11,2%%time:~-8,2%.docx"
我尝试集成以下语法:
copy /b filename.ext +,,
我从: https://superuser.com/questions/10426/windows-equivalent-of-the-linux-command-touch/764716
但是,当我将 + 放在源文件之后时,它没有输出任何内容。
copy /b "C:\path\to\file\test\test.docx" + "C:\path\to\file\test2\test-
%date:~-7,2%-%date:~-10,2%-%date:~-4,4% %time:~-11,2%%time:~-8,2%.docx"
我还尝试在批处理文件中调用 PowerShell 脚本来更新上次修改日期:
$file = Get-Item C:\Path\TO\test.docx
$file.LastWriteTime = (Get-Date)
copy C:\path\to\file\test\test.docx "C:\path\to\file\test2\test-
%date:~-7,2%-%date:~-10,2%-%date:~-4,4% %time:~-11,2%%time:~-8,2%.docx"
powershell -file C:\path\to\powershell.ps1
无论哪种方式都无法让它工作,我是新手,所以可能缺少一些简单的东西。
【问题讨论】:
-
单行PowerShell命令
'C:\path\to\file\test\test.docx'|gi|copy-item -dest {'{0}\{1}-{2:MMddyyyy\ HHmm}{3}' -f $_.Directory,$_.Basename,$_.LastWriteTime,$_.Extension} -WhatIf可以打包成一个批处理。
标签: windows powershell command-line