Search code examples
windowsbatch-filecommand-linecmdftp

Can I create a folder with current datetime when I use Windows Command-line FTP


Is there any possibility to create a new directory with current datetime when I use the Windows Command-line FTP?

for more clarification if I have scheduler to run this:

C:\windows\system32\ftp -s:ftpMyFiles.txt

And inside ftpMyFiles.txt I have:

open 127.0.0.1
user
pass
mkdir %DATE%
bye

here is the question Can I create A new directory with a datetime here (mkdir %DATE%)?


Solution

  • You have to generate the ftpMyFiles.txt dynamically like:

    (
        echo open 127.0.0.1
        echo user
        echo pass
        echo mkdir %DATE%
        echo bye
    ) > ftpMyFiles.txt
    
    C:\windows\system32\ftp -s:ftpMyFiles.txt
    

    Note that the value of %DATE% is locale-specific. So make sure you test your batch file on the same locale you are going to actually run it. Otherwise you may get unexpected results. For example on my (Czech) locale the DATE=po 13. 04. 2015


    You can achieve the same easier and more reliably using WinSCP scripting. It supports:

    1. Specifying the commands directly on its command-line
    2. %TIMESTAMP% syntax.
    winscp.com /ini=nul /command ^
        "open ftp://user:[email protected]/" ^
        "mkdir %%TIMESTAMP#yyyymmdd%%" ^
        "exit"
    

    (I'm the author of WinSCP)