根据文件名打开文件夹中的所有文件并重新保存在创建的文件夹目录中

布莱恩·毕晓普·佩根科普夫

您好,我在 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\

想法和想法将不胜感激!先感谢您!

克莱门特0

请尝试以下操作:

# 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

}

注意:如果将-WhatIfcommon 参数添加Copy-Item命令中,您将获得复制操作预览,其中将包括完整的目标路径。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

遍历目录中的文件,根据文件名创建文件夹,并将文件移动到相应的文件夹中

在文件夹的所有子目录中创建相同的文件夹

将文件夹及其子文件夹中的所有文件名写入CSV文件的脚本

如何遍历目录中的所有子文件夹,并删除具有特定文件名的文件

从文件夹中的所有文件创建zip文件

删除文件夹中的所有文件,列表中的文件名除外

根据文件夹中是否存在来自 df 的文件名,在 df 中删除行

从给定文件夹中的所有文件名中删除所有非法字符

从具有指定文件夹名称的文件夹结构中获取文件名

创建新文件夹并重命名并将所有具有特殊名称的文件夹移到新创建的文件夹中

python将文件夹名称附加到所有子文件夹中的文件名

批处理文件,用于获取文件夹中的所有文件名

根据文件名自动创建文件夹

根据文件名创建文件夹

根据文件名 bash 创建文件夹

打开文件夹python中的所有文件

抓取目录中的所有文件夹和文件

在目录中打开文件夹

Bash脚本从文件夹中的所有文件名中删除字符

代码从文件夹中的所有文件名中删除时间戳

与所有父文件夹中创建的Java文件

遍历文件夹和子文件夹中的所有文件并获取创建日期

将文件组织到文件夹中,然后根据文件名组织子文件夹

如何根据文件名将文件名排序到文件夹中

如何遍历目录中某个文件夹中的所有csv文件名,并对这些文件名执行字符串拆分?

如何更改文件夹中的所有文件名?

查找和替换文件夹中的所有文件名-PHPStorm / WebStorm

如何使用angular js列出文件夹中的所有文件名

将文件夹中的所有文件名更改为其他Python