DosTips.com ... for WinXP
Search:
Last update:
Jan 19, 2008

DOS Batch - Interfacing non DOS Software

Embed other languages into your batch, like: Perl, SQL, FTP, ...


DOS Batch FTP - Simple
FTP script and batch in a single file.

OSQL.EXE - Run SQL script from DOS Batch
SQL script and dos batch script in one file, the One-File Solution

OSQL.EXE - Run SQL script from DOS Batch, passing parameters
Run SQL scripts with parameters passed in from the batch script.

Perl - Perl Script within a DOS Batch
Perl script and batch in a single file.

Perl Script within a DOS Batch with delayed exit
Perl script and batch in a single file.


TOP
2008-01-01

DOS Batch FTP - Simple - FTP script and batch in a single file

Description:

Embed FTP script into a batch script. Add this line at the beginning of the FTP script:

@ftp -i -s:"%~f0"&GOTO:EOF

The "FTP -s:ftpscript.txt" option executes a FTP script wheres "%~f0" resolved to the name of the running batch file. "GOTO:EOF" ends the batch script and makes sure the FTP script doesn`t run as part of the batch.
Good: You end up with only one file that contains the batch script and the FTP script combined.
Minor flaw: The batch command in the first line causes an "Invalid command." error when executed in FTP context, however the FTP execution will continue.

Features:
  • Single file to distribute combining batch and FTP script
Script:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
@ftp -i -s:"%~f0"&GOTO:EOF
open example.com
username
password
!:--- FTP commands below here ---
lcd c:\MyLocalDirectory
cd  public_html/MyRemoteDirectory
binary
mput "*.*"
disconnect
bye
Script Output:
 DOS Script Ouput
ftp> @ftp -i -s:"%~f0"&GOTO:EOF
Invalid command.
ftp> open example.com
User (Username:(none)):

ftp> !:--- FTP commands below here ---
ftp> lcd c:\MyLocalDirectory
Local directory now c:\MyLocalDirectory.
ftp> cd  public_html/MyRemoteDirectory
ftp> binary
ftp> !: mput "*.*"
ftp> disconnect
ftp> bye

TOP
2008-01-01

OSQL.EXE - Run SQL script from DOS Batch - SQL script and dos batch script in one file, the One-File Solution

Description:

Embedding SQL script within a batch script is just as easy. The following batch script executes itself in SQL context. The trick is the GOTO command in the first line of the script. When executing GOTO START in batch context than the command processor will jump to the label ":START" and execute the batch script. The batch script will then run the OSQL.EXE using the batch file itself as SQL file argument to be executed. When subsequently executing the GOTO START line in SQL context, the query language processor will jump to the label "START:" and execute the SQL queries. In fact the file can be opened and executed in Query Analyzer as is, since the batch script in the file looks like a comment to the query language processor.

Script:
Download: Batch4SQL.bat  
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
GOTO START
-- DOS jumps to the ':START' label
-- SQL jumps to the 'START:' label

/* Begin of SQL comment, this makes the BATCH script invisible for SQL
:: BATCH starts below here
:START
@echo off
CLS

OSQL.EXE -n -E -w 65536 -d NORTHWIND -i "%~f0"

PAUSE&GOTO:EOF
*/

-- SQL starts below here
START:
GO
SELECT * FROM AUTHOR
GO

TOP
2008-01-01

OSQL.EXE - Run SQL script from DOS Batch, passing parameters - Run SQL scripts with parameters passed in from the batch script

Description:

Now we can embed SQL queries into a batch file. But how can we pass arguments from the batch script into SQL? This can be done using a temporary table. Temporary tables live as long as the connection to the SQL Server. But how can we fill a temporary table with runtime data and execute the embedded SQL script without creating two separate SQL Server connections by calling OSQL.EXE twice? The trick is that OSQL allows to use the -i and -q option at the same time whereas:

  • -q specifies a query string to be executed
  • -i specifies a filename with SQL syntax to be executed
Both the query string and the SQL file will be executed using the same Server connection. Some testing shows that the query string -q will always be executed before the -I SQL file, which allows us to use a query string to set up a temporary table.

Script:
Download: Batch4SQL2.bat  
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
GOTO START
-- DOS jumps to the ':START' label
-- SQL jumps to the 'START:' label
-- Source http://www.dostips.com

/* Begin of SQL comment, this makes the BATCH script invisible for SQL
:: BATCH starts below here
:START
@echo off
CLS

set /p AuthorName=Author Name:
set /p AuthorCity=Author City:

rem.-------------------------------------------------------------------------
rem.Execute THIS file in SQL context transmitting arguments per temp table
set Args=

rem.--BEGIN ARGS SECTION ------------------
set Args=%Args% INSERT #ArgsTable VALUES ('AuthorName','%AuthorName%')
set Args=%Args% INSERT #ArgsTable VALUES ('AuthorCity','%AuthorCity%')
rem.--END   ARGS SECTION ------------------

set Args= -q "SET NOCOUNT ON CREATE TABLE #ArgsTable(Arg char(16) PRIMARY KEY, Val char(32)) %Args%"

OSQL.EXE -n -E -w 65536 -d NORTHWIND %Args% -i "%~f0"

PAUSE&GOTO:EOF
*/

-- SQL starts below here
START:
GO

DECLARE @AuthorName varchar(128)
DECLARE @AuthorCity varchar(128)

--Set defaults for values as needed
SET @AuthorName = ''
SET @AuthorCity = ''

--Copy data from temporary table into variables
IF EXISTS (SELECT name, type FROM tempdb..sysobjects WHERE name like '#ArgsTable_%' AND type = 'U')
BEGIN
    SELECT @AuthorName=Val FROM #ArgsTable  WHERE Arg='AuthorName'
    SELECT @AuthorCity=Val FROM #ArgsTable  WHERE Arg='AuthorCity'
END

print @AuthorName
print @AuthorCity

GO

TOP
2008-01-01

Perl - Perl Script within a DOS Batch - Perl script and batch in a single file

Description:

It`s nice not to have to type "perl -s Batch4Parl.pl" into the command line and rather being able to just double click a Perl script in Explorer. The trick of renaming the Perl .pl to a batch .bat file and wrapping a batch script around the Perl script is well known under Perl monks. However the solution I have seen so far needed batch code before and after the Perl script where as the solution presented below only needs some lines of DOS at the top. The added DOS script is generic and works independent from the name of the file.

When running the batch the DOS command interpreter will read the first lines and execute the file itself in Perl context.

Script:
Download: Batch4Perl.bat  
1.
2.
3.
4.
5.
6.
7.
8.
@rem = 'Perl, ccperl will read this as an array of assignment & skip this block
@CD /d "%~dp0"
@perl -s "%~nx0" %*
@GOTO:EOF
@rem ';

#perl script starts below here
print 'Hi there!  DOS rocks!\n'

TOP
2008-01-01

Perl Script within a DOS Batch with delayed exit - Perl script and batch in a single file

Description:

This example works just as the previews one but will wait 4 seconds before the application finally closes. This is just enough time to inspect the screen output before the window vanishes. The delay can probably be done much easier in Perl, but somebody just starting on Perl might find this still useful.

Script:
Download: Batch4Perl2.bat  
1.
2.
3.
4.
5.
6.
7.
8.
9.
@rem = 'Perl, ccperl will read this as an array of assignment & skip this block
@CD /d "%~dp0"
@perl -s "%~nx0" %*
@FOR /L %%c in (4,-1,1) do @(TITLE %~nx0 - %%cs to close & ping -n 2 -w 1000 127.0.0.1 >NUL)
@TITLE Press any key to close the window&ECHO.&GOTO:EOF
@rem ';

#perl script starts below here
print 'Hi there!  DOS rocks!\n'