Problem with names of the files containing a symbol [

by Dmitry at 2013-03-16 03:15:18

Hey Powershell.org community.
There is a script:
# Move all files from Directory folder to Data

$fromDirPath = "\server\Directory$"
$toDirPath = "F:\Folder\Data"

If ((Test-Path $toDirPath) -eq $true) # Check if script run on active node
{
# Local and remote dirs. Format - “<local dir><remote dir>"
$nameArr = @("Data1
Folder1”,“Data2Folder2","Data3Folder3”,“Data4Folder4")

$errorMsg = “”

$logFile = “F:\Folder\Data\ScriptLogs.log”

# Error messages recipient.
$to1 = ”user@yourdomain.ru”

# SMTP server settings.
$smtpServer = “mailserver.local”
$SMTPPort = “888”
$msg = new-object Net.Mail.MailMessage
$msg.From = “no-reply@yourdomain.ru
$msg.To.Add($to1)


function sendEMail($subj,$body)
{
$msg.Subject = $subj
$msg.Body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpServer,$SMTPPort)
$smtp.Send($msg)
}

function moveFiles ($from, $to)
{
$status = 0


$lPath = $fromDirPath + $from
$rPath = $toDirPath + $to
if (((Test-Path $lPath)) -and ((Test-Path $rPath)))
{
$items = Get-ChildItem ($lPath + “*”) -Include "
.txt”
if ($items -ne 0)
{
foreach ($item in $items)
{
try
{
Move-Item -Path $item.FullName -Destination ($rPath + "&quot; + $item.Name) -Force -Confirm:$false
$message = (Get-Date -UFormat “%d-%m-%Y %H:%M”) + " | INFO | File " + $item.FullName + " has been sucessfully moved to destination - " + ($rPath + "&quot; + $item.Name) + “.”
}
catch
{
$status = 1
$global:errorMsg = $global:errorMsg + "nCannot move file &quot; + $item&#46;FullName<br> $message = (Get-Date -UFormat &quot;%d-%m-%Y %H&#58;%M&quot;) + &quot; | ERROR | File &quot; + $item&#46;FullName + &quot; cannot be moved to destination - &quot; + ($rPath + &quot;\&quot; + $item&#46;Name) + &quot;&#46;&quot;<br> }<br> echo $message &gt;&gt; $logFile<br> }<br> }<br> if ((Get-ChildItem $lPath)&#46;Count -ne 0)<br> {<br> $status = 1<br> $global&#58;errorMsg = $global&#58;errorMsg + &quot;nThere are unknown files: " + (Get-ChildItem $lPath)
}
}
else
{
$status = 1
if (!(Test-Path $lPath))
{
$global:errorMsg = $global:errorMsg + “nLocal path error&#46; (&quot; + $lPath + &quot;)&#46;&quot;<br> }<br> elseif (!(Test-Path $rPath))<br> {<br> $global&#58;errorMsg = $global&#58;errorMsg + &quot;nRemote path error. (” + $rPath + “).”
}
else
{
$global:errorMsg = $global:errorMsg + "`nUnknown file error. (local - " + $lPath + " , remote - " + $rPath + “).”
}
}
return $status
}


foreach ($str in $nameArr)
{
$splitStr = $str -Split “*”
if ($splitStr.Count -eq 2)
{
if ((moveFiles $splitStr[0] $splitStr[1]))
{
sendEmail “Script Work Error” $errorMsg
}
}
}
}


Script task is simple - it moves the data from four folders in the FTP directory Directory in 4 matching folders stored in the Data.
Everything worked fine, but then began to upload files with names like XX[dd.mm.yyyy]YY[dd.mm.yyyy] and then the problems started. The script does not move
files, which does contain the symbol [. How in this script it is better to shield it [ or the file name entirely can?
by mk.maddin at 2013-03-16 05:10:06
I had a problem like this, too.

Try to use Move-Item -LiteralPath $item.FullName … in your MoveFiles function instead ofMove-Item -Path $item.FullName …
by MasterOfTheHat at 2013-03-17 11:09:07
mk hit it on the head… You have to either escape special characters in the strings or you have to tell Powershell not to treat special characters as anything but characters.
by Dmitry at 2013-03-18 01:24:32
Use Move-Item -LiteralPath did not help(((
by MasterOfTheHat at 2013-03-18 10:45:50
Not sure what happened then, Dmitry… Here’s what happens to me when I try to reproduce it:
PS C:\temp\test> gci


Directory: C:\temp\test


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a— 3/18/2013 12:34 PM 8 XX[03.18.2013]YY[03.01.2012]


PS C:\temp\test> move-item -LiteralPath “.\XX[03.18.2013]YY[03.01.2012]” -Destination "…\test2\XX[03.18.2013]YY[03.01.2
012]"
PS C:\temp\test> gci
PS C:\temp\test> gci …/test2


Directory: C:\temp\test2


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a— 3/18/2013 12:34 PM 8 XX[03.18.2013]YY[03.01.2012]


And even in a basic script:
$filename = “XX[03.18.2013]YY[03.01.2012]”
“Source directory”
gci c:\temp\test
"Destination directory"
gci c:\temp\test2
move-item -LiteralPath “c:\temp\test$filename” -Destination “c:\temp\test2$filename”
“Source directory”
gci c:\temp\test
"Destination directory"
gci c]

Output:
PS F:\Storage\Scripts\Windows\Junk> .\specialchars.ps1
Source directory


Directory: C:\temp\test


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a— 3/18/2013 12:34 PM 8 XX[03.18.2013]YY[03.01.2012]
Destination directory
Source directory
Destination directory


Directory: C:\temp\test2


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a— 3/18/2013 12:34 PM 8 XX[03.18.2013]YY[03.01.2012]


What exactly are you doing in the console, and what output do you get?