2017-01-17 2 views
1

私は一連の質問に答えるためのスクリプトを書いています。これらの質問に答えると、スクリプトは(提供された回答に基づいて)どのファイルをソースパスからデスティネーションパスに移動する必要があるかを判断します。Move-Itemを使ってファイルを新しい共有に移動する

$endpath変数に表示されているように、目的のパス(これはスクリプトにパスを入れる前)にユーザー入力を含めるように変更するまで、すべてスクリプトでうまくいきました。

スクリプトを実行すると、正常に実行されているように見えますが、完了しますが、ファイルは移動しませんでした。

$earliesttime = "00/00/0000" 
$lasttime = "00/00/0000" 
$size2 = 0 
$user = "" 
$sourcepath = $null 
$endpath = $null 
while ($sourcepath -eq $null) { 
    $sourcepath = Read-Host "Enter source file path" 
} 
Set-Location $sourcepath 
while ($endpath -eq $null) { 
    $endpath = Read-Host "Enter destination file path" 
} 
Set-Location $endpath 
Write-Host 
switch ($what = Read-Host "Do you want to search by owner?") { 
    No { 
     while ($earliesttime -eq "00/00/0000") { 
      Write-Host 
      $earliesttime = Read-Host "What is the earliest date of modification?" 
     } 
     while ($lasttime -eq "00/00/0000") { 
      Write-Host 
      $lasttime = Read-Host "What is the latest date of modification?" 
     } 
     Write-Host 
     $skip1 = Read-Host "Do you want to search by size?" 
     if ($skip1 -match "yes") { 
      while ($size2 -eq 0) { 
       $size2 = Read-Host "What is the size in KB of the objects you are looking for?" 
      } 
      if ($size2 -ge 1) { 
       Get-ChildItem -Recurse | Where-Object { 
        $_.Length /1KB -ge $size2 -and 
        $_.LastWriteTime -ge $earliesttime -and 
        $_.LastWriteTime -le $lasttime 
       } | Move-Item -Destination "$endpath" 
      } 
     } elseif ($skip1-match "no") { 
      Get-ChildItem -Recurse | Where-Object { 
       $_.LastWriteTime -ge $earliesttime -and 
       $_.LastWriteTime -le $lasttime 
      } | Move-Item -Destination "$endpath" 
     } 
    } 
    Yes { 
     while ($user -eq "") { 
      Write-Host 
      $user = Read-Host "What is the name of the owner (i.e. john.smith)?" 
     } 
     while ($earliesttime -eq "00/00/0000") { 
      Write-Host 
      $earliesttime = Read-Host "What is the earliest date of modification?" 
     } 
     while ($lasttime -eq "00/00/0000") { 
      Write-Host 
      $lasttime = Read-Host "What is the latest date of modification?" 
     } 
     Write-Host 
     $skip1 = Read-Host "Do you want to search by size?" 
     if ($skip1 -match "yes") { 
      while ($size2 -eq 0) { 
       $size2 = Read-Host " What is the size in KB of the objects you are looking for?" 
      } 
      if ($size2 -ge 1) { 
       Get-ChildItem -Recurse | Where-Object { 
        $_.GetAccessControl().Owner -eq $user -and 
        $_.Length /1KB -ge $size2 -and 
        $_.LastWriteTime -ge $earliesttime -and 
        $_.LastWriteTime -le $lasttime 
       } | Move-Item -Destination "$endpath" 
      } 
     } elseif ($skip1 -match "no") { 
      Get-ChildItem -Recurse | Where-Object { 
       $_.GetAccessControl().Owner -eq $user -and 
       $_.LastWriteTime -ge $earliesttime -and 
       $_.LastWriteTime -le $lasttime 
      } | Move-Item -Destination "$endpath" 
     } 
    } 
} 

答えて

0

あなたがここでの問題があります。

while ($sourcepath -eq $null) { 
    $sourcepath = Read-Host "Enter source file path" 
} 
Set-Location $sourcepath 
while ($endpath -eq $null) { 
    $endpath = Read-Host "Enter destination file path" 
} 
Set-Location $endpath 

だから、あなたはあなたの現在の場所としてあなたの$ソースパスを設定しているが、その後、その直後、あなたは$のEndPathとして、あなたの現在の位置を設定しています。

Set-Locationはcdに相当するので、ファイルをコピーしたいディレクトリの最後のディレクトリに変更するのが基本的です。次に、その場所から自分自身にファイルをコピーしようとしています。

あなたはSet-Location $endpath行を削除するか、Get-ChildItem -LiteralPath $sourcepath -Recurse | ...

+0

おかげでたくさんのおのGet-ChildItemsを変更したいと思います。私は実際に自分自身について疑問を抱いていました。ほんとうにありがとう。 – user7431743

+0

あなたがチャンスを取ったときに答えとしてマークしてください。 –

+0

こんにちはショーン、どうすればいいですか? – user7431743

関連する問題