Differences
This shows you the differences between the selected revisions of the page.
script_formatting_timestamp_batch_file 2017-10-16 | 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 24: | Line 28: | ||
Modify the ''TIMESTAMP_FORMAT'' as you need. For all options of the timestamp format, see the ''[[scripting#timestamp|%TIMESTAMP%]]'' syntax documentation. | 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 a file), you do not need to do it indirectly via the environment variable, just use a simple: | + | 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 31: | 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. | ||
Line 38: | Line 42: | ||
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 44: | 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''. |
For all options of the relative date/time calculation, see the ''[[scripting#timestamp|%TIMESTAMP%]]'' syntax documentation. | For all options of the relative date/time calculation, see the ''[[scripting#timestamp|%TIMESTAMP%]]'' syntax documentation. | ||
Line 59: | Line 67: | ||
</code> | </code> | ||
- | To calculate relative times, use methods of the ''[[https://msdn.microsoft.com/en-us/library/system.datetime.aspx|DateTime]]'' class, like the ''[[https://msdn.microsoft.com/en-us/library/system.datetime.adddays.aspx|AddDays]]''. To format the calculated timestamp, use the ''[[https://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx|ToString]]'' method. | + | 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> | <code powershell> |