0

I need to replace square brackets in filenames, and I've successfully created (and validated at the console) a -replace. Now I'm trying to Move-Item to a new directory because this bug in Powershell 2.0 prevents me from doing a simple file rename.

Here's my script:

$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 '\{|}','_') 
}

Here's what's happening: I was using the $txtPath2 variable, but kept getting "cannot bind to null directory" errors, so I explicitly coded to the path to see if there was something odd with how the variable parsed. Now, I get this error:

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

Here's what's odd: I create the new directory. I run the script, watch it vanish from my desktop as the script fails. WTF? I've quit and restarted the console application to flush any variables. I've tried different flavors of variable vs. constant in the Move-Item line. Unless there's a Move-Item parameter that I'm missing, I really have no idea what's going on. Does anyone else see anything that would cause my file to be deleted?

EDIT: After editing to

Get-ChildItem $txtPath | % { [system.io.file]::Move($_.fullname, ($i.FullName -replace '\[|\]', '') ) }

I get a new error:

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
dwwilson66
  • 6,806
  • 27
  • 72
  • 117

1 Answers1

2

You set a modified version of $_.Name as destination (second arg). "Name" is just the FILEname of an item, so I'm guessing your test001 file/folder got moved to the place you run the script from and renamed to whatever $_.Name was (it uses Name as a relative path). So if you run this script from c:\windows\system32 (default folder when PS is running as admin), you move it there.

The next time in your foreach-loop, test001 is already moved and it returns an error. -LiteralPath is source location, not destination.

Try:

Get-ChildItem $txtPath | % { [system.io.file]::Move($_.fullname, ($_.FullName -replace '\[|\]', '') ) }
Frode F.
  • 52,376
  • 9
  • 98
  • 114
  • OK, that makes sense why that failed on me. So what's the proper syntax? I had used `Move-Item -literalpath `. Do I need the modifier on `-literalpath`? or can I just use `Move-Item -literalpath `. I was under the impression that I needed to define my path after the `-literalpath` variable. Thanks for clarifying. – dwwilson66 Jan 31 '13 at 15:42
  • I need more info to help you. Are you trying to rename the file, move it or both? what is the destination? – Frode F. Jan 31 '13 at 17:21
  • I need to replace square brackets in filenames. This bug in Powershell https://connect.microsoft.com/PowerShell/feedback/details/277707/rename-item-fails-when-renaming-a-file-and-the-filename-contains-brackets prevents me from a simple `rni`. The workaround is to use `move-item` and `-literalpath`. So, while renaming the file is my desired result, I need to jump through a `move-item` hoop to get there. As noted in my code, I am moving the files (with a new name) from one subdirectory to another: `.\tx` to `.\tx2`. Does that help? – dwwilson66 Jan 31 '13 at 20:32
  • you don't have to actually move the file to a New folder, just use the move-item cmdlet(or .net class). see updated answer – Frode F. Jan 31 '13 at 22:07
  • see my origina; post for the error I get when using your sample in your answer. I'm not sure exactly what the `$i.` does when it replaces the `$_.` is `i` just an on-the-fly variable to hold the new file name with the replaced characters? – dwwilson66 Feb 01 '13 at 14:14
  • No sry. $i should be $_ i was just testing the replace command against a var and forgot to change it – Frode F. Feb 01 '13 at 15:18
  • Ah, crap. Same error after changing to `$_i`. Does `$_i.` need to be declared somewhere? or is it just recognized as "use the first parameter for the second field instance". – dwwilson66 Feb 01 '13 at 15:50
  • Haha. iPhone autocorrect. Forget the "i". Only "$_". :) I updated my answer too – Frode F. Feb 01 '13 at 15:57
  • Note to self: never do stackoverflow on a smartphone!! :) I'm a lot less worried I was missing some vital technique with that stray i. Works like a dream...thanks so much for the assistance. – dwwilson66 Feb 01 '13 at 16:09