【问题标题】:Renaming a folder, and merging with exisiting folder if it exists in Powershell重命名文件夹,并与现有文件夹合并(如果它存在于 Powershell 中)
【发布时间】:2020-12-23 10:16:39
【问题描述】:

尝试进行一些目录清理,并从旧方法转换为新方法。目前我们一直在逐案手动进行,但我决定查看automating the process

在我们开始提取更多 CSV 数据并遇到新问题之前,这在理论上是可行的。

我在任何地方都找不到答案是“重命名文件夹,如果名称存在,则合并两者”,就像您在 Windows 资源管理器中看到的那样:

我知道这很危险 - 但这是我能想到的唯一方法。

looked into Copy-Item 但由于文件夹的范围从几 MB 一直到 >20GB(平均在较大的一侧)。复制、移动子项目然后删除太慢了——加上大约 8000 个文件夹要创建和移动,我认为没有足够的缓冲存储空间。

是否可以将文件夹和子文件夹合并到新创建的文件夹中?

这是我目前所拥有的:

# set the working directory
Set-Location "B:\"

# import the CSV file for folder creation
$folders = Import-Csv -Delimiter "," -Header @("ID","caseName","caseNumber") -Path .\export.csv

# begin the loop
ForEach( $folder in $folders ) {

   # create the variables
   $columnID = $folder.ID
   $columnCase = "{0} {1}" -f $folder.caseName, $folder.caseNumber
   $columnNewCase = "{1}, {0}" -f $folder.caseName, $folder.caseNumber

   # use later to create root folders
   $yearAllocation = "20{0}" -f $folder.caseNumber.Substring(0,2)


   #
   # // MARK: begin main folder creation
   #

   # "SMITH 12345678" and "12345678, SMITH" do NOT exist
   if( (-not ( Test-Path "$columnCase" )) -and (-not ( Test-Path "$columnNewCase" )) ) {
       # create the "12345678, SMITH"
       New-Item "$columnNewCase" -ItemType Directory
   }

   # "SMITH 12345678" EXISTS but "12345678, SMITH" does NOT exist
   if( ( Test-Path "$columnCase" ) -and (-not ( Test-Path "$columnNewCase" )) ) {
       # rename "SMITH 12345678" -> "12345678, SMITH"
       Rename-Item "$columnCase" -NewName $columnNewCase -Force
   }

   # "SMITH 12345678" does NOT exist but "12345678, SMITH" EXISTS
#   if( (-not ( Test-Path "$columnCase" )) -and ( Test-Path "$columnNewCase" ) ) {
#      # do nothing
#   }

   # "SMITH 12345678" and "12346578, SMITH" both EXIST
   if( ( Test-Path "$columnCase" ) -and ( Test-Path "$columnNewCase" ) ) {
       # merge "SMITH 12345678" -> "12345678, SMITH"
   }


   #
   # // MARK: begin ID folder moves
   #

   # if "98765" folder exists
   if( Test-Path "$columnID" ) {
       # move "98765" into "12345678, SMITH"
       Move-Item "$columnID" -Destination "$columnNewCase"
   }
} 

最后我希望我们能得到目前看起来像这样的东西:

B:\
- 12345
- 23445
- 63574
- 73363
- SMITH 12345678
- JONES 58478945

进入:

B:\
- 12345678, SMITH
--- 12345
--- 23445
- 58478945, JONES
--- 63574
--- 73363

如果有人不小心创建了一个新的手动文件夹:

B:\
- 12345678, SMITH
--- 12345
--- 23445
- 58478945, JONES
--- 63574
--- 73363
- SMITH 12345678
--- 66684

然后重新运行脚本就很简单了:

B:\
- 12345678, SMITH
--- 12345
--- 23445
--- 66684
- 58478945, JONES
--- 63574
--- 73363

【问题讨论】:

  • 你能告诉我“合并”和“复制”有什么区别吗?对我来说也是一样的,如果你复制一些东西,它会覆盖相同的文件(源和目标),而把其他文件放在一边,合并不是做同样的事情吗?
  • Adis 是正确的。当您在 powershell 中复制时,默认情况下它将合并具有相同名称的文件夹。你可以copy-item 合并,然后remove-item 复制过来的文件。

标签: powershell csv powershell-3.0


【解决方案1】:

原来没有办法在 Powershell 中合并文件夹。

我最终通过扫描原始文件夹中的所有子文件夹并将它们移动到新目标来解决问题。

作为预防措施,然后将原始文件夹移动到另一个目的地,以便我们可以检查文件大小属性是否遗漏任何内容。

# set the working directory
Set-Location "C:\"

# import the CSV file for folder creation
# create the headers for the file since CSVs dont have them
$csvRows = Import-Csv -Delimiter "," -Header @("ID","name","number") -Path .\file.csv

# begin the loop
    ForEach( $csvRow in $csvRows ) {

    # create the variables
    $columnID = $csvRow.ID
    $columnNameNumber = "{0} {1}"  -f $csvRow.name, $csvRow.number
    $columnNumberName = "{1}, {0}" -f $csvRow.name, $csvRow.number


    #
    # // MARK: begin main folder creation
    #

    # "SMITH 12345678" and "13246578, SMITH" do NOT exist
    if( (-not ( Test-Path "$columnNameNumber" )) -and (-not ( Test-Path "$columnNumberName" )) ) {
        # create the "12345678, SMITH"
        New-Item "$columnNumberName" -ItemType Directory
    }

    # "SMITH 12345678" EXISTS but "13246578, SMITH" does NOT exist
    if( ( Test-Path "$columnNameNumber" ) -and (-not ( Test-Path "$columnNumberName" )) ) {
        # rename "SMITH 12345678" -> "12345678, SMITH"
        Rename-Item "$columnNameNumber" -NewName $columnNumberName -Force
    }

    # "SMITH 12345678" does NOT exist but "13246578, SMITH" EXISTS
    if( (-not ( Test-Path "$columnNameNumber" )) -and ( Test-Path "$columnNumberName" ) ) {
        # do nothing
    }

    # "SMITH 12345678" and "13246578, SMITH" both EXIST
    if( ( Test-Path "$columnNameNumber" ) -and ( Test-Path "$columnNumberName" ) ) {
        # find all the items in $columnNameNumber
        $subDirectory = Get-ChildItem $columnNameNumber -Name

        # loop through and move to $columnNameNumberNew
        ForEach( $childItem in $subDirectory ) {
            Move-Item "$columnNameNumber\$childItem" -Destination "$columnNumberName"
        }

        # rename to know its done
        if( ( Test-Path .\destination ) ) {
            Move-Item "$columnNameNumber" -Destination .\destination
        }
    }


    #
    # // MARK: begin ID folder moves
    #

    # if "98765" folder exists
    if( Test-Path "$columnID" ) {
        # move "98765" into "12345678, SMITH"
        Move-Item "$columnID" -Destination "$columnNumberName"
    }
}

希望这对将来的某人有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 2014-02-21
    相关资源
    最近更新 更多