Call .bat file within another .bat with parameters - batch-file

I need to upload zip files to an FTP server.
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.

You can loop over all the .zip files in the folder, and run the ftp script for each of them.
#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}
)

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 /?):
FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]
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.
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.

Related

Copy command has stopped working after merging batch files

I inherited a set of batch files.
Bat1 called bat2 which called bat3.
Bat3 created an xml, which bat2 used to create an html and bat1 used that to create a pdf.
Finally bat1 copied the pdf to an output directory.
I've recently joined the three batch files into one and everything works exactly the same up to the final copy command, which fails stating the pdf cannot be found.
#echo off
pushd %~dp0
set sourceFile=%1
set xslt=%2
set outputFile=%3
for %%i in (%sourceFile%) do (
set documentName=%%~ni
)
::BATCH FILE 2 CALLED
::BATCH FILE 3 CALLED
echo Processing source file compose
java -cp lib\saxon9.jar; net.sf.saxon.Transform -s:%sourceFile% -xsl:conversion\compose.xsl -o:compose.xml
echo Processing source file catalog
java -cp lib\saxon9.jar; -s:compose.xml -xsl:conversion\catalog.xsl -o:catalog.xml
::END OF BATCH FILE 3
echo Processing source file to HTML
java -cp lib\saxon9.jar; net.sf.saxon.Transform -s:catalog.xml -xsl:conversion\html5.xsl -o:index.html
echo Bundling publication
mkdir %documentName%
xcopy /y "conversion\webapp\*" %documentName%\ /o /x /e /h /k
move index.html %documentName%\
copy source graphics into web application img res dir
for %%f in ("*.jpg" "*.png" "*.gif" "*.svg" "*.psd" "*.ai" "*.pdf") do copy "%%f" %documentName%\asset\img\
::END OF BATCH FILE 2
echo Running AHF
AHFCmd -extlevel 4 -d %documentName%\index.html -o %documentName%.pdf -f HTML -x 4 -i tool\AHF\AHFSettings(x64).xml -stdout
::EVERYTHING WORKS FINE TO HERE; %documentName%.pdf is created just as it was previously.
echo Moving %documentName%.pdf
copy %documentName%.pdf %outputFile% ::Error - The system cannot find the file specified.
popd
The line copy %documentName%.pdf %outputFile% hasn't changed. I have tried using CALL in front of the AHFCmd and JAVA commands but that makes no difference. I have tried dir /b /a-d just before the copy command and it lists the file I want to move in the current directory. What am I doing wrong?

CMD/FTP to create folder using to today date & connect ftp download into created folder

I'm new to this cmd/FTP command. I would like to create a new folder at my local directory using today's date and connect to FTP to download the specific file to the newly created folder. If I manually type in command one by one at cmd, it has no issue. But when I use a batch file to run, my command stopped at FTP.
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4"_"job%
mkdir C:\%name%
cd C:\%name%
ftp
open 192.168.31.93
*user*
*password*
binary
cd *directory*
mget -i *.*
I did try to separate my command to two batches;
1. folder creation
2. FTP download but the file downloaded didn't go into the folder I created. the downloaded file went to C:\Document & Settings.
main batch file
#echo off
call rename.bat
ftp -i -s:ftp.txt
rename.bat
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"_job"
mkdir c:\%name%
cd c:\%name%
ftp.txt
open 192.168.31.93
*user*
*password*
binary
cd *directory*
mget *.*
close
Another method I try is using '!' when in FTP environment, then create a folder then exit back to FTP environment. This method again doesn't work with the batch file. Please help
It seems that with command extensions enabled, the working directory set by a child batch file is lost, then the batch file exits.
I'm not sure how to solve it, but you actually do not need the rename.bat file to be a separate file. This "main batch file" should work:
#echo off
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"_job"
mkdir c:\%name%
cd /d c:\%name%
ftp -i "-s:%~dp0\ftp.txt"
Also note the /d added to cd. Without that your batch will not work when started from another drive. You also have to use %~dp0 to refer to the batch file folder for the ftp.txt. As at the time ftp is called, you have changed to the target directory.
You possibly do not even need the command extensions to be enabled. So simply removing the setlocal enableextensions might solve the problem too. Though you still need the %~dp0 and /d.
I've decided to post this, although similar to the answer given, there are a couple of differences.
It creates the text file, then deletes it, (this keeps everything more portable).
I have corrected your directory name, (because of a typo).
#Echo Off
Set "Name=%DATE%"
Set "Name=%Name:~-10,2%-%Name:~-7,2%-%Name:~-4%_job"
MD "C:\%Name%" 2>Nul
CD /D "C:\%Name%" || Exit /B
( Echo open 192.168.31.93
Echo *user*
Echo *password*
Echo binary
Echo cd *directory*
Echo mget *.*
Echo close
)>"ftp.txt"
FTP -i -s:ftp.txt
Del "ftp.txt" 2>Nul
Exit /B

windows batch script to unzip a file

I am trying to create a batch file that gets a zipped folder for that particular date (v_date) from the sftp site and then unzip them. The zip file contains five files that are text documents. I have written batch scripts that successfully get the zip file from the remote site and save a copy of it on the local drive. I need to incorporate the unzip part in to my script.
SET v_date=%1
if [%v_date%] == [] SET v_date=%date:~10,4%%date:~4,2%%date:~7,2%
echo option batch continue>FTP_File_Get.txt
echo option confirm off>>FTP_File_Get.txt
echo open Target>>FTP_File_Get.txt
echo lcd "M:\Development\Data History\File" >> FTP_File_Get.txt
echo cd /Export/File >> FTP_File_Get.txt
echo get /Export/File/Filename_%v_date%.zip "M:\Development\DataHistory\Filename_%v_date%.zip">>FTP_File_Get.txt
echo exit>>FTP_File_Get.txt
M:\temp\apps\WinSCP\winscp.com/script="M:\Development\SFTPBatchFiles\FTP_File_Get.txt"
del FTP_File_Get.txt
This is my code to UNZIP:
SET v_date=%1
if [%v_date%] == [] SET v_date=%date:~10,4%%date:~4,2%%date:~7,2%
cd "M:\Development\Data History\"
::SET v_file="M:\Development\Data History\Filename_%v_date%.zip"
::unzip -o %v_file%
"C:\Program Files\7-Zip\7z.exe" e "Filename_%v_date%.zip"
I need to move the extracted files (6 Files) into their respective folders, Any help is greatly appreciated
To unzip the files you can use this command line:
"C:\Program Files\7-Zip\7z.exe" e "filename.zip"
#echo off
set "source=%userprofile%\Desktop\basanta\Automation\a"
set "target=%userprofile%\Desktop\basanta\Automation\b"
FOR %%A IN ("%source%\*.gz") DO (
"%programfiles%\7-zip\7z.exe" x "%%~A" -o"%target%\%%~pA"
del "%%~A" /Y
)
Use the above code by saving it to .bat file extension
Remember %userprofile% is for the directory, %programfiles% for the program files set as a variable in windows
hope that helps

AutoIt or batch for FTP transfer to on multiple servers

I must log in to multiple servers and upload some files. Is there a chance to code this with AutoIt?
I have tested it with an batch file, but doesn't work:
echo open < /FTP/ftphost.txt
echo < /FTP/ftpuser.txt
echo < /FTP/ftppass.txt
echo cd C:\Dokumente und Einstellungen\Administrator\Desktop\FTP\dateien>>ftp.txt
echo cd httpdocs>>ftp.txt
echo mput test.txt test2.txt
echo bye
p
in the ftphost.txt File should be the diffrent FTP Servers
in the ftpuser.txt file the diffrent ftp users
in the ftppass.txt file one password.
If you use a file with the FTP information like this (and each FTP server has the same folder structure)
FTPlist.txt
ftp.host1.com|username1|password1
ftp.host2.com|username2|password2
ftp.host3.com|username3|password3
ftp.host4.com|username4|password4
Then this batch file can upload the set of files to each FTP server:
#echo off
for /f "usebackq tokens=1,2,3 delims=|" %%a in ("FTPlist.txt") do (
(
echo.open %%a
echo.%%b
echo.%%c
echo.binary
echo.lcd "C:\Dokumente und Einstellungen\Administrator\Desktop\FTP\dateien"
echo.cd httpdocs
echo.mput test.txt test2.txt
echo.bye
) >ftp.script
ftp -i -s:ftp.script >> ftp.log
)
del ftp.script

Batch script to wait for file to be generated on FTP server and download

I have a program in my FTP server to generate a file, which may take 3-5 minutes to complete and also I knew the name of the file which i being created by my program. Now, once I initiate the program in my server, I have keep checking until the file is created. Once it is created, I am using the below batch script to ftp the file to my local desktop.
#ftp -i -s:"%~f0"&GOTO:EOF
open 10.100.16.111
username
password
lcd c:\
cd root/output_folder
binary
mget "*partial_file_name*" REM mget using wildcard search
disconnect
bye
This script works fine for me. But the problem is, I need modify this script as such, script should keep running until the file is generated. Because i don't know when the file creation will get completed. So, it will great if some one help/guide me to make a looping script which will wait until the completion of file creation and download the same file through FTP.
With this edit you can launch the batch file with the file name on the command line, like this:
ftpscript.bat "filename.ext"
Note that your lcd uses c:\ which is a restricted location in later versions of windows.
#echo off
>file.tmp echo open 10.100.16.111
>>file.tmp echo username
>>file.tmp echo password
>>file.tmp echo lcd c:\
>>file.tmp echo cd root/output_folder
>>file.tmp echo binary
>>file.tmp echo mget "%~1"
>>file.tmp echo disconnect
>>file.tmp echo bye
:retry
ftp -i -s:"file.tmp"
if not exist "%~1" timeout /t 300 & goto :retry
echo file has downloaded
del file.tmp
pause
More elegant solution is to use an FTP client that supports parametrized scripts or commands on command-line, such as WinSCP, to avoid creating a temporary script file.
Parametrized script
The batch file would be more or less identical as with the Windows ftp:
#echo off
:retry
winscp.com /script=script.txt /parameter "%~1"
if not exist "%~1" timeout /t 300 & goto :retry
echo file has downloaded
pause
The ftp script converts to following WinSCP script:
open ftp://username:password#10.100.16.111/
lcd c:\
cd root/output_folder
get -transfer=binary "%1%"
exit
Commands on command-line
You can also inline the commands the to the batch file:
#echo off
:retry
winscp.com /command ^
"open ftp://username:password#10.100.16.111/" ^
"lcd c:\" ^
"cd root/output_folder" ^
"get -transfer=binary ""%~1""" ^
"exit"
if not exist "%~1" timeout /t 300 & goto :retry
echo file has downloaded
pause
References:
Automating file transfers to FTP server;
Upload to multiple servers / Parametrized script.
Had you ever need to upgrade to the FTPS or the SFTP, just modify the session URL in the open command command accordingly.
(I'm the author of WinSCP)

Resources