Posh-SSH
Posh-SSH copied to clipboard
Move-SFTPItem results in 'Unable to rename file or folder'
Hello...I'm trying to move files from one folder to another on the SFTP server and keep getting the "Unable to rename file or folder" error.
The source and destination folders were created by the owner of the SFTP server and I don't have the required authorization to rename them manually under my login, so that's probably the cause of the issue.
What other technique can I use to move the files from source to destination without having to have to ask the folder creator to change the RW status of the folder name?
$SFTPSession = New-SFTPSession -ComputerName $ComputerName -Credential $Credentials
$LIMSImageFileSourcePath = '/Active Files/LIMSImages/' $LIMSImageFileDestinationPath = 'C:\Users\mburnett\Documents\LIMSData' $LIMSImageFileProcessedPath = '/Processed Files/LIMSImages/'
$LIMSProductDataFileSourcePath = '/Active Files/LIMSProductData/' $LIMSProductDataFileDestinationPath = 'C:\Users\mburnett\Documents\LIMSData' $LIMSProductDataFileProcessedPath = '/Processed Files/LIMSProductData/'
#the following two commands work correctly to copy folder contents from the SFTP server to my local drive Get-SFTPItem -SessionId $SFTPSession.SessionId -Path $LIMSImageFileSourcePath -Destination $LIMSProductDataFileDestinationPath -Force Get-SFTPItem -SessionId $SFTPSession.SessionId -Path $LIMSProductDataFileSourcePath -Destination $LIMSProductDataFileDestinationPath -Force
#the following two commands cause the "unable to rename file or folder" error Move-SFTPItem -SessionId $SFTPSession.SessionId -Path $LIMSImageFileSourcePath -Destination $LIMSImageFileProcessedPath Move-SFTPItem -SessionId $SFTPSession.SessionId -Path $LIMSProductDataFileSourcePath -Destination $LIMSProductDataFileProcessedPath
Exception calling "MoveTo" with "1" argument(s): "Unable to rename file or folder" At C:\Program Files\WindowsPowerShell\Modules\Posh-SSH\3.0.4\Posh-SSH.psm1:1391 char:17
-
$itemInfo.MoveTo($Destination)
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : NotSpecified: (:) [], MethodInvocationException
- FullyQualifiedErrorId : SshException
Exception calling "MoveTo" with "1" argument(s): "Unable to rename file or folder" At C:\Program Files\WindowsPowerShell\Modules\Posh-SSH\3.0.4\Posh-SSH.psm1:1391 char:17
-
$itemInfo.MoveTo($Destination)
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : NotSpecified: (:) [], MethodInvocationException
- FullyQualifiedErrorId : SshException
Thanks in advance!
Update...I now have the required permissions to update the folder names, which I was hoping was the issue that was causing the move operation to fail...but that isn't the issue. I am still getting the "unable to rename file or folder" error on both Move-SFTPItem commands.
What I need is a method to copy the files from the Active Files folder to the Processed Files folder and then kill the files in the Active Files folder to clear the slate for the next round of files.
Is there an alternative to Move-SFTPItem that I can use for troubleshooting. I can probably use Remove-SFTPItem to kill a file, but what is the equivalent of FileCopy?
I've tried using the Get-SFTPItem and Set-SFTPItem cmdlets for this purpose and they assume that files are being moved back and forth between the server and a local drive....doesn't work for intraserver file transfers.
Please let me know if the syntax I'm using for Move-SFTPItem is correct so I can let the person who manages the SFTP server know that there is something else that needs to be done on his end to allow the cmdlet to function properly.
Thank you.
just tried, i have no problem with my test files and folders...
btw, can you try to use paths without spaces ?
Thank you for confirming @MVKozlov.
Yes, I've removed all of the spaces from the paths during troubleshooting and I get the same error.
I've asked the server manager about the rights-settings. The files (txt and xlsx) I'm trying to move are marked 'rw-' and can't be changed to 'rwx', which I'm not even sure is an issue...but we're grasping at straws at this point.
I think the 'x' just indicates that a user has the right to execute the file, which doesn't make much sense for a txt file. It could be that files with this extension are automatically set to 'rw-'
In the properties, the 'x' can be selected with a checkbox, but the change doesn't stick. The rights revert back to 'rw-'
Any ideas you have on this matter are certainly welcomed.
This is a windows system with SSH/SFTP?
Sent from my iPhone
On Jun 22, 2022, at 11:38 AM, Michael Burnett @.***> wrote:
Thank you for confirming @MVKozlov.
Yes, I've removed all of the spaces from the paths during troubleshooting and I get the same error.
I've asked the server manager about the rights-settings. The files (txt and xlsx) I'm trying to move are marked 'rw-' and can't be changed to 'rwx', which I'm not even sure is an issue...but we're grasping at straws at this point.
I think the 'x' just indicates that a user has the right to execute the file, which doesn't make much sense for a txt file. It could be that files with this extension are automatically set to 'rw-'
In the properties, the 'x' can be selected with a checkbox, but the change doesn't stick. The rights revert back to 'rw-'
Any ideas you have on this matter are certainly welcomed.
— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.
@darkoperator I'm not sure about the OS of the SFTP server as we're not the owners. Is there a Posh-SSH command I can run to get those details?
I did learn from them that we can't use an SSH key for login. We must use username/pw.
I also wanted to say that I just tested the following command and it works just fine (fwiw)...
Remove-SFTPItem -SessionId $SFTPSession.SessionId -Path '/SourceFolder/test'
Just wanted to simplify the presentation of code that's creating the issue.
The objective is to move all of the files in folder /ActiveFiles/LIMSImages/ into the folder /ProcessedFiles/LIMSImages/
$LIMSImageFileSourcePath = '/ActiveFiles/LIMSImages/' $LIMSImageFileDestinationPath = 'C:\Users\mburnett\Documents\LIMSData' $LIMSImageFileProcessedPath = '/ProcessedFiles/LIMSImages/'
#works... Get-SFTPItem -SessionId $SFTPSession.SessionId -Path $LIMSImageFileSourcePath -Destination $LIMSProductDataFileDestinationPath -Force
#doesn't work... Move-SFTPItem -SessionId $SFTPSession.SessionId -Path $LIMSImageFileSourcePath -Destination $LIMSImageFileProcessedPath
I mentioned it because windows has a more complex permission system on files than linux normally. if the folder is flat in terms of files I would do
Get-SFTPChildItem -SessionId 0 -Path /home/pi/test1 | foreach {if ($_.IsRegularFile){$_.moveto("/home/pi/test2/$($_.name)")}}
to enumerate all files and use the method to copy each, if it has subfolders it will be a bit more tricky.
OK thank you...I'll let you know how it goes.
Worked great! Thanks for taking the time to help me.
$LIMSImageFileSourcePath = '/ActiveFiles/LIMSImages/' $LIMSImageFileProcessedPath = '/ProcessedFiles/LIMSImages/'
$LIMSProductDataFileSourcePath = '/ActiveFiles/LIMSProductData/' $LIMSProductDataFileProcessedPath = '/ProcessedFiles/LIMSProductData/'
Get-SFTPChildItem -SessionId 0 -Path $LIMSImageFileSourcePath | foreach {if ($_.IsRegularFile)
{$.moveto("$LIMSImageFileProcessedPath/$($.name)")}}
Get-SFTPChildItem -SessionId 0 -Path $LIMSProductDataFileSourcePath | foreach {if ($_.IsRegularFile)
{$.moveto("$LIMSProductDataFileProcessedPath/$($.name)")}}
@MVKozlov Just for the record, did you run your tests on a Linux or Windows SFTP system?
linux, but if you want I may try windows too
I wanted to clarify, at first I thought you wanted to move directories, but when you mentioned .txt files, I thought that the names of these files are not in your code. Could this really be the problem? and solution like
Get-SFTPChildItem ... | Foreach { Move-SFTPItem ... }
will work too ?
Should work to, since I was working with the properties to not follow links I stuck with the object methods
Sent from my iPhone
On Jun 22, 2022, at 7:09 PM, Max Kozlov @.***> wrote:
linux, but if you want I may try windows too
I wanted to clarify, at first I thought you wanted to move directories, but when you mentioned .txt files, I thought that the names of these files are not in your code. Could this really be the problem? and solution like
Get-SFTPChildItem ... | Foreach { Move-SFTPItem ... } will work too ?
— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.
Thank you @darkoperator and @MVKozlov. I'll ask the SFTP server admin about the configuration the next time we connect.
Thanks again for helping me push this over the line.
can I close the issue?