0

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%)?

Martin Prikryl
  • 147,050
  • 42
  • 335
  • 704
Majed
  • 907
  • 6
  • 18

2 Answers2

1

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

Martin Prikryl
  • 147,050
  • 42
  • 335
  • 704
0

I prefer using ISO 8601 date formats (sometimes minus hyphens), as that makes ordering files by date easier (simply order alphabetically by folder name). Generally, I find ISO 8601 dates with hyphens easier to read than those without, but your requirements may vary.

Also, ISO 8601 avoids the silliness from across the pond where days and months are switched around - this confuses those of us who use international standards no end.

The ISO 8601 format used most often is YYYY-MM-DD or YYYYMMDD (e.g., 2016-02-24 or 20150224 for today's date).

Using wmic os get LocalDateTime, you can get non-locale-specific datetime from the operating system, and then convert it to ISO 8601 format, as demonstrated here:

@echo off
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldtime=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2% %ldt:~8,2%:%ldt:~10,2%:%ldt:~12,6%
set ldate=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2%
set ldatestraight=%ldt:~0,8%
echo Local datetime is [%ldtime%]
echo Local date is [%ldate%]
echo Local date no hyphens is [%ldatestraight%]
echo .
echo Raw datetime is [%ldt%]
pause

So, taking Martin Prikryl's sample code a step further, you could do something like this (for ISO 8601 dates with hyphens):

(
    echo open 127.0.0.1
    echo user
    echo pass
    for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
    set ldate=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2%
    echo mkdir %ldate%
    echo bye
) > ftpMyFiles.txt

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

Kudos to Anerty and Jay for their answer to a similar question that helped me a little while back, here.