Copy a list of files to a directory

前端 未结 1 1879
花落未央
花落未央 2021-02-07 13:57

There is a folder that contains a lot of files. Only some of the files needs to be copied to a different folder. There is a list that contains the files that need to be copied.<

相关标签:
1条回答
  • 2021-02-07 14:27

    Try this as your foreach-loop. It creates the targetfolder AND the necessary subfolders before copying the file.

    foreach ($itemToCopy in $imagesList)
    {
        $targetPathAndFile =  $itemToCopy.Replace( $sourceFolderName , $targetFolderName )
        $targetfolder = Split-Path $targetPathAndFile -Parent
    
        #If destination folder doesn't exist
        if (!(Test-Path $targetfolder -PathType Container)) {
            #Create destination folder
            New-Item -Path $targetfolder -ItemType Directory -Force
        }
    
        Copy-Item -Path $itemToCopy -Destination   $targetPathAndFile 
    }
    
    0 讨论(0)
提交回复
热议问题