Add -RenamePattern/-RenameReplacement for renaming files before upload

This commit is contained in:
2026-04-16 21:34:20 -05:00
parent b2fed6c373
commit 6a451044dd
2 changed files with 69 additions and 3 deletions
+26
View File
@@ -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: