Home>

After working hard again,Finally completed his more satisfactory version,Everyone criticize ...

Batch processing function:

for example:

I need to download the file of the specified directory from the server 202.33.22.102 to the machine every morning.Then you need to specify the following parameters

First, remote IP, is not null

Second, the ftp username, is not null

Third, the remote password,is not null

Fourth, which directory to download the files,If it is the root directory, enter a little bit,Ie ".." (without double quotes)

Five, sub directories, you may want to download multiple directories under a folder, right?If so, separate them with commas,If not, do not set anything (including spaces), please do not include spaces between folder names,Lest unnecessary trouble,When you specify a subdirectory,The program will only download files in that subdirectory,Do nothing to the next folder.

Six, the local directory,Please specify an existing directory,Including "\", the program will automatically create the same folder under this folder as on the server

Seven, log file directory,It already exists.

OK, you can now double-click this batch to execute the operation!

@echo off
echo ******** ============================================== ==============
echo ******** ============================================== ==============
echo ++++++++ batch function:remote data ftp backup to local
echo ++++++++ date:%date:~ 0,4%-%date:~ 5,2%-%date:~ 8,2%
echo ++++++++ author:zc_0101
echo ++++++++ email:[email protected]
echo ++++++++ qq:275374095
echo ++++++++ mark:
echo ++++++++ Please define the parameters strictly according to the parameter description,Input parameters
echo ++++++++ Please try to be as general as possible,Do not include special characters including spaces
echo ++++++++ skip if the file already exists,Does not support short point resume
echo ++++++++ does not check the integrity of the backup data,Users can modify it by themselves
echo ******** ============================================== ==============
echo ******** ============================================== ==============
pause
rem remote ip (make sure to enable server_u)
set remote_ip=103.111.124.123
rem remote login username (if there is a batch symbol,Please escape)
set remote_user=test
rem remote login password (if there is a batch symbol,Please escape)
set remote_pwd=fkhvbweos ^! sd4al %%
rem remote operation directory,If it is the root directory,Please fill in ".." (without double quotes), otherwise fill in the directory name directly,Databaseback
set remote_path=databaseback
rem list of directories to download remotely (note:only files in these directories are downloaded,Will not download files in subdirectories), please separate them with commas,If there are no subdirectories (make sure there are files available for download in the current directory), please do not fill in anything (including spaces);otherwise:please do not have spaces
set remote_directory=db_hxxl, db_oa, master, model, msdb
rem local backup directory,Please fill in an existing directory (the program will automatically create the same subdirectory under this directory as on the remote server,(Do not include "\" slash)
set local_path=d:\ dbback
rem operation log and temporary file storage path (be sure to bring "\" slash)
set log_path=d:\
rem gets the current date,As the log file name,This example is the log storage directory plus the date of the day
set today=``%log_path %%date:~ 0,4%-%date:~ 5,2%-%date:~ 8,2%-%time:~ 0,2 %%time:~ 3,2%%time:~ 6,2%.txt "
echo is doing basic checks on the parameters you enter ...
echo is detecting input parameters .........>%today%
if "%remote_ip%" equ "" (
  echo remote ip cannot be empty! Please return to assign remote_ip
  echo The remote IP is empty and the program exits.
>>%today%
  pause
  exit
)
if "%remote_user%" equ "" (
  echo remote login name cannot be empty! Please return to assign a value to remote_user
  echo remote login name is empty,The program exits.
>>%today%
  pause
  exit
)
if "%remote_pwd%" equ "" (
  The echo remote login password cannot be empty! Please return to assign a value to remote_pwd
  echo remote login password is empty,The program exits.
>>%today%
  pause
  exit
)
if "%remote_path%" equ "" (
  The echo remote operation directory cannot be empty! Please return to assign a value to remote_path, if it is operating on the root directory,Please enter ^ (Little bit ^), that is, ".."
  The echo remote operation directory is empty,The program exits.
>>%today%
  pause
  exit
)
if "%remote_directory%" equ "" (
  The echo remote subdirectory is wrong! Did you mistakenly assign a space for remote_directory?If there are no subdirectories, do not assign a value.
Include spaces.
  echo remote subdirectory is wrong,The program exits.
>>%today%
  pause
  exit
)
if "%local_path%\" equ "" (
  echo Local operation directory cannot be empty! Please return to assign a value to local_path
  echo local operation directory is empty,The program exits.
>>%today%
  pause
  exit
)
if "%log_path%" equ "" (
  The echo local log storage directory cannot be empty! Please return to assign a value to log_path
  echo local log storage directory is empty,The program exits.
>>%today%
  pause
  exit
)
The echo parameter basically passed the test,Prepare to perform database backup
The echo parameter basically passed the test,Database backup starts now>%today%
Echo today is%date%, the time is:%time:~ 0,2%:%time:~ 3,2%:%time:~ 6,2%, database backup is being performed .........
Echo today is%date%, the time is:%time:~ 0,2%:%time:~ 3,2%:%time:~ 6,2%, database backup is being performed .........>%today%
echo is creating an ftp file that generates a list of files, please wait ...
echo is creating an ftp file that generates a list of files, please wait ...>>%today%
ping -n 3 127.1>nul
echo is writing the remote login user name to the ftp file .........>%today%
echo%remote_user%>%log_path%command.txt
echo is writing the remote login password to the ftp file .........>>%today%
echo%remote_pwd%>>%log_path%command.txt
echo is writing the download method to the ftp file ...>>%today%
echo type binary>>%log_path%command.txt
echo is judging whether the remote parent directory is the root directory ............>%today%
if "%remote_path%" neq ".." (
  if "%remote_path%" neq "" (
    echo is writing a statement into the remote directory%remote_path%to the ftp file .........>%today%
    echo cd%remote_path%>>%log_path%command.txt
  )
)
echo is judging the remote subdirectory .........>>%today%
if "%remote_directory%" neq "" (
  echo is writing the generated statement of the subdirectory list to the ftp file .........>%today%
  for %%i in (%remote_directory%) do (
     echo is writing the generated statement of the subdirectory list %%i to the ftp file .........>%today%
     echo mls %%i%log_path %%%i_temp.txt>>%log_path%command.txt
  )
) else (
  echo did not fill in the subdirectory,Then write the generated statement of the file list in the root directory to the ftp file .........>%today%
  echo mls-%log_path%filelist_temp.txt>>%log_path%command.txt
)
echo close>>%log_path%command.txt
echo quit>%log_path%command.txt
echo is judging the command.txt file
if not exist "%log_path%command.txt \" (
  if exist "%log_path%command.txt" (
    echo%log_path%command.txt file created successfully
    echo%log_path%command.txt file created successfully>%today%
  ) else (
    echo%log_path%command.txt file creation failed,The program will exit.
    echo%log_path%command.txt file creation failed,Program exits>>%today%
    pause
    echo exit
  )
) else (
  echo There is even a folder named command.txt in the path where the parameters you entered are stored in the log.
Please delete it manually.
Run the program again.
  There is a folder named command.txt in the path where the parameters you entered are stored in the log.
The program exits.
>>%today%
  pause
  exit
)
echo is performing server directory file list creation,Please wait…………
echo is performing server directory file list creation,Please wait ...>>%today%
ping -n 3 127.1>nul
ftp -i -s:%log_path%command.txt%remote_ip%
echo is checking the list of server directory files that have been created,Please wait…………
echo is checking the list of server directory files that have been created,Please wait ...>>%today%
if "%remote_directory%" neq "" (
  for %%i in (%remote_directory%) do (
    if not exist "%log_path %%%i.txt \" (
      if exist "%log_path%\ %%i_temp.txt" (
        mshta "javascript:var fso=new activexobject (" scripting.filesystemobject ");fso.opentextfile ("%log_path%\ temp1.txt ", 2, true) .write (fso.opentextfile ("%log_path%\ %%i_temp .txt "). readall (). replace (/\ r \ n/g," \ n \ n "));close ()"
        mshta "javascript:var fso=new activexobject (" scripting.filesystemobject ");fso.opentextfile ("%log_path%\ temp2.txt ", 2, true) .write (fso.opentextfile ("%log_path%\ temp1.txt ") .readall (). replace (/\ r/g," "));close ()"
        mshta "javascript:var fso=new activexobject (" scripting.filesystemobject ");fso.opentextfile ("%log_path%\ %%i.txt ", 2, true) .write (fso.opentextfile ("%log_path%\ temp2 .txt "). readall (). replace (/\ n \ n/g," \ r \ n "));close ()"
      )
      if exist "%log_path %%%i.txt" (
        The echo%log_path %%%i.txt file was created successfully!
        The echo%log_path %%%i.txt file was created successfully!>>%today%
      ) else (
        echo Sorry, the creation of the%log_path %%%i.txt file failed! The program will exit!
        echo%log_path %%%i.txt file creation failed! Program Termination>>%today%
        pause
        exit
      )
    ) else (
      There is a folder named %%i.txt in the log path%log_path%where you entered the parameters.
Please delete it manually.
Run the program again.
      There is a folder named %%i.txt in the log path%log_path%where you entered the parameters.
The program exits.
>>%today%
      pause
      exit
    )
  )
) else (
  if not exist "%log_path%filelist.txt \" (
    if exist "%log_path%\ filelist_temp.txt" (
      mshta "javascript:var fso=new activexobject (" scripting.filesystemobject ");fso.opentextfile ("%log_path%\ temp1.txt ", 2, true) .write (fso.opentextfile ("%log_path%\ filelist_temp.txt ") .readall (). replace (/\ r \ n/g," \ n \ n "));close ()"
      mshta "javascript:var fso=new activexobject (" scripting.filesystemobject ");fso.opentextfile ("%log_path%\ temp2.txt ", 2, true) .write (fso.opentextfile ("%log_path%\ temp1.txt ") .readall (). replace (/\ r/g," "));close ()"
      mshta "javascript:var fso=new activexobject (" scripting.filesystemobject ");fso.opentextfile ("%log_path%\ filelist.txt ", 2, true) .write (fso.opentextfile ("%log_path%\ temp2.txt ") .readall (). replace (/\ n \ n/g," \ r \ n "));close ()"
    )
    if exist "%log_path%filelist.txt" (
    The echo%log_path%filelist.txt file was created successfully!
      The echo%log_path%filelist.txt file was created successfully!>>%today%
    ) else (
      echo Sorry, the creation of the%log_path%filelist.txt file failed!
      echo%log_path%filelist.txt file creation failed! Program Termination>>%today%
      pause
      exit
    )
  ) else (
    There is a folder named filelist.txt in the log path%log_path%where you entered the parameters.
Please delete it manually.
Run the program again.
    There is a folder named filelist.txt in the log path%log_path%where you entered the parameters.
The program exits.
>>%today%
    pause
    exit
  )
)
The echo program starts downloading the file,please wait patiently…………
The echo program starts downloading the file,Please be patient .........>%today%
if "%remote_directory%" neq "" (
  for %%i in (%remote_directory%) do (
    echo is creating the directory%local_path%\ %%i .........
    echo creates the directory%local_path%\ %%i ............>>%today%
    md%local_path%\ %%i
    echo is downloading the%remote_path%\ %%i folder backup,Please wait…………
    echo is downloading the%remote_path%\ %%i folder backup,Please wait ...>>%today%
    ping -n 3 127.1>nul
    for/f "delims =. tokens=1,2" %%j in (%log_path %%%i.txt) do (
      if not exist "%local_path%\ %%i \ %%j. %%k \" (
        if not exist "%local_path%\ %%i \ %%j. %%k" (
          echo is creating a script to download the file %%j. %%k, please wait ...
          echo is creating a script to download the file %%j. %%k, please wait .........>%today%
          echo%remote_user%>%log_path%getfiles.txt
          echo%remote_pwd%>>%log_path%getfiles.txt
          echo type binary>>%log_path%getfiles.txt
          if "%remote_path%" neq "" (
            if "%remote_path%" neq ".." (
              echo cd%remote_path%>>%log_path%getfiles.txt
            )
          )
          echo cd %%i>>%log_path%getfiles.txt
          echo lcd%local_path%\ %%i>>%log_path%getfiles.txt
          echo mget "%%j. %%k">>%log_path%getfiles.txt
          echo close>>%log_path%getfiles.txt
          echo quit>%log_path%getfiles.txt
          ftp -i -s:%log_path%getfiles.txt%remote_ip%
          echo is checking the file%local_path%\ %%i \ %%j. %%k, please wait ...
          echo is checking the file%local_path%\ %%i \ %%j. %%k, please wait ...>>%today%
          if exist "%local_path%\ %%i \ %%j. %%k \" (
            echo ---- Download%local_path%\ %%i \ %%j. %%k successfully!
            echo ---- Download%local_path%\ %%i \ %%j. %%k successfully!>>%today%
          ) else (
            echo-for unknown reasons,Downloading%local_path%\ %%i \ %%j. %%k failed!
            echo-for unknown reasons,Downloading%local_path%\ %%i \ %%j. %%k failed!>>%today%
          )
        ) else (
          echo%local_path%\ %%i \ %%j. %%k already exists and is being skipped ------
          echo%local_path%\ %%i \ %%j. %%k already exists and is being skipped->>>%today%
        )
      ) else (
        echo The parameters you entered are stored in the backup path%local_path%\ %%i. There is a folder named %%j. %%k.
Please delete it manually.
Run the program again.
        echo The parameters you entered are stored in the backup path%local_path%\ %%i.There is a folder named %%j. %%k.
The program exits.
>>%today%
        pause
        exit
      )
    )
  )
) else (
  echo Since no server subdirectory was specified,The program will back up the files in the server's parent directory to the local directory you specified ...
  echo Since no server subdirectory was specified,The program will back up the files in the server's parent directory to the local directory you specified .........>>%today%
  for/f "delims =. tokens=1,2" %%j in (%log_path%filelist.txt) do (
    if not exist "%local_path%\ %%j. %%k \" (
      if not exist "%local_path%\ %%j. %%k" (
        echo is creating a script to download the file %%j. %%k, please wait ...
        echo is creating a script to download the file %%j. %%k, please wait .........>%today%
        echo%remote_user%>%log_path%getfiles.txt
        echo%remote_pwd%>>%log_path%getfiles.txt
        echo type binary>>%log_path%getfiles.txt
        if "%remote_path%" neq "" (
          if "%remote_path%" neq ".." (
            echo cd%remote_path%>>%log_path%getfiles.txt
          )
        )
        echo lcd%local_path%>>%log_path%getfiles.txt
        echo mget "%%j. %%k">>%log_path%getfiles.txt
        echo close>>%log_path%getfiles.txt
        echo quit>%log_path%getfiles.txt
        ftp -i -s:%log_path%getfiles.txt%remote_ip%
        echo is checking the file%local_path%\ %%j. %%k, please wait ...
        echo is checking the file%local_path%\ %%j. %%k, please wait ...>>%today%
        if exist "%local_path%\ %%j. %%k" (
          echo ---- Download%local_path%\ %%j. %%k successfully!
          echo ---- Download%local_path%\ %%j. %%k successfully!>>%today%
        ) else (
          echo-for unknown reasons,Downloading%local_path%\ %%j. %%k failed!
          echo-for unknown reasons,Downloading%local_path%\ %%j. %%k failed!>>%today%
        )
      ) else (
        echo%local_path%\ %%j. %%k already exists and is being skipped ------
        echo%local_path%\ %%j. %%k already exists and is being skipped --- >>%today%
      )
    ) else (
      echo The parameters you entered are stored in the backup path%local_path%\. There is actually a folder named %%j. %%k.
Please delete it manually.
Run the program again.
      The parameter you entered is stored in the backup path%local_path%\. There is even a folder named %%j. %%k.
The program exits.
>>%today%
      pause
      exit
    )
  )
)
The echo program is clearing temporary files,Please wait…………
set temp_str=command.txt, temp1.txt, temp2.txt, getfiles.txt
for %%i in (%temp_str%) do (
  The echo program will delete the%log_path %%%i file .........>>%today%
  del%log_path %%%i
  if exist "%log_path %%%i" (
    echo%log_path %%%i file deletion failed,Program continues to run
    echo%log_path %%%i file deletion failed,Program continues to run>>%today%
  ) else (
    echo%log_path %%%i file deleted successfully
    echo%log_path %%%i file deleted successfully>>%today%
  )
)
The echo program is deleting the temporary file list file,Please wait…………
The echo program is deleting the temporary file list file,Please wait ...>>%today%
if "%remote_directory%" neq "" (
  for %%i in (%remote_directory%) do (
    del%log_path %%%i.txt,%log_path %%%i_temp.txt
    if exist "%log_path %%%i.txt" (
      echo%log_path %%%i.txt file deletion failed,Program continues to run
      echo%log_path %%%i.txt file deletion failed,Program continues to run>>%today%
    ) else (
      echo%log_path %%%i.txt file deleted successfully
      echo%log_path %%%i.txt file deleted successfully>>%today%
    )
  )
) else (
  del%log_path%filelist.txt,%log_path%filelist_temp.txt
  if exist "%log_path%filelist.txt" (
    echo%log_path%filelist.txt file deletion failed,Program continues to run
    echo%log_path%filelist.txt file deletion failed,Program continues to run>>%today%
  ) else (
    echo%log_path%filelist.txt file deleted successfully
    echo%log_path%filelist.txt file deleted successfully>%today%
  )
)
echo Congratulations, this backup job has been successfully completed! See the%today%log file for details!
echo Congratulations, this backup job has been successfully completed!>>%today%
The echo data has been backed up to the%local_path%\ directory.>>%today%
echo completion time:%time:~ 0,2%:%time:~ 3,2%:%time:~ 6,2%>>%today%
pause
exit
  • Previous Android context menu usage example analysis
  • Next JQuery entry basic small examples (1)