我正在尝试进行一些目录清理,并从旧方法转换为新方法。目前我们一直在手动逐个案例地处理,但我决定研究自动化该过程。
这在理论上是可行的,直到我们开始提取更多的CSV数据并遇到新问题。
我无法在任何地方找到答案的是“重命名文件夹,如果名称存在,则合并两个文件夹”,就像你在Windows资源管理器中看到的那样:
我知道这很危险,但这是我能想到的唯一方法。我研究了
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
copy-item
进行合并,然后使用remove-item
删除已复制的文件。 - AutomatedOrder