Remove-Item -Path '.\index.html' -Force
Remove-Item -Path '.\generated' -Force -Recurse #folder containing generated files
#Start-Sleep -Seconds 10 #uncommenting this line fixes the issue
#$tempDir contains index.html and a sub folder, "generated", which contains additional files.
#i.e. we're replacing the content we just deleted with new versions.
Get-ChildItem -Path $tempDir | %{
Move-Item -Path $_.FullName -Destination $RelativePath -Force
}
我遇到了一个间歇性错误,在generated
路径的Move-Item
行上出现错误信息:Move-Item : Cannot create a file when that file already exists.
我通过在第二个Remove-Item
语句后添加一个hacky的Start-Sleep -Seconds 10
来解决这个问题,但这不是一个好的解决方案。
我认为问题在于Remove-Item
语句完成/代码继续执行下一行之前,操作系统还没有完成文件删除;但这似乎很奇怪/令人担忧。注意:生成的文件夹中有大约2500个文件(所有文件大小都在1-100 KB之间)。
没有其他进程访问这些文件夹(即使关闭了我的资源管理器窗口并测试了将此目录从我的 AV 中排除),也没有用。
我考虑了其他选项:
使用
Copy-Item
而不是Move-Item
。我不喜欢这个方法,因为它需要在不需要文件时创建新文件(即复制比移动慢)……这比我的当前睡眠hack要快,但仍然不理想。删除文件而不是文件夹,然后迭代子文件夹并将文件复制到新位置。这个方法可以解决问题,但对于应该简单的事情来说,需要更多的代码;所以我不想选择这个选项。
Robocopy可以解决问题;但我更喜欢一个纯PowerShell解决方案。如果没有干净的解决方案,这是我最终会选择的选项。
问题
- 有人遇到过这种情况吗?
- 这是一个错误还是我错过了什么?
- 有人知道修复/好的解决方案吗?
更新
在单独的作业中运行删除操作(即使用下面的代码)无法解决问题。
Start-Job -ScriptBlock {
Remove-Item -Path '.\index.html' -Force
Remove-Item -Path '.\generated' -Force -Recurse #folder containing generated files
} | Wait-Job | Out-Null
#$tempDir contains index.html and a sub folder, "generated", which contains additional files.
#i.e. we're replacing the content we just deleted with new versions.
Get-ChildItem -Path $tempDir | %{
Move-Item -Path $_.FullName -Destination $RelativePath -Force
}
更新 #2
添加这个可以解决问题;即不是等待固定的时间,而是等待路径被删除/每秒检查一次。如果在30秒后路径未被删除,则认为它不会被删除;因此继续进行(这将导致move-item
引发一个错误,在其他地方得到处理)。
# ... remove-item code ...
Start-Job -ScriptBlock {
param($Path)
while(Test-Path $Path){start-sleep -Seconds 1}
} -ArgumentList '.\generated' | Wait-Job -Timeout 30 | Out-Null
# ... move-item code ...
start-job ... | wait-job | out-null; move-item ...
),我就很高兴让它在单独的作业下运行...现在将进行测试以查看是否有所帮助... - JohnLBevanwait-job
上的超时选项等待最大时间,同时每秒检查文件夹并在它消失时继续执行... 还是很hacky,所以我会保留这个问题的开放性;但它确实有效。 - JohnLBevanRemove-Job * -force
,否则那些挂起的作业就会一直存在。 - Jacob Colvin