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
  • 749
  • 8
  • 23
  • 1
    `date` is a rserved system variable. Change it at your own risk! – Magoo Apr 21 '17 at 12:29
  • @Magoo How am i changing the date here? I am just using it to name my folder. Thanks for that though :/ – qwerty Apr 21 '17 at 12:33
  • 2
    Your first line is assigning to the variable date. It is a system variable, and should be treated as though it is read-only. – Jeff Zeitlin Apr 21 '17 at 12:36
  • @Jeff My first line is just changing the format of the date. Do you mean to say if i don't change the format I can create a folder with "MM\DD\YYYY"? – qwerty Apr 21 '17 at 12:39
  • If you assign to `date`, you are potentially changing the system date, not merely the format of the date. Instead of assigning the new format to `date`, and then assigning `date` to `mydir`, simply fold the two into a single assignment: `set mydir="%date:~7,2%%date:~4,2%%date:~10,4%"`. If this produces a valid string for a file name, you can then use it to create the folder. – Jeff Zeitlin Apr 21 '17 at 12:45
  • @JeffZeitlin Yes, i also tried that, it just creates the folder but doesnt copy the file. Regarding changing the system date.. I am not doing so because i am assigning the date variable as the changed format of the date (i get what youre saying about folding them in mydir) – qwerty Apr 21 '17 at 12:46
  • The copying of the file is a different issue, and one that I'm not qualified to address, as I'm not familiar with WinSCP. I will, however, suggest that the problem might be in the `get` line; typically, `>` means "redirect", and that may be causing something to be overridden so that your file isn't being put where you expect. – Jeff Zeitlin Apr 21 '17 at 12:48
  • Alright, Thanks for your insight! :) – qwerty Apr 21 '17 at 12:55
  • is the date folder created on the same system you are ftp`ing the file to? – Gerhard Apr 21 '17 at 13:17
  • *"This code creates a folder but doesn't copy any files to the newly created folder."* - So what does it do? Show use script output and log file (`"C:\Program Files (x86)\WinSCP\WinSCP.com" /log=winscp.log`) – Martin Prikryl Apr 21 '17 at 13:18
  • @GerhardBarnard No its created on the local machine. – qwerty Apr 21 '17 at 13:43
  • @MartinPrikryl It shows a error that the get and exit function could not be found – qwerty Apr 21 '17 at 13:43
  • @Rahul Sure, so use the fixed code in my answer. – Martin Prikryl Apr 21 '17 at 13:45
  • @Rahul Also, did you put the commands to a .bat file, right? – Martin Prikryl Apr 21 '17 at 14:27
  • @JeffZeitlin, assigning to `date` does *not* change the system date, it merely overwrites the system's `date` variable with a normal one, so you will no longer get the system date but the specific string you assigned when doing `echo %date%`; try `set "DATE=String"`, then `echo %DATE%`, and you will get `String`; do `set "DATE="`, then `echo %DATE%` again, and you will get the system date again... – aschipfl Apr 21 '17 at 15:45
  • Related: [How to get current datetime on Windows command line, in a suitable format for using in a filename?](http://stackoverflow.com/q/203090) – aschipfl Apr 21 '17 at 15:54

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
  • 167,268
  • 50
  • 405
  • 846
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
  • 21,163
  • 7
  • 24
  • 41
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
  • 167,268
  • 50
  • 405
  • 846
Saad Awan
  • 448
  • 2
  • 6
  • 19