2016-04-07 10 views
0

HI誰もが私のPowerShellの練習用のスクリプトを書いて、ユーザの入力に基づいてあるフォルダから別のフォルダにファイルを移動しようとしています。スクリプトは正常に動作しますが、そのファイルがスクリプトと同じフォルダに存在する場合に限ります。誰かが私が間違っているの何私を助けるか、ロジックpowershellのユーザ入力に基づいて別のフォルダにファイルを移動する方法

あなたは、ファイルが現在の場所に存在しているかどうかそうでない場合は、テスト・パスのチェックを完全なファイルパスを決定する必要が
$destination = 'P:\Powershell practice\Movefolder' 
$ListFile = "P:\Powershell practice\" 
get-childitem $ListFile 
$Filename = Read-host -prompt "Please Enter File to be moved" 
$FileExists = test-path $Filename 

If ($FileExists -eq $True) { 
move-item $Filename $destination 
Write-Host "File is moved to $destination " 
} 
else 
{write-host "No File Found"} 
+0

最初は、移動するファイルの正確な名前を入力できるように、PowerShellの練習フォルダのすべてのコンテンツをリストします。問題は、私がスクリプトを実行しているファイルを探しているだけです。ユーザーが入力したファイル名の$ listfileの下を見て、それを移動してください。 – sanees

答えて

0

を支援してみてくださいすることができます。あなたはパスに参加するJoin-Pathコマンドレットを使用することができます。

$FilePath = Join-Path $ListFile $FileName 
$FileExists = test-path $FilePath 

If ($FileExists -eq $True) { 
move-item $FilePath $destination 
... 

Offtopic: あなたはまた、グリッドビュー、ユーザーが複数のファイルを選択し、ファイルをコピーするために下部にある[OK]ボタンを押すことができます使用することができます;-):

$destination = 'P:\Powershell practice\Movefolder' 
$ListFile = 'P:\Powershell practice\' 

$filesToMove = get-childitem $ListFile | Out-GridView -OutputMode Multiple 
$filesToMove | % { move-item $_.FullName $destination} 
+0

鮮やかなもの、この作品 – sanees

+0

その答えで私の更新をチェックしてください。多分あなたのニーズにも合っています。 –

+0

でもそれはより良いです。私はこれも好きだったが、これでもう少し再生され、自分の必要に合ったものを見て – sanees

0

誰かが必要な場合に備えてください。以下はこのための正しい作業用スクリプトです

$destination = 'P:\Powershell practice\Movefolder' 
$ListFile = 'P:\Powershell practice\' 
get-childitem $ListFile 
$Filename = Read-host -prompt "Please Enter File to be moved" 
$FilePath = Join-Path $ListFile $FileName 
$FileExists = test-path $FilePath 

If ($FileExists -eq $True) { 
move-item $FilePath $destination 
Write-Host "File is moved to $destination " 
} 
else 
{write-host "No File Found"} 
関連する問題