Search code examples
batch-filecommand-linewinscp

use variable in command line batch file


I use the following command in a batch file:

winscp sitename /command "get /filename.160313.tgz q:\localfolder\"

which downloads the file using winscp program.

What I'd like to do is have "160313" be automatically generated based on today's date.

How would I replace 160313 with such variable?


Solution

  • @echo off
    for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
    set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
    set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
    
    echo %YY%%MM%%DD%
    
    winscp sitename /command "get /filename.%YY%%MM%%DD%.tgz q:\localfolder\"