我们有数千个数据包,每天都被扫描到一个临时文件夹中,它们的名称都是用数据包号命名的。例如: 301949.pdf、405311.pdf、481502.pdf等。
我们的文件夹结构在千层构建成文件夹,在一百层构建子文件夹,如下所示:
我们需要根据每个数据包的数字文件名将其移动到正确的文件夹中:
例如:
into需要进入Y:\PACKETS\301000\301900-301999
我甚至不知道如何开始这样做,但我希望这里的人能帮忙!
发布于 2020-05-05 14:00:46
我有点无聊,所以给你:
$SourceFolder = 'Y:\Temp' # the temporary folder where the pdf files are
$Destination = 'Y:\PACKETS' # just the root folder
# you may want to add '-Recurse' if the temp folder contains subfolders
Get-ChildItem -Path $SourceFolder -Filter '*.pdf' -File |
Where-Object { $_.BaseName -match '^\d{4,}$' } | # filenames must be numeric and at least 1000 or more
ForEach-Object {
$packet = [int]$_.BaseName # create a numeric value from the file's BaseName
$level1 = [Math]::Floor($packet / 1000) * 1000 # get the 'thousand' value ( 301949 --> 301000 )
$level2 = [Math]::Floor($packet / 100) * 100 # get the 'hundred' value ( 301949 --> 301900 )
# create the complete path to move the file to
$target = Join-Path -Path $Destination -ChildPath ('{0}\{1}-{2}' -f $level1, $level2, ($level2 + 99))
# test if this path exists and if not create it
if (!(Test-Path -Path $target -PathType Container)) {
$null = New-Item -Path $target -ItemType Directory
}
$_ | Move-Item -Destination $target
}希望这有帮助
https://stackoverflow.com/questions/61603320
复制相似问题