如果文件名包含文件夹名称,PowerShell将文件复制到文件夹

时间:2019-05-19 20:45:55

标签: powershell

我尝试运行以下代码,因此将一些银行文档分类到相应的文件夹中。如果文件名包含同一目录中的文件夹名称,则应将文件移至匹配的文件夹。如果所有文件夹名都不是文件名的一部分,则应将相应文件移至文件夹<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="android.support.design.widget.BottomSheetBehavior"> <android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_anchor="@id/myBottomLL" app:layout_anchorGravity="top|center_horizontal" /> <LinearLayout android:id="@+id/myBottomLL" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:src="@drawable/tarot" /> </LinearLayout> </android.support.design.widget.CoordinatorLayout>

Other

但是,我一直在运行else循环。我不知道为什么。非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

不清楚您要查找的部分的外观以及文件夹的命名方式。

之前的样本树:

list(tmp.state_dict().keys())[-1].split('.')[0]

运行此脚本

> tree /F
│   bar Wertpapierertrag 2016-04-08.pdf
│   baz Kontoauszug 2018-01-30.pdf
│   blah Depotauszug 2017-10-15.pdf
│   foo other 2019-05-19.pdf
│   foo Wertpapierabrechnung 2017-10-15.pdf
│   test Kapitalmaßnahmen 2016-04-08.pdf
│
├───Depotauszug
├───Kapitalmaßnahmen
├───Kontoauszug
├───Other
├───Wertpapierabrechnung
└───Wertpapierertrag

如果输出看起来正常,请删除尾随## Q:\Test\2019\05\19\SO_56211858.ps1 $Folder = [Environment]::GetFolderPath("MyDocuments") $FileType = "*.pdf" $Files = Get-ChildItem -Path $Folder -Filter $FileType $RE = [regex]"(Kapitalmaßnahmen|Kontoauszug|Wertpapierabrechnung|Wertpapierertrag|Depotauszug)" # alternativly build the RegEx from current subfolder names (might need escaping) # $RE = [regex]((Get-ChildItem -Path $Folder -Directory -Name) -Join '|') ForEach($file in $Files){ if ($file.BaseName -Match $RE){ $file | Move-Item -Destination (Join-Path $Folder $Matches[0]) -WhatIf } else { $file | Move-Item -Destination (Join-Path $Folder "Other") -WhatIf } } 参数。

脚本运行后的树:

-WhatIf
相关问题