簡體   English   中英

Powershell-在Get-ChildItem中包含子文件夾

[英]Powershell - Include subfolders in Get-ChildItem

好吧,還有一個PowerShell問題。

該腳本根據文件名的第一部分將文件移動到文件夾中。 我現在遇到了一個問題,即最終將它們移動到的文件夾嵌套在頂部文件夾中,因此:

“主文件夾”->“ X0”->“ X00000”因此,如果我有X0897,則需要查找主文件夾“ X0”的子文件夾,以查找名為“ X0897”的“ X0”的子文件夾。

該腳本必須能夠在“主文件夾”中查找,然后在“ X00000”的子文件夾中查找。

這是使用遞歸完成的嗎? 我在正確的道路上嗎? 我需要刪除GetChild嗎?

# First, we retrieve a list of files where
# $sDir = Source Directory
$sDir = "N:\USERS\Username\General\Test1\"

# Generate a list of all files in source directory
$Files = (Get-ChildItem -recurse?? $sDir)

# $tDir = Root Target Directory (setting the directory the files are to be moved to)
$tDir = "N:\USERS\User\General\Test\"

# Loop through the list of file names
foreach($File in $Files)
{
# $wFile is set as the variable for our working file name
$wFile = $File.BaseName

# This command splits the working file name to extract the file number from the first part of the filename (nFile)
$nFile = $wFile.Split(' ')[0]


# dFold = the final destination folder of the file in the format of \\drive\folder\SubFolder\    
$dFold = "$tDir$nFile"

# Tests if the destination folder exists
if(Test-Path $dFold -PathType Container)
  {
  # If the folder exists then we move the file
  Copy-Item -Path $sDir$File -Destination $dFold

  # This denotes where the file has been moved to        
  Write-Host $File "Was Moved to:" $dFold
  Write-Host
  }
  # If a folder for the file number does not exist it will not be moved!
  else 
  {
  # This tells you if the file was not moved
  Write-Host $File "Was not moved!"
  }

}

好的,我想我有:

$targetFolder = '{0}{1}\{2}' -f $tDir, $nFile.SubString(0,3), $nFile

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM