Differences
This shows you the differences between the selected revisions of the page.
library_example_advanced_rename 2017-02-02 | library_example_advanced_rename 2023-01-11 (current) | ||
Line 7: | Line 7: | ||
* changing leading characters: ''mv new*.* old*.*'' | * changing leading characters: ''mv new*.* old*.*'' | ||
- | ===== Advanced rename with PowerShell ===== | + | ===== [[powershell]] Advanced rename with PowerShell ===== |
But for more advanced rename operations, you need to use your favorite scripting language language, like [[library_powershell|PowerShell]], to generate a new name and use [[library|WinSCP .NET assembly]] for the actual [[library_session_movefile|rename]] (or [[guide_automation#parametrized|generate a script file]]). | But for more advanced rename operations, you need to use your favorite scripting language language, like [[library_powershell|PowerShell]], to generate a new name and use [[library|WinSCP .NET assembly]] for the actual [[library_session_movefile|rename]] (or [[guide_automation#parametrized|generate a script file]]). | ||
Line 16: | Line 16: | ||
<code powershell - BatchRename.ps1> | <code powershell - BatchRename.ps1> | ||
# @name Batch &Rename... | # @name Batch &Rename... | ||
- | # @command powershell.exe -ExecutionPolicy Bypass -File "%EXTENSION_PATH%" -sessionUrl "!S" -remotePath "!/" -pattern "%Pattern%" -replacement "%Replacement%" -pause -sessionLogPath "%SessionLogPath%" %PreviewMode% !& | + | # @command powershell.exe -ExecutionPolicy Bypass -File "%EXTENSION_PATH%" ^ |
- | # @description Renames remote file using regular expression | + | # -sessionUrl "!E" -remotePath "!/" -pattern "%Pattern%" ^ |
+ | # -replacement "%Replacement%" -refresh -pause -sessionLogPath "%SessionLogPath%" ^ | ||
+ | # %PreviewMode% !& | ||
+ | # @description Renames remote files using a regular expression | ||
# @flag RemoteFiles | # @flag RemoteFiles | ||
- | # @version 1 | + | # @version 7 |
# @homepage ~~SELF~~ | # @homepage ~~SELF~~ | ||
- | # @require WinSCP 5.8.4 | + | # @require WinSCP 5.19 |
# @option - -run group "Rename" | # @option - -run group "Rename" | ||
# @option Pattern -run textbox "Replace file name part matching this pattern:" | # @option Pattern -run textbox "Replace file name part matching this pattern:" | ||
# @option Replacement -run textbox "with:" | # @option Replacement -run textbox "with:" | ||
# @option - -run -config group "Options" | # @option - -run -config group "Options" | ||
- | # @option PreviewMode -run -config checkbox "&Preview changes" "-previewMode" "-previewMode" | + | # @option PreviewMode -run -config checkbox "&Preview changes" "-previewMode" ^ |
+ | # "-previewMode" | ||
# @option - -config group "Logging" | # @option - -config group "Logging" | ||
# @option SessionLogPath -config sessionlogfile | # @option SessionLogPath -config sessionlogfile | ||
Line 32: | Line 36: | ||
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. |
- | $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xx-xx-xx@example.com/", | + | $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/", |
[Parameter(Mandatory = $True)] | [Parameter(Mandatory = $True)] | ||
$remotePath, | $remotePath, | ||
Line 41: | Line 45: | ||
[Switch] | [Switch] | ||
$pause, | $pause, | ||
+ | [Switch] | ||
+ | $refresh, | ||
$sessionLogPath = $Null, | $sessionLogPath = $Null, | ||
[Switch] | [Switch] | ||
Line 56: | Line 62: | ||
{ | { | ||
$newName = $file -replace $pattern, $replacement | $newName = $file -replace $pattern, $replacement | ||
- | Write-Host ("{0} => {1}" -f $file, $newName) | + | if ($newName -eq $file) |
- | ············if ($newName -ne $file) | + | |
{ | { | ||
+ | Write-Host "$file not changed" | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | Write-Host "$file => $newName" | ||
$anyChange = $True | $anyChange = $True | ||
} | } | ||
Line 64: | Line 74: | ||
Write-Host | Write-Host | ||
- | ········ | + | |
if (!$anyChange) | if (!$anyChange) | ||
{ | { | ||
Line 87: | Line 97: | ||
$continue = $True | $continue = $True | ||
} | } | ||
- | |||
if (!$continue) | if (!$continue) | ||
Line 98: | Line 107: | ||
$assemblyPath = if ($env:WINSCP_PATH) { $env:WINSCP_PATH } else { $PSScriptRoot } | $assemblyPath = if ($env:WINSCP_PATH) { $env:WINSCP_PATH } else { $PSScriptRoot } | ||
Add-Type -Path (Join-Path $assemblyPath "WinSCPnet.dll") | Add-Type -Path (Join-Path $assemblyPath "WinSCPnet.dll") | ||
- | ····· | + | |
# Setup session options | # Setup session options | ||
$sessionOptions = New-Object WinSCP.SessionOptions | $sessionOptions = New-Object WinSCP.SessionOptions | ||
$sessionOptions.ParseUrl($sessionUrl) | $sessionOptions.ParseUrl($sessionUrl) | ||
- | ····· | + | |
$session = New-Object WinSCP.Session | $session = New-Object WinSCP.Session | ||
- | · | + | |
try | try | ||
{ | { | ||
$session.SessionLogPath = $sessionLogPath | $session.SessionLogPath = $sessionLogPath | ||
- | # Connect | + | Write-Host "Connecting..." |
$session.Open($sessionOptions) | $session.Open($sessionOptions) | ||
+ | Write-Host "Renaming..." | ||
foreach ($file in $files) | foreach ($file in $files) | ||
{ | { | ||
$newName = $file -replace $pattern, $replacement | $newName = $file -replace $pattern, $replacement | ||
- | Write-Host ("{0} => {1}" -f $file, $newName) | + | if ($newName -eq $file) |
- | + | { | |
- | ···············$fullName = $session.CombinePaths($remotePath, $file) | + | ····················Write-Host "$file not changed" |
- | ···············$fullNewName = $session.CombinePaths($remotePath, $newName) | + | } |
- | ···············$session.MoveFile($fullName, $fullNewName) | + | else |
+ | ················{ | ||
+ | ····················Write-Host "$file => $newName" | ||
+ | ···················$fileMask = [WinSCP.RemotePath]::EscapeFileMask($file) | ||
+ | $sourcePath = [WinSCP.RemotePath]::Combine($remotePath, $fileMask) | ||
+ | ···················$operationMask = [WinSCP.RemotePath]::EscapeOperationMask($newName) | ||
+ | $targetPath = [WinSCP.RemotePath]::Combine($remotePath, $operationMask) | ||
+ | ···················$session.MoveFile($sourcePath, $targetPath) | ||
+ | } | ||
} | } | ||
} | } | ||
Line 128: | Line 146: | ||
} | } | ||
- | & "$env:WINSCP_PATH\WinSCP.exe" "$sessionUrl" /refresh "$remotePath" | + | if ($refresh) |
+ | { | ||
+ | ············& "$env:WINSCP_PATH\WinSCP.exe" "$sessionUrl" /refresh "$remotePath" | ||
+ | } | ||
} | } | ||
$result = 0 | $result = 0 | ||
} | } | ||
- | catch [Exception] | + | catch |
{ | { | ||
- | Write-Host $_.Exception.Message | + | Write-Host "Error: $($_.Exception.Message)" |
$result = 1 | $result = 1 | ||
} | } | ||
Line 159: | Line 180: | ||
Check the //Preview changes// checkbox to see and confirm the changes in file names before actually renaming the files. | Check the //Preview changes// checkbox to see and confirm the changes in file names before actually renaming the files. | ||
+ | |||
+ | In the //Session log file//, you can specify a path to a [[logging|session log file]]. The option is available on the [[ui_pref_commands|Preferences dialog]] only. | ||
+ | |||
+ | In the //Keyboard shortcut//, you can specify a [[custom_key_shortcuts|keyboard shortcut]] for the extension. The option is available on the [[ui_pref_commands|Preferences dialog]] only. | ||