Differences

This shows you the differences between the selected revisions of the page.

library_example_advanced_rename 2019-07-25 library_example_advanced_rename 2023-01-11 (current)
Line 17: Line 17:
# @name        Batch &Rename... # @name        Batch &Rename...
# @command      powershell.exe -ExecutionPolicy Bypass -File "%EXTENSION_PATH%" ^ # @command      powershell.exe -ExecutionPolicy Bypass -File "%EXTENSION_PATH%" ^
-#                  -sessionUrl "!S" -remotePath "!/" -pattern "%Pattern%" ^ +#                  -sessionUrl "!E" -remotePath "!/" -pattern "%Pattern%" ^ 
-#                  -replacement "%Replacement%" -pause -sessionLogPath "%SessionLogPath%" ^+#                  -replacement "%Replacement%" -refresh -pause -sessionLogPath "%SessionLogPath%" ^
#                  %PreviewMode% !& #                  %PreviewMode% !&
# @description  Renames remote files using a regular expression # @description  Renames remote files using a regular expression
# @flag        RemoteFiles # @flag        RemoteFiles
-# @version      5+# @version      7
# @homepage    ~~SELF~~ # @homepage    ~~SELF~~
-# @require      WinSCP 5.14.2+# @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:"
Line 37: Line 37:
param ( param (
    # Use Generate Session 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-xxxxxxxxxxx...=@example.com/",+    $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/",
    [Parameter(Mandatory = $True)]     [Parameter(Mandatory = $True)]
    $remotePath,     $remotePath,
Line 45: Line 45:
    [Switch]     [Switch]
    $pause,     $pause,
 +    [Switch]
 +    $refresh,
    $sessionLogPath = $Null,     $sessionLogPath = $Null,
    [Switch]     [Switch]
Line 60: Line 62:
        {         {
            $newName = $file -replace $pattern, $replacement             $newName = $file -replace $pattern, $replacement
-            Write-Host "$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 68: Line 74:
        Write-Host         Write-Host
-········+
        if (!$anyChange)         if (!$anyChange)
        {         {
Line 91: Line 97:
        $continue = $True         $continue = $True
    }     }
-     
    if (!$continue)     if (!$continue)
Line 102: 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 "$file => $newName" +                if ($newName -eq $file) 
- +                { 
- ···············$fullName = [WinSCP.RemotePath]::Combine($remotePath, $file+                    Write-Host "$file not changed" 
- ···············$fullNewName = [WinSCP.RemotePath]::Combine($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 132: Line 146:
        }         }
-        & "$env:WINSCP_PATH\WinSCP.exe" "$sessionUrl" /refresh "$remotePath"+        if ($refresh) 
 +        { 
 +············& "$env:WINSCP_PATH\WinSCP.exe" "$sessionUrl" /refresh "$remotePath" 
 +        }
    }     }

Last modified: by martin