Differences
This shows you the differences between the selected revisions of the page.
library_example_moves_files_keeping_directory_structure 2016-05-31 | library_example_moves_files_keeping_directory_structure 2023-11-15 (current) | ||
Line 8: | Line 8: | ||
==== C# ==== | ==== C# ==== | ||
- | Use the ''[[https://msdn.microsoft.com/en-us/library/dd383689.aspx|DirectoryInfo.EnumerateFileSystemInfos]]'' method to walk the source local tree. | + | Use the ''[[dotnet>system.io.directoryinfo.enumeratefilesysteminfos|DirectoryInfo.EnumerateFileSystemInfos]]'' method to walk the source local tree. |
<code csharp> | <code csharp> | ||
Line 29: | Line 29: | ||
UserName = "user", | UserName = "user", | ||
Password = "mypassword", | Password = "mypassword", | ||
- | SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
}; | }; | ||
Line 41: | Line 41: | ||
// Enumerate files and directories to upload | // Enumerate files and directories to upload | ||
- | IEnumerable<FileSystemInfo> fileInfos = new DirectoryInfo(localPath).EnumerateFileSystemInfos("*", SearchOption.AllDirectories); | + | IEnumerable<FileSystemInfo> fileInfos = |
+ | ···················new DirectoryInfo(localPath).EnumerateFileSystemInfos( | ||
+ | ························"*", SearchOption.AllDirectories); | ||
foreach (FileSystemInfo fileInfo in fileInfos) | foreach (FileSystemInfo fileInfo in fileInfos) | ||
{ | { | ||
- | string remoteFilePath = session.TranslateLocalPathToRemote(fileInfo.FullName, localPath, remotePath); | + | string remoteFilePath = |
+ | RemotePath.TranslateLocalPathToRemote( | ||
+ | ····························fileInfo.FullName, localPath, remotePath); | ||
if (fileInfo.Attributes.HasFlag(FileAttributes.Directory)) | if (fileInfo.Attributes.HasFlag(FileAttributes.Directory)) | ||
Line 57: | Line 61: | ||
else | else | ||
{ | { | ||
- | Console.WriteLine(string.Format("Moving file {0}...", fileInfo.FullName)); | + | Console.WriteLine("Moving file {0}...", fileInfo.FullName); |
// Upload file and remove original | // Upload file and remove original | ||
session.PutFiles(fileInfo.FullName, remoteFilePath, true).Check(); | session.PutFiles(fileInfo.FullName, remoteFilePath, true).Check(); | ||
Line 75: | Line 79: | ||
</code> | </code> | ||
- | ==== PowerShell ==== | + | ==== [[upload_powershell]] PowerShell ==== |
+ | |||
+ | You can install this script as an [[extension|WinSCP extension]] by using this page URL in the //[[ui_pref_commands#extensions|Add Extension]]// command. | ||
<code powershell - UploadDeleteKeepStructure.ps1> | <code powershell - UploadDeleteKeepStructure.ps1> | ||
# @name Upload and Delete Files | # @name Upload and Delete Files | ||
- | # @command powershell.exe -ExecutionPolicy Bypass -File "%EXTENSION_PATH%" -sessionUrl "!S" -remotePath "!/" -sessionLogPath "%SessionLogPath%" -pause !& | + | # @command powershell.exe -ExecutionPolicy Bypass -File "%EXTENSION_PATH%" ^ |
- | # @description Moves selected local files to a remote directory, but keeps local directory structure | + | # -sessionUrl "!E" -remotePath "!/" -sessionLogPath "%SessionLogPath%" ^ |
+ | # -pause !& | ||
+ | # @description Moves selected local files to a remote directory, ^ | ||
+ | # but keeps local directory structure | ||
# @flag ApplyToDirectories | # @flag ApplyToDirectories | ||
- | # @version 1 | + | # @version 5 |
# @homepage ~~SELF~~ | # @homepage ~~SELF~~ | ||
- | # @require WinSCP 5.8.4 | + | # @require WinSCP 5.16 |
- | # @option SessionLogPath file "&Session log file:" | + | # @option SessionLogPath -config sessionlogfile |
# @optionspage ~~SELF~~#options | # @optionspage ~~SELF~~#options | ||
param ( | param ( | ||
- | # Use Generate URL function to obtain a value for -sessionUrl parameter. | + | # Use Generate Session URL function to obtain a value for -sessionUrl parameter. |
- | [Parameter(Mandatory)] | + | [Parameter(Mandatory = $True)] |
- | $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xx-xx-xx@example.com/", | + | $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/", |
- | [Parameter(Mandatory)] | + | [Parameter(Mandatory = $True)] |
$remotePath, | $remotePath, | ||
$sessionLogPath = $Null, | $sessionLogPath = $Null, | ||
[Switch] | [Switch] | ||
- | $pause = $False, | + | $pause, |
- | [Parameter(Mandatory, ValueFromRemainingArguments, Position=0)] | + | [Parameter(Mandatory = $True, ValueFromRemainingArguments = $True, Position·=·0)] |
$localPaths | $localPaths | ||
) | ) | ||
Line 125: | Line 134: | ||
if (Test-Path $localPath -PathType container) | if (Test-Path $localPath -PathType container) | ||
{ | { | ||
- | $files = @($localPath) + (Get-ChildItem $localPath -Recurse | Select-Object -ExpandProperty FullName) | + | $files = |
+ | ···················@($localPath) + | ||
+ | ···················(Get-ChildItem $localPath -Recurse | Select-Object -ExpandProperty FullName) | ||
} | } | ||
else | else | ||
Line 136: | Line 147: | ||
foreach ($localFilePath in $files) | foreach ($localFilePath in $files) | ||
{ | { | ||
- | $remoteFilePath = $session.TranslateLocalPathToRemote($localFilePath, $parentLocalPath, $remotePath) | + | $remoteFilePath = |
+ | ···················[WinSCP.RemotePath]::TranslateLocalPathToRemote( | ||
+ | ························$localFilePath, $parentLocalPath, $remotePath) | ||
if (Test-Path $localFilePath -PathType container) | if (Test-Path $localFilePath -PathType container) | ||
Line 148: | Line 161: | ||
else | else | ||
{ | { | ||
- | Write-Host ("Moving file {0} to {1}..." -f $localFilePath, $remoteFilePath) | + | Write-Host "Moving file $localFilePath to $remoteFilePath..." |
# Upload file and remove original | # Upload file and remove original | ||
$session.PutFiles($localFilePath, $remoteFilePath, $True).Check() | $session.PutFiles($localFilePath, $remoteFilePath, $True).Check() | ||
Line 165: | Line 178: | ||
$result = 0 | $result = 0 | ||
} | } | ||
- | catch [Exception] | + | catch |
{ | { | ||
- | Write-Host ("Error: {0}" -f $_.Exception.Message) | + | Write-Host "Error: $($_.Exception.Message)" |
$result = 1 | $result = 1 | ||
} | } | ||
Line 181: | Line 194: | ||
</code> | </code> | ||
- | ===== Download ===== | + | === [[options]] Options === |
+ | |||
+ | In the //Session log file//, you can specify a path to a [[logging|session log file]]. | ||
+ | |||
+ | In the //Keyboard shortcut//, you can specify a [[custom_key_shortcuts|keyboard shortcut]] for the extension. | ||
+ | |||
+ | ===== [[download]] Download ===== | ||
- | For a download, you can use the code from the [[library_example_recursive_download_custom_error_handling|Recursively download directory tree with custom error handling]] example. | + | For a download, you can use the code from the [[library_example_recursive_download_custom_error_handling#tree_download|Explicit implementation of a file tree download section of Recursively download directory tree with custom error handling]] example. |
Just pass a ''true'' to the optional ''[[library_session_getfiles#remove|remove]]'' parameter of the ''[[library_session_getfiles|Session.GetFiles]]''. | Just pass a ''true'' to the optional ''[[library_session_getfiles#remove|remove]]'' parameter of the ''[[library_session_getfiles|Session.GetFiles]]''. | ||
Line 190: | Line 209: | ||
<code csharp> | <code csharp> | ||
- | session.GetFiles(session.EscapeFileMask(fileInfo.FullName), localFilePath, true); | + | session.GetFiles(remoteFilePath, localFilePath, true).Check(); |
</code> | </code> | ||
Line 196: | Line 215: | ||
<code powershell> | <code powershell> | ||
- | $session.GetFiles($session.EscapeFileMask($fileInfo.FullName), $localFilePath, $True) | + | $session.GetFiles($remoteFilePath, $localFilePath, $True).Check() |
</code> | </code> |