批量重命名文件,具体取决于它们所在的子文件夹

时间:2017-10-16 14:04:40

标签: powershell powershell-v3.0 file-rename bulk batch-rename

我试图在一堆子文件夹中重命名一堆文件。 我希望他们有一个'前缀',它与他们所在的子文件夹相对应。

示例:

  • 主文件夹:
    • Subfolder1
    • Subfolder2
    • Subfolder3

Subfolder1中的所有文件都应重命名为" Subfolder1_filename.pdf",其他文件夹也是如此。

$dir = 'C:\Users\user\desktop\testfolder\'

foreach ($i in $dir){
    $foldername = Get-ChildItem $dir -name
    cd $newdir
    rename-item -NewName  {$foldername + "_" + $_.Name}
}

1 个答案:

答案 0 :(得分:0)

library(arules)
data("Groceries")
rules = apriori(Groceries,
                parameter = list(supp = 0.01, conf = 0.1, target = "rules"),
                appearance = list(lhs=c("whole milk", "root vegetables"), default="rhs"))

rules = sort(rules, by=c("confidence", "lift", "support"))
rhs.unique = unique(rules@rhs@itemInfo$labels[rules@rhs@data@i+1]) #Already sorted by top items.

#Function that returns the top rule for a particular RHS item in a set of rules.
top_item_rule = function(item, rules=NULL) {
  rules = subset(rules, rhs %in% item)
  rules = sort(rules, by=c("confidence", "lift", "support"))
  head(rules, n=1)
}

n = 3
toprules = lapply(rhs.unique[1:n], top_item_rule, rules)
toprules = do.call(c, args=toprules)

这将完成根据容器重命名它们,但是如果你想移动+重命名,或者那种性质的话,你需要利用$Files = Get-ChildItem -Path $Dir -Recurse -File ForEach ($File in $Files) { $FolderName = ((Split-Path -Path $File.FullName -Parent) -split '\\')[-1] $FileName = $File.Name Rename-Item -Path $File.FullName -NewName "${FolderName}_$FileName" }