Add -RenamePattern/-RenameReplacement for renaming files before upload
This commit is contained in:
@@ -42,6 +42,8 @@ The script auto-searches these locations in order:
|
||||
| `-CredentialFile` | No | — | Path to saved credential XML (see below) |
|
||||
| `-KeyFilePath` | No | — | Path to SSH private key (`.ppk`) |
|
||||
| `-SshHostKeyFingerprint` | No | — | SSH host key fingerprint for verification |
|
||||
| `-RenamePattern` | No | — | Regex pattern to match in filename for renaming before upload |
|
||||
| `-RenameReplacement` | No | — | Replacement string for `-RenamePattern` (supports capture groups like `$1`) |
|
||||
| `-Recurse` | No | `false` | Scan subdirectories |
|
||||
| `-DeleteAfterTransfer` | No | `false` | Delete local files after successful upload |
|
||||
| `-DryRun` | No | `false` | Preview transfers without uploading |
|
||||
@@ -125,6 +127,30 @@ The `-FileFilter` parameter uses PowerShell regex (case-insensitive by default).
|
||||
-Recurse -FileFilter '\.pdf$'
|
||||
```
|
||||
|
||||
## Renaming Files Before Upload
|
||||
|
||||
Use `-RenamePattern` (regex) and `-RenameReplacement` together to rename files on the remote side without touching the local files.
|
||||
|
||||
| Goal | Pattern | Replacement |
|
||||
|------|---------|-------------|
|
||||
| Add date before extension | `'^(.+?)(\.[^.]+)$'` | `'${1}_20260416${2}'` |
|
||||
| Add prefix | `'^'` | `'processed_'` |
|
||||
| Add suffix before extension | `'^(.+?)(\.[^.]+)$'` | `'${1}_done${2}'` |
|
||||
| Strip `_draft` from name | `'_draft'` | `''` |
|
||||
| Replace spaces with underscores | `' '` | `'_'` |
|
||||
|
||||
Rename uses PowerShell's `-replace` operator (regex, case-insensitive). The local file is **not** modified — only the remote destination path changes.
|
||||
|
||||
### Add today's date to every filename
|
||||
```powershell
|
||||
$date = Get-Date -Format 'yyyyMMdd'
|
||||
.\Send-FilesToSftp.ps1 -LocalPath "C:\exports" -RemotePath "/incoming" `
|
||||
-HostName "sftp.example.com" -UserName "uploader" `
|
||||
-RenamePattern '^(.+?)(\.[^.]+)$' -RenameReplacement "${date}_`$1`$2"
|
||||
```
|
||||
|
||||
> **Tip:** Use `-DryRun` first to preview the renamed remote paths before committing to the transfer.
|
||||
|
||||
## SSH Host Key Fingerprint
|
||||
|
||||
For production use, always provide the host key fingerprint to prevent MITM attacks:
|
||||
|
||||
Reference in New Issue
Block a user