1

I need to upload via FTP some files (not all) from a folder to a remote server.

I coded the following which performs the wildcard match, but I think I am missing something to FTP the resulting files.

Remark: the problem is not to upload the folder (which is relatively simple), but to exclude some entries from the given folder and upload all other files.

I was successful in excluding those files and keep the rest, but I can't find the way to upload the latter.

In particular, mput cmd seems to not work with the input filename. Why ?

I want to focus on this question: how can I feed the FTP cmds to upload each resulting file from the above filtering ?

echo off
setlocal enabledelayedexpansion
FOR /R localfolder %%F IN (*.*) DO (
    set fname=%%~nF
    set ext=%%~xF
    set filename=!fname!!ext!
    set subfname=!fname:~0,4!
    IF NOT "!subfname!" == "idat" (
        echo ftp
        echo open ftp.something.it
        echo ftpuser
        echo ftppass
        echo lcd localfolder
        echo cd remotefolder
        echo binary
        echo mput !filename!
        echo disconnect
        echo bye
        )
    )
    pause
Sandro Rosa
  • 507
  • 4
  • 12

2 Answers2

0

This is the solution, I was nearly close to it: just fill-in an external batch .bat file with ftp commands and then call this file at the end of each loop. Delete it and start again with the next entry on the list. p.s.: for any further usage, beware of trimming any blank spaces before >> operator, as shown below.

@echo off
setlocal enabledelayedexpansion
FOR /R localfolder %%F IN (*.*) DO (
set fname=%%~nF
set ext=%%~xF
set filename=!fname!!ext!
set subfname=!fname:~0,4!
IF NOT "!subfname!" == "idat" (
echo open ftp.something.it>> ftp.cmds.bat
echo username>> ftp.cmds.bat
echo password>> ftp.cmds.bat
echo lcd localfolder>> ftp.cmds.bat
echo cd remotefolder>> ftp.cmds.bat
echo binary>> ftp.cmds.bat
echo mput !filename!>> ftp.cmds.bat
echo disconnect>> ftp.cmds.bat
echo bye>> ftp.cmds.bat
ftp -i -s:ftp.cmds.bat
del ftp.cmds.bat

) )

Sandro Rosa
  • 507
  • 4
  • 12
0

Next script could help. Read List of FTP commands for the Microsoft command-line FTP client

@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
set "localfolder=D:\test\31441809\root"   change
set "lcd-folder=%localfolder%"
set "excludeFile=nico"                    change to `idat`
set "ftpscript=%temp%\35574454ftp.dat"    change 
(
  echo open ftp.something.it
  echo user ftpuser
  echo ftppass
  echo binary
  echo cd remotefolder
  echo lcd %lcd-folder%
  FOR /R %localfolder% %%F IN (*.*) DO (
      set "fname=%%~nF"
      set "ext=%%~xF"
      set "filename=!fname!!ext!"
      set "subfname=!fname:~0,4!"
      IF /I NOT "!subfname!"=="%excludeFile%" (
        if /I not "!lcd-folder!\"=="%%~dpF" (
          rem change local directory only if necessary
          set "lcd-folder=%%~dpF"
          set "lcd-folder=!lcd-folder:~0,-1!"     remove trailing backslash
          echo lcd !lcd-folder!
        )
        echo put !filename!
      )
  )
  echo disconnect
  echo bye
)>"%ftpscript%"
type "%ftpscript%"
pause
ftp -i -n -s:"%ftpscript%" 

Sample output:

d:\bat> D:\bat\SO\35574454.bat
open ftp.something.it
user ftpuser
ftppass
binary
cd remotefolder
lcd D:\test\31441809\root
put JohnDoe.txt
lcd D:\test\31441809\root\Ian-ionescu
put Ian-ionescuY.txt
lcd D:\test\31441809\root\John-doe
put John-doe.txt
put John-doeA.txt
lcd D:\test\31441809\root\Nicola-sheperd
put SheperdNicola.txt
lcd D:\test\31441809\root\Sara-smith
put Sara-smith.txt
put Sara-smithZ.txt
disconnect
bye
Press any key to continue . . .
ftp> open ftp.something.it
Connected to www.something.it.
220 FTP Server ready.
ftp> user ftpuser
331 Password required for ftpuser.
etc.etc.
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • Thank you JosefZ for your solution. Luckily, I had been successful in finding it too. They are similar and thus I'm glad cause this approach seems to be the only affordable one. – Sandro Rosa Feb 23 '16 at 21:56