简体   繁体   English

动态地将文件从文件夹和子文件夹复制到目标位置

[英]Dynamcially copy files from folders and subfolders to destination

I have a folder 我有一个文件夹

D:\\Projects\\PowerShell\\Common D:\\ Projects \\ PowerShell \\ Common

Inside Common folder, there got many sub folders and sub sub folders. 在“公用”文件夹中,有许多子文件夹和子子文件夹。 What I want to achieve is to look for a csv files and copy to: 我要实现的是寻找一个csv文件并复制到:

E:\\Projects\\PowerShell2\\Common E:\\ Projects \\ PowerShell2 \\ Common

**D:\\Projects\\PowerShell\\Common and E:\\Projects\\PowerShell2\\Common has same directory structure. ** D:\\ Projects \\ PowerShell \\ Common和E:\\ Projects \\ PowerShell2 \\ Common具有相同的目录结构。

Example

D:\\Projects\\PowerShell\\Common\\Geo\\ABC.csv D:\\ Projects \\ PowerShell \\ Common \\ Geo \\ ABC.csv

D:\\Projects\\PowerShell\\Common\\Geo\\MEA.csv D:\\ Projects \\ PowerShell \\ Common \\ Geo \\ MEA.csv

D:\\Projects\\PowerShell\\Common\\AREA\\ASA.csv D:\\ Projects \\ PowerShell \\ Common \\ AREA \\ ASA.csv

Copy to 复制到

E:\\Projects\\PowerShell2\\Common\\Geo\\ABC.csv E:\\ Projects \\ PowerShell2 \\ Common \\ Geo \\ ABC.csv

E:\\Projects\\PowerShell2\\Common\\Geo\\MEA.csv E:\\ Projects \\ PowerShell2 \\ Common \\ Geo \\ MEA.csv

E:\\Projects\\PowerShell2\\Common\\AREA\\ASA.csv E:\\ Projects \\ PowerShell2 \\ Common \\ AREA \\ ASA.csv

I can achieve this if I specify the location exactly like below, but I want the script able to handle it dynamically instead of hard-code. 如果我完全按照以下方式指定位置,则可以实现此目的,但是我希望脚本能够动态处理而不是硬代码处理。 if the same files exist, just replace them. 如果存在相同的文件,只需替换它们即可。

$Src = 'D:\Projects\PowerShell\Common\Geo'
$Dst = 'E:\Projects\PowerShell2\Common\Geo'

$Extension = '*.csv'

Get-ChildItem -Path $Src -Filter $Extension -Recurse |
Where-Object {!$_.PsIsContainer} |
    # For each file
    ForEach-Object 
    {
        some code
    }

try this, copy and create dir in same time (if not exists) 试试这个,同时复制并创建目录(如果不存在)

$source='D:\Projects\PowerShell'
$destination='E:\Projects\PowerShell2'

gci $source -file -Filter "*.csv" -Recurse | 
%{$newfile=$_.Directory -replace [regex]::Escape($source), $destination; copy-item -Path $_.FullName -Destination (new-item -type directory -force $newfile)  -force -ea 0}

if you want log copied files 如果要日志复制的文件

$source='D:\Projects\PowerShell'
$destination='E:\Projects\PowerShell2'
$logfile="c:\temp\mylog.log"

gci $source -file -Filter "*.csv" -Recurse | 
%{$newfile=$_.Directory -replace [regex]::Escape($source), $destination; copy-item -Path $_.FullName -Destination (new-item -type directory -force $newfile)  -force -ea 0 -PassThru | 
%{ $_.fullname | out-file $logfile -Append}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PowerShell将文件复制到目标的子文件夹,同时排除目标中的某些文件夹 - PowerShell to copy files to destination's subfolders while excluding certain folders in the destination Powershell将某些特定文件和某些子文件夹复制到目标位置 - Powershell to copy some specific files and some subfolders to destination 文件夹和子文件夹中的列表文件详细信息重复 - List files details from folders and subfolders are duplicading Powershell:将文件夹和子文件夹中的所有文件移动到单个文件夹中 - Powershell: Move all files from folders and subfolders into single folder 将文件从多个文件夹移动到相应的目标文件夹 - Move files from multiple folders to a corresponding destination folder 使用 Powershell 脚本从子文件夹复制特定类型的文件 - Copy specific type files from subfolders using a Powershell script 使用PowerShell复制源服务器相同目录结构中的文件夹和子文件夹中的项目文件 - Copy-item Files in Folders and subfolders in the same directory structure of source server using PowerShell PowerShell - 从特定文件夹复制特定文件 - PowerShell - Copy specific files from specific folders 我坚持使用 powershell 脚本,该脚本必须将多个子文件夹中的所有文件压缩到目标文件夹 - Im stuck on a powershell script that has to compress all files from multiple subfolders to a destination folder 从特定文件夹复制特定文件(.rar中的文件夹) - Copy specific files from specific folders (folders in .rar)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM