Add -AppendDate and -DateFormat parameters

Appends today's date to the remote filename before the extension
(e.g. export.txt → export_20260423.txt). Applies after -RenamePattern
if both are used. Works with -DryRun for preview.
This commit is contained in:
blance
2026-04-23 20:44:29 -05:00
parent 8505e75757
commit 32b265876c
2 changed files with 55 additions and 2 deletions
+26
View File
@@ -49,6 +49,8 @@ The script auto-searches these locations in order:
| `-LocalRenameReplacement` | No | — | Replacement string for `-LocalRenamePattern` (supports capture groups like `$1`) |
| `-Recurse` | No | `false` | Scan subdirectories |
| `-DeleteAfterTransfer` | No | `false` | Delete local files after successful upload. Cannot combine with `-ArchivePath` |
| `-AppendDate` | No | `false` | Append today's date to the remote filename before the extension (e.g. `export.txt``export_20260423.txt`) |
| `-DateFormat` | No | `yyyyMMdd` | Date format string used with `-AppendDate`. Any valid `Get-Date` format accepted |
| `-DryRun` | No | `false` | Preview transfers without uploading |
| `-LogFile` | No | — | Path to log file (logs to console if omitted) |
| `-WinScpDllPath` | No | — | Explicit path to `WinSCPnet.dll` |
@@ -158,6 +160,30 @@ After a successful upload you can archive or rename the local source file. These
> `-LocalRenamePattern`/`-LocalRenameReplacement` only rename the local file — the remote name is controlled by `-RenamePattern`/`-RenameReplacement`. You can use both together.
## Appending the Date to Filenames
Use `-AppendDate` to automatically insert today's date before the file extension on the remote side. The local file is not modified.
```powershell
# Upload every export.txt in all subfolders, renamed with today's date
.\Send-FilesToSftp.ps1 -LocalPath "C:\jobs" -RemotePath "/incoming" `
-HostName "sftp.example.com" -UserName "uploader" `
-FileFilter '^export\.txt$' -Recurse -AppendDate
```
Result: `export.txt``export_20260423.txt`
Use `-DateFormat` to change the format:
```powershell
# Use dashes instead: export_2026-04-23.txt
.\Send-FilesToSftp.ps1 -LocalPath "C:\jobs" -RemotePath "/incoming" `
-HostName "sftp.example.com" -UserName "uploader" `
-FileFilter '^export\.txt$' -Recurse -AppendDate -DateFormat 'yyyy-MM-dd'
```
`-AppendDate` applies after `-RenamePattern` if both are used, and works with `-DryRun` so you can preview the result first.
## Renaming Files on the Remote Side
Use `-RenamePattern` (regex) and `-RenameReplacement` together to rename files on the remote side without touching the local files.