尝试将同一文件夹结构中某个文件夹的子文件夹中的第n个文件复制到某个文件夹中

  • 本文关键字:文件夹 文件 复制 结构 powershell
  • 更新时间 :
  • 英文 :


这只复制空文件夹。我正在尝试将同一文件夹结构中文件夹的子文件夹中的第n个文件复制到文件夹中

$Files = Get-ChildItem "D:n.a.dexcel backupsautorecover backupsExcelNew folder (2)"
$i = 0
Do{
$files[$i]|copy-Item -Dest "D:n.a.dexcel backupsautorecover backupsExcelNew folder (3)"
$i=$i+2
}While($i -le $files.count)
}

好吧,这里有一种方法!可能有一种更干净的方法,但这种方法有效,可以将文件夹结构正确地放在目的地。

# Add our paths to variables
$SourceFolder = 'D:n.a.dexcel backupsautorecover backupsExcelNew folder (2)'
$Destination = 'D:n.a.dexcel backupsautorecover backupsExcelNew folder (3)'
# Variable for n, where n is the gap between files you want copied
$n = 2
# Get all subfolders
$AllSubFolders = Get-ChildItem $SourceFolder -Recurse -Directory
# Add our source root folder to the folder list
$AllFolders = $AllSubFolders.FullName + $SourceFolder
# Loop over all folders, figure out the correct destination folder path by joining the destination +
# the current folder with the $SourceFolder part removed (that's the .SubString-part) and create matching
# folders in the destination, then get all files in the folder and copy every nth file in a similar manner
# as your question.
foreach ($Folder in $AllFolders) {
$CurrentDestFolder = (Join-Path $Destination ($Folder.Substring($SourceFolder.Length)))
[void](New-Item -Path $CurrentDestFolder -Force -ItemType Directory)
$CurrentFiles = Get-ChildItem $Folder -File
$i = 0
while ($i -lt $CurrentFiles.Count) {
$CurrentFiles[$i] | Copy-Item -Destination (Join-Path $CurrentDestFolder $_)
$i = $i+$n
}
}

最新更新