问题描述
我正在尝试将文件夹结构中的所有文件复制到也是该结构一部分的文件夹中。因此,目标文件夹从搜索中排除。我还想排除路径“ .thumbnails”中包含的所有文件夹,但是当我用通配符路径(例如“ D:\ ZZZ_Phone_test * .thumbnails”)将$ Skip中的完整路径替换时,它将无法正常工作。 / p>
其次,我想尽可能提高效率,以便更快地完成工作。当脚本运行时,主要是cpu工作而不是硬盘驱动器。
第三,有什么方法可以生成一些复制,跳过,错误...的输出并将其保存到日志文件中吗?
$Source = 'D:\ZZZ_Phone_test'
$Dest = 'D:\ZZZ_Phone_test\1\1\BackUp'
$Skip = 'D:\ZZZ_Phone_test\4\.thumbnails'
Get-ChildItem $Source -Directory -Recurse | ? FullName -ne $Dest | ? FullName -ne $Skip | get-ChildItem -File | copy-Item -Exclude `
*.0,*.1,*.nomedia,*.thumbnail,*.chck,*.crypt12,*.tmp,*.db,*.crypt1,*.ini,*.pdrproj,*.pkpass,*.dat,*.enc,*.lck,*.xml,*.json,*.LOCK,*.443,*.preference `
-Destination $Dest
。
编辑:,以下操作有效,但它仅排除名称以“缩略图”或“备份”结尾的目录中的文件。如果“ thumbnails”文件夹中有文件目录,则将全部处理它们。我想定义要排除的文件夹,即使在$ Skip中定义的目录中包含文件的子目录也不会被处理。
$Source = 'D:\ZZZ_Phone_test'
$Dest = 'D:\ZZZ_Phone_test\1\1\BackUp'
$Skip = '*thumbnails','*BackUp'
(Get-ChildItem -Path $Source -Directory -Recurse -Exclude $Skip).FullName |
get-ChildItem -File |
copy-Item -WhatIf -Exclude `
*.0,*.preference `
-Destination $Dest
解决方法
尝试此操作并根据需要修改该文件排除部分...
$Source = 'D:\Temp'
$Dest = 'D:\Destination'
$Skip = '*est','*here'
<#
Always build you code one use case at a time to ensure you are getting what
you'd expect before moving ot the next.
#>
# Get all directories off a give path
(Get-ChildItem -Path $Source -Directory -Recurse).FullName |
Select-Object -First 5
# Results
<#
D:\Temp\AddressFiles
D:\Temp\ChildFolder
D:\Temp\est
D:\Temp\here
D:\Temp\hold
#>
# Exclude named directories
(Get-ChildItem -Path $Source -Directory -Recurse -Exclude $Skip).FullName |
Select-Object -First 5
# Results
<#
D:\Temp\AddressFiles
D:\Temp\ChildFolder
D:\Temp\ChildFolder\New folder
D:\Temp\ChildFolder\temp
D:\Temp\hold
#>
# Or include only what you want
(Get-ChildItem -Path $Source -Directory -Recurse -Include $Skip).FullName |
Select-Object -First 5
# Results
<#
D:\Temp\ChildFolder\New folder\est
D:\Temp\est
D:\Temp\here
#>
# Loop directories and process files,trap errors
(Get-ChildItem -Path $Source -Directory -Recurse -Exclude $Skip).FullName |
Select-Object -First 5 |
ForEach {
Try
{
"Processing $PSItem"
$CopyItemSplat = @{
Path = (Get-ChildItem -Path $PSItem -ErrorAction Stop).FullName
Destination = $Dest
Verbose = $true
WhatIf = $true
}
}
Catch
{
Write-Warning -Message 'An error was encountered.'
$PSitem.Exception.Message
}
}
# Results
<#
Processing D:\Temp\AddressFiles
Processing D:\Temp\ChildFolder
Processing D:\Temp\ChildFolder\New folder
Processing D:\Temp\ChildFolder\temp
WARNING: An error was encountered.
The property 'FullName' cannot be found on this object. Verify that the property exists.
Processing D:\Temp\hold
WARNING: An error was encountered.
The property 'FullName' cannot be found on this object. Verify that the property exists.
#>
,
对于复杂的过滤需求,通常采用除数除法。也就是说,将问题简化为多个步骤,而不是尝试编写单行代码。
让我们列出所有文件的目录,并排除目标目录class AppRepository() {
ApiService apiService;
Retrofit retrofit;
public AppRepository(ApiService apiService,Retrofit retrofit){
this.apiService = apiService;
this.retrofit = retrofit;
}
public MediatorLiveData<List<NoteEntity>> getNotes() {
MediatorLiveData<List<NoteEntity>> data = new MediatorLiveData<List<NoteEntity>>()
apiService.getNotes()
.enqueue(new Callback<List<NoteEntity>> {
@Override
void onResponse(
Call<List<NoteEntity>> call,Response<List<NoteEntity>> response
) {
if (response.isSuccessful()) {
if(response.body()!=null){
data.postValue(response.body()); //successfull data
}else{
data.postValue(null); //error
}
} else {
data.postValue(null); //error
}
}
@Override
fun onFailure(
Call<List<NoteEntity>> call,Throwable t
) {
data.postValue(null); //error
}
})
return data;
}
}
。由于$dest
(是%
的缩写)参数Where-Object
期望使用正则表达式,因此-notmatch
的路径由$dest
进行了转义。因为反斜杠[regex]::Escape
是正则表达式中的保留字符,所以需要这样做。 可以第一手以转义的形式编写路径,例如\
,但是Escape可以完成所有需要的工作。
c:\\my\\path\\to\\somewhere
现在,我们拥有除目标位置以外的所有文件,开始修剪列表。由于文件扩展名很多,因此将它们放在一个数组中。遍历数组,然后从Get-ChildItem $source\* -Recurse -File | ? { $_.psparentpath -notmatch [regex]::escape($dest) }
数组中删除每个匹配项。
$files
要删除$excluded = @("*.0","*.1","*.nomedia","*.thumbnail","*.chck","*.crypt12","*.tmp","*.db","*.crypt1","*.ini","*.pdrproj","*.pkpass","*.dat","*.enc","*.lck","*.xml","*.json","*.LOCK","*.443","*.preference")
foreach($ex in $excluded) {
$files = $files | ? {$_.extension -notlike $ex}
}
,请再次过滤集合:
$skip
这时,您所拥有的只是一个数组,其中包含要复制到$files = $files | ? {$_.DirectoryName -ne $skip)
中的文件。复制之前,请使用$dest
开关查看-WhatIf
会做什么,以确保复制能够按预期进行:
Copy-Item
作为一个完整的例子,
$files | % { Copy-Item -WhatIf $_ $dest }