批处理文件 - 使用通配符匹配的多个FTP上载

时间:2016-02-23 10:07:04

标签: batch-file file-upload ftp

我需要通过FTP将一些文件(不是全部)从文件夹上传到远程服务器。

我编写了以下执行通配符匹配的代码,但我认为我遗漏了FTP所产生的文件。

备注:问题不是上传文件夹(相对简单),而是从给定文件夹中排除某些条目并上传所有其他文件。

我成功排除了这些文件并保留了其余文件,但我找不到上传后者的方法。

特别是,mput cmd似乎不适用于输入文件名。为什么?

我想专注于这个问题:如何提供FTP cmd以从上述过滤中上传每个结果文件?

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

2 个答案:

答案 0 :(得分:0)

这是解决方案,我几乎接近它:只需用ftp命令填写外部批处理.bat文件,然后在每个循环结束时调用此文件。删除它,然后使用列表中的下一个条目重新开始。 p.s。:对于任何进一步的使用,请注意在>>之前修剪任何空格。运算符,如下所示。

@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

) )

答案 1 :(得分:0)

下一个脚本可以提供帮助。阅读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%" 

示例输出:

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.