Windows 脚本结束后删除文件夹
我目前正在编写一个脚本,它接收一个文件夹中的文件,将第一个文件移动到一个具有特定名称的文件夹中,然后将其余文件移动到另一个具有名称编号的文件夹中 我的脚本工作,但它也会移动文件夹并重命名它。代码的哪一部分导致了这种情况Windows 脚本结束后删除文件夹,windows,powershell,Windows,Powershell,我目前正在编写一个脚本,它接收一个文件夹中的文件,将第一个文件移动到一个具有特定名称的文件夹中,然后将其余文件移动到另一个具有名称编号的文件夹中 我的脚本工作,但它也会移动文件夹并重命名它。代码的哪一部分导致了这种情况 $path = "C:\Users\User1\Desktop\MergeTest\_First\" $FileCount = Get-ChildItem -Path $path -File | Measure-Object | %{$_.Count} $FirstFile =
$path = "C:\Users\User1\Desktop\MergeTest\_First\"
$FileCount = Get-ChildItem -Path $path -File | Measure-Object | %{$_.Count}
$FirstFile = Get-ChildItem -Path $path -Force -File | Select-Object -First 1
$FinalReport = "C:\Users\User1\Desktop\MergeTest\___Final\TestOutput.xlsx"
Move-Item "C:\Users\User1\Desktop\MergeTest\_First\$FirstFile" $FinalReport
$Counter = 0;
Write-host $FileCount
for($Counter = 0; $Counter -lt $FileCount; $Counter++)
{
$FileInWork = Get-ChildItem -Path $path -Force -File | Select-Object -First 1
move-item "C:\Users\User1\Desktop\MergeTest\_First\$FileInWork" "C:\Users\User1\Desktop\MergeTest\__Second\$Counter.xlsx"
Write-host "File Moved"
}
你所能做的就是在移动项目命令中指定
-Include*.txt
条件,这样只需移动.txt、.log或你正在移动的任何文件类型,并保持文件夹的状态。我相信你的代码可以做一些清理。现在,您执行了3次Get ChildItem
,其中使用一次就足够了
此外,您应该尝试使用,而不是自己构造路径和文件名。特别是当您执行
“C:\Users\User1\Desktop\MergeTest\\u First\$FileInWork”
时,您应该意识到Get ChildItem
返回FileInfo和/或DirectoryInfo对象;不是字符串
无论如何,下面的代码应该满足您的要求:
# define the path where all other paths are in
$rootPath = "C:\Users\User1\Desktop\MergeTest"
# create the working paths using the common root folder path
$filesPath = Join-Path -Path $rootPath -ChildPath '_First'
$firstDestination = Join-Path -Path $rootPath -ChildPath '___Final'
$secondDestination = Join-Path -Path $rootPath -ChildPath '__Second'
# test if the destination folders exist and if not create them
if (!(Test-Path -Path $firstDestination -PathType Container)) {
Write-Host "Creating folder '$firstDestination'"
$null = New-Item -Path $firstDestination -ItemType Directory
}
if (!(Test-Path -Path $secondDestination -PathType Container)) {
Write-Host "Creating folder '$secondDestination'"
$null = New-Item -Path $secondDestination -ItemType Directory
}
# get an array of all FileInfo objects in $filesPath
# you could consider adding -Filter '*.xlsx' here..
$allFiles = Get-ChildItem -Path $filesPath -Force -File
Write-Host 'Total number of files found: {0}' -f $allFiles.Count
# move the files
for ($i = 0; $i -lt $allFiles.Count; $i++) {
if ($i -eq 0) {
# the first file should go in the $firstDestination folder with specified name
$target = Join-Path -Path $firstDestination -ChildPath 'TestOutput.xlsx'
}
else {
# all other files go to the $secondDestination folder
# each file should have the index number as name
$target = Join-Path -Path $secondDestination -ChildPath ('{0}.xlsx' -f ($i + 1))
}
$allFiles[$i] | Move-Item -Destination $target -Force -WhatIf
}
希望有帮助
如果您对console上显示的输出感到满意,请删除-WhatIf
另外,我真的认为您应该编辑您的问题并更改其标题,因为问题中的任何内容都与脚本结束后删除文件夹无关。您自己尝试了什么?我解决了问题,但我记不起确切的解决方案!