PowerShell-从特定文件夹复制特定文件

PowerShell-从特定文件夹复制特定文件,powershell,powershell-ise,Powershell,Powershell Ise,因此,文件夹结构如下所示: 源文件夹 file1.txt file1.doc 子文件夹1 file2.txt file2.doc 子文件夹 file3.txt doc3.txt 我要做的是将所有.txt文件从文件夹(文件夹名称包含eng)复制到目标文件夹。只是文件夹中的所有文件,而不是文件结构 我用的是: $dest = "C:\Users\username\Desktop\Final" $source = "C:\Users\username\Desktop\Test1" Co

因此,文件夹结构如下所示:

  • 源文件夹
    • file1.txt
    • file1.doc
    • 子文件夹1
      • file2.txt
      • file2.doc
      • 子文件夹
        • file3.txt
        • doc3.txt
  • 我要做的是将所有.txt文件从文件夹(文件夹名称包含eng)复制到目标文件夹。只是文件夹中的所有文件,而不是文件结构

    我用的是:

    $dest = "C:\Users\username\Desktop\Final"
    $source = "C:\Users\username\Desktop\Test1"
    Copy-Item $source\eng*\*.txt $dest -Recurse
    
    问题是它只从每个父文件夹复制.txt文件,而不复制子文件夹

    如何在脚本中包含所有子文件夹,并保持对英文名称的检查?你能帮帮我吗


    我说的是PowerShell命令。我应该改用机器人摄影吗

    使用powershell这样做很好。尝试:

    $dest = "C:\Users\username\Desktop\Final"
    $source = "C:\Users\username\Desktop\Test1"
    
    Get-ChildItem $source -filter "*.txt" -Recurse | Where-Object { $_.DirectoryName -match "eng"} | ForEach-Object { Copy-Item $_.fullname $dest }
    

    首先获取名称中包含
    eng
    的所有文件夹的列表可能更容易

    $dest=“C:\Users\username\Desktop\Final”
    $source=“C:\Users\username\Desktop\Test1”
    $engFolders=Get ChildItem$source-Directory-Recurse | Where{$\.BaseName-match“^eng”}
    Foreach($engFolders中的文件夹){
    复制项目($folder.FullName+“\*.txt”)$dest
    }
    
    另一个PowerShell解决方案:)

    #设置变量
    $Dst='C:\Users\username\Desktop\Final'
    $Src='C:\Users\username\Desktop\Test1'
    $FolderName='eng*'
    $FileType='*.txt'
    #获取“eng*”文件对象的列表
    Get ChildItem-Path$Src-Filter$FolderName-Recurse-Force|
    #那些“eng*”文件对象应该是文件夹
    其中对象{$\ PSIsContainer}|
    #对于每个“eng*”文件夹
    ForEach对象{
    #将其中的所有“*.txt”文件复制到目标文件夹
    复制项-Path(连接路径-Path$\.FullName-ChildPath'\*')-Filter$FileType-Destination$Dst-Force
    }
    
    您可以执行以下操作:

    $dest = "C:\NewFolder"
    $source = "C:\TestFolder"
    $files = Get-ChildItem $source -File -include "*.txt" -Recurse | Where-Object { $_.DirectoryName -like "*eng*" }
    Copy-Item -Path $files -Destination $dest
    
    另一个例子:

    $SourceRoot = <Source folder path>
    $TargetFolder = <Target folder path>
    
    
    @(Get-ChildItem $SourceRoot -Recurse -File -Filter *.txt| Select -ExpandProperty Fullname) -like '*\eng*\*' |
    foreach {Copy-Item $_ -Destination $TargetFolder}
    
    $SourceRoot=
    $TargetFolder=
    @(获取ChildItem$SourceRoot-Recurse-File-Filter*.txt | Select-ExpandProperty Fullname)-如“*\eng*\*”|
    foreach{Copy Item$\目标$TargetFolder}
    
    谢谢!它起作用了,但只是部分起作用。没有检查文件夹名称上的eng。你也能帮我吗?小心这个。它将匹配文件路径中任何包含“eng”的文件。以下任何一项都会匹配:C:\eng\file.txt、C:\lengend\file.txt、C:\directory\file-eng.txt等。为了澄清,我的上述注释仅在$source等于C:\时才出现。为了简洁起见,我的示例使用C:\作为根,但问题是相同的。@JohnEnxada更新。检查文件的目录名是否包括
    eng
    。正如“New Guy”所指出的,这可能出现在树中的任何位置,但当您设置源目录时,这不应该是一个问题here@arco444这个问题仍然可能发生。以下是一些与OP指定的源代码相匹配的示例:C:\Users\username\Desktop\Test1\english\file.txt,C:\Users\username\Desktop\Test1\english\file.txt,C:\Users\username\Desktop\Test1\bioengineering\file.txt,可能OP不必担心,因为文件名可能是一致且众所周知的。然而,用户和管理员往往会产生不一致。最好是查看每个文件夹的基本名称,并确保它从 Eng/Cube >开始,而不是在名称的中间或结尾。感谢PS脚本,它工作得很好。评论也很有帮助。@JohnEnxada不客气。检查这个答案(更高级的脚本,处理文件名冲突):虽然不能按文件夹名过滤,但如果需要,可以从中删除文件名检查代码。