Create a folder according to today's date and copy files to a new folder using batch files

I have the following code that creates a folder based on today's date and moves a file from the folder to a 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 does not copy files to the newly created folder. The files are copied from the FTP server to the new folder. Please suggest some changes that copy these files.

Thank!

+3


source to share


2 answers


Your code is correct at all (except for misuse of the variable date

as @Jeff pointed out).

You probably only have the wrong spaces around ^

.

  • ^

    must be the very last character on the line. You seem to have spaces after ^

    on lines with lcd

    and get

    .
  • The line following ^

    must start with a space. You have no spaces in the following lines.

See the WinSCP FAQ Why are some of the script commands specified on the WinSCP command line in a batch file not / are not executed?



Also, you shouldn't be assigning to a variable date

. You don't even have to use it this way to format the timestamp like a variable format date

in a language binding. See Create a file name as a timestamp in a batch job . Thus, you are better off using a different approach.

WinSCP itself supports date formatting with syntax %TIMESTAMP%

.

The following code fixed the issues with ^

and uses WinSCP to reliably format the date:

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" 

      

+2


source


I don't have WinSCP so this hasn't been tested, 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" 

      

+1


source







All Articles