使用参数调用另一个.bat中的.bat文件

时间:2021-09-12 16:03:06

I need to upload zip files to an FTP server.

我需要将zip文件上传到FTP服务器。

My first file looks like this:

我的第一个文件看起来像这样:

sqlcmd -S .\SQLEXPRESS -U <user> -P <pass> -i c:\sql_script.sql
7za a -tzip %~dp0\Archive\Backup_daily_full.zip *.bak -v100m

For uploading a single file I created this:

为了上传我创建的单个文件:

@echo off
echo user {user}> ftpcmd.dat
echo {pass}>> ftpcmd.dat
echo put %1>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat  {server}

I don't know how many .zip files I will have after the backup and how to upload all of them on FTP (how to call that file within main .bat) OR how to simply upload them all at once.

我不知道备份后我将拥有多少个.zip文件,以及如何在FTP上上传所有这些文件(如何在主.bat中调用该文件)或者如何一次性上传所有文件。

2 个解决方案

#1


0  

You can loop over all the .zip files in the folder, and run the ftp script for each of them.

您可以遍历文件夹中的所有.zip文件,并为每个文件运行ftp脚本。

@echo off
for /r %~dp0\Archive %%a in (*.zip) do (
echo user {user}> ftpcmd.dat
echo {pass}>> ftpcmd.dat
echo put %%a>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat  {server}
)

#2


0  

Add a third line to your first file:

在第一个文件中添加第三行:

cmd /c <your-ftp-script>.bat <zip-file-name>

As for traversing through a list of zip files, do as the manual says (cmd /?):

至于遍历zip文件列表,请按手册说(cmd /?):

FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]

FOR / R [[drive:] path]%变量IN(设置)DO命令[命令参数]

Walks the directory tree rooted at [drive:]path, executing the FOR statement in each directory of the tree. If no directory specification is specified after /R then the current directory is assumed. If set is just a single period (.) character then it will just enumerate the directory tree.

遍历以[drive:]路径为根的目录树,在树的每个目录中执行FOR语句。如果在/ R之后没有指定目录规范,则假定当前目录。如果set只是一个句点(。)字符,那么它将枚举目录树。

So store your zipped backup files at a clean location, then use for /r to go through the list, calling your second bat file in a loop.

因此,将压缩后的备份文件存储在干净的位置,然后使用/ r遍历列表,循环调用第二个bat文件。

#1


0  

You can loop over all the .zip files in the folder, and run the ftp script for each of them.

您可以遍历文件夹中的所有.zip文件,并为每个文件运行ftp脚本。

@echo off
for /r %~dp0\Archive %%a in (*.zip) do (
echo user {user}> ftpcmd.dat
echo {pass}>> ftpcmd.dat
echo put %%a>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat  {server}
)

#2


0  

Add a third line to your first file:

在第一个文件中添加第三行:

cmd /c <your-ftp-script>.bat <zip-file-name>

As for traversing through a list of zip files, do as the manual says (cmd /?):

至于遍历zip文件列表,请按手册说(cmd /?):

FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]

FOR / R [[drive:] path]%变量IN(设置)DO命令[命令参数]

Walks the directory tree rooted at [drive:]path, executing the FOR statement in each directory of the tree. If no directory specification is specified after /R then the current directory is assumed. If set is just a single period (.) character then it will just enumerate the directory tree.

遍历以[drive:]路径为根的目录树,在树的每个目录中执行FOR语句。如果在/ R之后没有指定目录规范,则假定当前目录。如果set只是一个句点(。)字符,那么它将枚举目录树。

So store your zipped backup files at a clean location, then use for /r to go through the list, calling your second bat file in a loop.

因此,将压缩后的备份文件存储在干净的位置,然后使用/ r遍历列表,循环调用第二个bat文件。