Differences
This shows you the differences between the selected revisions of the page.
| script_formatting_timestamp_batch_file 2016-09-29 | script_formatting_timestamp_batch_file 2023-04-24 (current) | ||
| Line 3: | Line 3: | ||
| ====== Formatting Timestamp in Batch File ====== | ====== Formatting Timestamp in Batch File ====== | ||
| - | While WinSCP is primarily an SFTP/FTP client, its powerful ''[[scripting#timestamp|%TIMESTAMP%]]'' syntax can be used as a separate feature from a Windows batch file. | + | While WinSCP is primarily an SFTP/FTP client, its powerful ''[[scripting#timestamp|%TIMESTAMP%]]'' syntax can be used as a separate feature in Windows batch files. |
| The ''%TIMESTAMP%'' syntax can be used as: | The ''%TIMESTAMP%'' syntax can be used as: | ||
| - | * A locale-independent alternative to the ''date'' command/variable ((The ''date'' command/variable uses locale-specific format, so common syntax like ''%%%date~...~%%'' yields expected ''%%yyyymmdd...%%'' value on US-locale only. | + | * A locale-independent alternative to the ''date'' command/variable; ((The ''date'' command/variable uses locale-specific format, so common syntax like '';<nowiki>%date:~10,4%%date:~4,2%%date:~7,2%</nowiki>'' yields expected ''%%yyyymmdd%%'' value on US-locale only.)) |
| * An easy way to calculate relative times, like yesterday date, tomorrow date, time one hour ago, etc. | * An easy way to calculate relative times, like yesterday date, tomorrow date, time one hour ago, etc. | ||
| Line 16: | Line 16: | ||
| <code batch> | <code batch> | ||
| set TIMESTAMP_FORMAT=yyyy-mm-dd | set TIMESTAMP_FORMAT=yyyy-mm-dd | ||
| - | for /F "tokens=* USEBACKQ" %%F in (`winscp.com /command "echo %%TIMESTAMP#%TIMESTAMP_FORMAT%%%" "exit"`) do set TIMESTAMP=%%F | + | |
| + | for /F "tokens=* USEBACKQ" %%F in ( | ||
| + | ····`winscp.com /command "echo %%TIMESTAMP#%TIMESTAMP_FORMAT%%%" "exit"` | ||
| + | ) do set TIMESTAMP=%%F | ||
| echo %TIMESTAMP% | echo %TIMESTAMP% | ||
| </code> | </code> | ||
| Line 22: | Line 26: | ||
| It outputs a current date in format ''2016-09-29'' on any locale. | It outputs a current date in format ''2016-09-29'' on any locale. | ||
| - | Of course, if you actually just need to print the timestamp (or output it a file), you do not need to do it indirectly via the environment variable, just use a simple: | + | Modify the ''TIMESTAMP_FORMAT'' as you need. For all options of the timestamp format, see the ''[[scripting#timestamp|%TIMESTAMP%]]'' syntax documentation. |
| + | |||
| + | Of course, if you actually just need to print the timestamp (or output it to a file), you do not need to do it indirectly via the environment variable, just use a simple: | ||
| <code batch> | <code batch> | ||
| Line 29: | Line 35: | ||
| </code> | </code> | ||
| - | ===== Calculating Relative Times ===== | + | ===== [[relative]] Calculating Relative Times ===== |
| To calculate relative times, you can extend the batch file like below. The script will print the yesterday date. | To calculate relative times, you can extend the batch file like below. The script will print the yesterday date. | ||
| - | <code> | + | <code batch> |
| set TIMESTAMP_FORMAT=yyyy-mm-dd | set TIMESTAMP_FORMAT=yyyy-mm-dd | ||
| set TIMESTAMP_RELATIVE=-1D | set TIMESTAMP_RELATIVE=-1D | ||
| - | for /F "tokens=* USEBACKQ" %%F in (`winscp.com /command "echo %%TIMESTAMP%TIMESTAMP_RELATIVE%#%TIMESTAMP_FORMAT%%%" "exit"`) do set TIMESTAMP=%%F | + | |
| + | for /F "tokens=* USEBACKQ" %%F in ( | ||
| + | ····`winscp.com /command "echo %%TIMESTAMP%TIMESTAMP_RELATIVE%#%TIMESTAMP_FORMAT%%%" "exit"` | ||
| + | ) do set TIMESTAMP=%%F | ||
| echo %TIMESTAMP% | echo %TIMESTAMP% | ||
| </code> | </code> | ||
| Line 42: | Line 52: | ||
| Change the value of the ''TIMESTAMP_RELATIVE'' variable to ''+1D'' to calculate tomorrow date. | Change the value of the ''TIMESTAMP_RELATIVE'' variable to ''+1D'' to calculate tomorrow date. | ||
| - | To calculate a time one hour ago, set the ''TIMESTAMP_RELATIVE'' to ''-1H'' and change the ''TIMESTAMP_FORMAT'' to include also a time a time, e.g. ''yyyy-mm-dd_hh:nn:ss''. | + | To calculate a time one hour ago, set the ''TIMESTAMP_RELATIVE'' to ''-1H'' and change the ''TIMESTAMP_FORMAT'' to include also a time, e.g. ''yyyy-mm-dd_hh:nn:ss''. |
| - | ===== More Options ===== | + | For all options of the relative date/time calculation, see the ''[[scripting#timestamp|%TIMESTAMP%]]'' syntax documentation. |
| - | For all options of the timestamp format and the relative date/time calculation, see the ''[[scripting#timestamp|%TIMESTAMP%]]'' syntax documentation. | + | ===== [[powershell]] In PowerShell ===== |
| + | |||
| + | In PowerShell, you can use its built-in timestamp functions. | ||
| + | |||
| + | To format the current timemestamp, use the ''[[ps>microsoft.powershell.utility/get-date|Get-Date]]'' cmdlet and its ''-Format'' switch: | ||
| + | |||
| + | <code powershell> | ||
| + | $timestamp = (Get-Date -Format "yyyy-MM-dd") | ||
| + | Write-Host $timestamp | ||
| + | </code> | ||
| + | |||
| + | To calculate relative times, use methods of the ''[[dotnet>system.datetime|DateTime]]'' class, like the ''[[dotnet>system.datetime.adddays|AddDays]]''. To format the calculated timestamp, use the ''[[dotnet>system.datetime.tostring#system-datetime-tostring(system-string)|ToString]]'' method. | ||
| + | |||
| + | <code powershell> | ||
| + | $yesterday = (Get-Date).AddDays(-1) | ||
| + | $timestamp = $yesterday.ToString("yyyy-MM-dd") | ||
| + | Write-Host $timestamp | ||
| + | </code> | ||