您好,我在 Z:\PDF Archive\2010 文件夹中保存了超过 15K 个 PDF 文件
并需要更好的组织。我不知道该怎么做,但我希望看到的是:如果文件名是 8757854.pdf
我想创建(如果它不存在)目录 Z:\PDF Archive\8700000-8799999\8750000-8759999\
并将文件复制到其中。
大多数文件名是 7 位,但有些只有 6 位,在这种情况下,我希望在文件名的开头添加一个 0。
所以如果文件名是 757854.pdf 那么这将是文件路径:Z:\PDF Archive\0700000-0799999\0750000-0759999\
想法和想法将不胜感激!先感谢您!
请尝试以下操作:
# Input and output (root) directory paths.
$inPath = 'Z:\PDF Archive\2010'
$outPathRoot = 'Z:\PDF Archive'
# The max. number of digits in the input file names.
$maxDigits = 7
Get-ChildItem -LiteralPath $inPath -Filter *.pdf | ForEach-Object {
# Left-pad the number string that constitutes the base file name
# with zeros to ensure a length of 7.
$paddedNum = $_.BaseName.PadLeft($maxDigits, '0')
# Construct the name of the subdirectories to copy the files to,
# replacing the digits after the leading 2 and 3 digits first with "0" and "9",
# respectively, joining the resulting strings with "-"
$subDirs =
foreach ($leadingDigits in $paddedNum.Substring(0, 2), $paddedNum.Substring(0, 3)) {
($leadingDigits + '0' * ($maxDigits - $leadingDigits.Length)),
($leadingDigits + '9' * ($maxDigits - $leadingDigits.Length)) -join '-'
}
# Create the output subdirectories, if necessary.
# -Force both creates parent directories on demand and
# quietly returns an existing directory, if present.
$targetDir = New-Item -Type Directory -Force ($outPathRoot + '\' + ($subDirs -join '\'))
# Copy the PDF file at hand to the target subdirectory.
$_ | Copy-Item -Destination $targetDir.FullName
}
注意:如果将-WhatIf
common 参数添加到Copy-Item
命令中,您将获得复制操作的预览,其中将包括完整的目标路径。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句