4

I have the following code that creates a folder based on today's date and moves the file from a folder to the new one. I have the following code:

set date="%date:~7,2%%date:~4,2%%date:~10,4%"
set mydir=%date%
cd "C:\Users\rnan\Desktop\Batch Files\Tess\File History\"
mkdir "C:\Users\rnan\Desktop\Batch Files\Tess\File History\%mydir%"
"C:\Program Files (x86)\WinSCP\WinSCP.com" /command ^
"open ftp://rnan:J13@Files8.cyberlynk.net/tess/" ^
"lcd ""C:\Users\rnan\Desktop\Batch Files\Tess\File History\%mydir%""" ^ 
"get *.csv>1D" ^ 
"exit" 

This code creates a folder but doesn't copy any files to the newly created folder. The files are being copied from FTP server to the new folder. Please suggest some changes that copies those files.

Thanks!

qwerty
  • 887
  • 11
  • 33

3 Answers3

2

Your code is correct in general (except for the date variable misuse, as @Jeff pointed out).

You most probably have just wrong white spaces around the ^.

  • The ^ has to be the very last character on the line. You seem to have spaces after ^ on the lines with lcd and get.
  • The line following the ^ has to start with a space. You do not have any spaces on the following lines.

See WinSCP FAQ Why are some scripting commands specified on WinSCP command-line in a batch file not executed/failing?

Also not only you should not assign to date variable. You should not even use it this way to format timestamp, as the format of the date variable in locale-specific. See Creating a file name as a timestamp in a batch job. So, you better use another approach.

WinSCP itself supports date formatting using its %TIMESTAMP% syntax.

The following code fixed the issues with ^ and uses WinSCP to format date reliably:

set TIMESTAMP_FORMAT=yyyy-mm-dd
cd "C:\Program Files (x86)\WinSCP"
for /F "tokens=* USEBACKQ" %%F in (`WinSCP.com /command "echo %%TIMESTAMP#%TIMESTAMP_FORMAT%%%" "exit"`) do set TIMESTAMP=%%F
set mydir=C:\Users\rnan\Desktop\Batch Files\Tess\File History\%TIMESTAMP%
mkdir "%mydir%"
WinSCP.com /command ^
    "open ftp://rnan:J13@Files8.cyberlynk.net/tess/" ^
    "lcd ""%mydir%""" ^
    "get *.csv>1D" ^
    "exit" 
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
1

I do not have WinSCP installed, so this is untested, but why not try this?

set datetime=%date:~7,2%%date:~4,2%%date:~10,4%
set "mydir=C:\Users\rnan\Desktop\Batch Files\Tess\File History\%datetime%"
mkdir "%mydir%"
"C:\Program Files (x86)\WinSCP\WinSCP.com" /command ^
   "open ftp://rnan:J13@Files8.cyberlynk.net/tess/" ^
   "lcd ""%mydir%""" ^
   "get *.csv>1D" ^
   "exit" 
Gerhard
  • 22,678
  • 7
  • 27
  • 43
1

Here is a batch script which first creates a folder with name date&time and after that copies files from source folder and passes it on to the new folder:

echo off

for /f %%# in ('wMIC Path Win32_LocalTime Get /Format:value') do @for /f %%@ in ("%%#") do @set %%@
set CUR_YYYY=%year%
set CUR_MM=%month%
set CUR_DD=%day%


set CUR_HH=%time:~0,2%


set SOURCE=%C:\Work\Deployed-Content%
set TARGET=%C:\Work\%

if %CUR_HH% lss 10 (set CUR_HH=0%time:~1,1%)

set CUR_NN=%time:~3,2%
set CUR_SS=%time:~6,2%
set CUR_MS=%time:~9,2%


set FOLDERNAME=%CUR_YYYY%.%CUR_MM%.%CUR_DD%-%CUR_HH%-%CUR_NN%-%CUR_MS%
mkdir %FOLDERNAME%

robocopy %SOURCE%  %TARGET%%FOLDERNAME% /mir

echo Over and out.
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Saad Awan
  • 566
  • 2
  • 9
  • 23