ファイル名の角かっこを置き換える必要があり、を正常に作成(およびコンソールで検証)しました-replace
。Powershell 2.0のこのバグにより、ファイルの名前を簡単に変更できないMove-Item
ため、新しいディレクトリに移動しようとしています。
これが私のスクリプトです:
$txtPath = "c:\users\xxxxxx\desktop\cgc\tx" #source files
$txtPath2 = "c:\users\xxxxxx\desktop\cgc\tx2" #renamed files
Get-ChildItem $txtPath | foreach {
Move-Item -literalpath C:\users\xxxxxx\desktop\test001 ($_.Name -replace '\{|}','_')
}
何が起こっているのか:私は$txtPath2
変数を使用していましたが、「nullディレクトリにバインドできません」というエラーが発生し続けたため、変数の解析方法に何か奇妙なことがあるかどうかを確認するためにパスに明示的にコーディングしました。今、私はこのエラーを受け取ります:
Move-Item : Cannot move item because the item at 'C:\users\xxxxxx\desktop\test001' does not exist.
At C:\users\xxxxxx\desktop\cgc\rni.ps1:5 char:10
+ Move-Item <<<< -literalpath C:\users\xxxxxx\desktop\test001 ($_.Name -replace '\{|}','_')
+ CategoryInfo : InvalidOperation: (:) [Move-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.MoveItemCommand
奇妙なことに、新しいディレクトリを作成します。スクリプトを実行し、スクリプトが失敗するとデスクトップから消えるのを確認します。WTF?コンソールアプリケーションを終了して再起動し、変数をフラッシュしました。私は、さまざまなフレーバーの変数と定数をMove-Item
ラインで試しました。欠落しているパラメーターがない限りMove-Item
、何が起こっているのか本当にわかりません。私のファイルが削除される原因となるものを他の誰かが見ていますか?
編集:編集後
Get-ChildItem $txtPath | % { [system.io.file]::Move($_.fullname, ($i.FullName -replace '\[|\]', '') ) }
新しいエラーが発生します:
Exception calling "Move" with "2" argument(s): "Empty file name is not legal.
Parameter name: destFileName"
At C:\users\x46332\desktop\cgc\rni.ps1:6 char:52
+ Get-ChildItem $txtPath | % { [system.io.file]::Move <<<< ($_.fullname, ($i.FullName -replace '\[|\]', '') ) }
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException