User Input - DOS batch file - batch-file

I get a bat file as below:
#ECHO Executing scripts...
PAUSE
for %%X in (*.SQL) do SQLCMD -S localhost -d CTL -I -i "%%X" >> ResultScript.txt
pause
In this I want to has user inputs for localhost (Sql server instance) and CTL (database).
How can this be achieved in DOS (os: WinXP)
Thanks

SET /P variable=PromptString
So your batch file would be something like this
#ECHO Executing scripts...
PAUSE
SET /P sqlServer=Please enter SQLServer:
SET /P CTL=Please enter CTL:
for %%X in (*.SQL) do SQLCMD -S %sqlServer% -d %CTL% -I -i "%%X" >> ResultScript.txt
pause

Use parameter markers, where %1 refers to the first parameter, %2 the second, and so forth.:
for %%X in (*.SQL) do SQLCMD -S %1 -d %2 -I -i "%%X" >> ResultScript.txt
If your batch file was called ExecScript.bat, your user would run it as
ExecScript instancename databasename
You'll probably want to add a test above the for loop to make sure both parameters are passed in. Running SQLCMD with a blank instance and database wouldn't work too well.

Related

User-specified scripts and output folders for sqlcmd in batch file

Currently, the below For/Do loop goes through all SQL files that are in the same folder as the batch file itself and outputs the CSVs to a user-specified folder (%OUTPUTFOLDER% is actually created earlier in the batch file as a subfolder of %SCRIPTFOLDER%, which is specified by the user):
FOR /F "tokens=*" %%S IN (
'DIR /B "%SCRIPTFOLDER%\*.sql" '
) DO (
echo Reading scripts from: %SCRIPTFOLDER%\*.sql
echo Script: %%~fS
echo Output: %OUTPUTFOLDER%\%%~nS.csv
sqlcmd -b -S %INSTANCE% -d %DATABASE% -i "%%~fS" -s "|" -o "%OUTPUTFOLDER%\%%~nS.csv" -W
IF ERRORLEVEL 1 GOTO errorhandling
ECHO %%~nS.csv successfully created
)
For a visual example, if I had the folder structure
C:\Extract\extract.bat
C:\Extract\Scripts\
C:\Extract\Output\
The variable %SCRIPTFOLDER% is set by the user and is the folder that, you guessed it, holds the scripts. But the batch file has to be in that folder, too. I need to change this so that the scripts do not have to be in the same folder as the batch file. I.e. the user can specify both %SCRIPTFOLDER% and %OUTPUTFOLDER%
Due to the output of echo Script: %%fS, I'm guessing that's what I need to change - possibly what's in the FOR line as well, but I'm not seeing how exactly to do that.

Using sqlcmd in a batch script to run multiple scripts with output files

I need to run about 50 scripts in a folder using sqlcmd from a batch file. Each script's query results need to be sent to its own output file. I have a working batch file that just runs each from a separate line:
sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P "%PASSWORD%" -i "%SCRIPTFOLDER%\master_departments.sql" -s "|" -o "%OUTPUTFOLDER%\master_departments.csv" -W
sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P "%PASSWORD%" -i "%SCRIPTFOLDER%\master_companies.sql" -s "|" -o "%OUTPUTFOLDER%\master_companies.csv" -W
sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P %PASSWORD% -i "%SCRIPTFOLDER%\bill_history.sql" -s "|" -o "%OUTPUTFOLDER%\bill_history.csv" -W
sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P %PASSWORD% -i "%SCRIPTFOLDER%\episodes.sql" -s "|" -o "%OUTPUTFOLDER%\episodes.csv" -W
Is there any way to run this in some kind of loop? I've seen examples that run a loop of all SQL scripts in a folder, but nothing that I've seen does it with an output file set.
Per #LotPings' suggestion I used the below code:
set INSTANCE=<someinstance>
set DATABASE=<somedb>
set USERNAME=<someuser>
set PASSWORD=<somepassword>
set "SCRIPTFOLDER=D:\<pathToScripts>\"
set "OUTPUTFOLDER=D:\<pathForOutput>\"
#Echo off
For /F "tokens=*" %%S in ('Dir /B "%SCRIPTFOLDER%*.sql" '
) do echo sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P "%PASSWORD%" -i "%%~fS" -s "|" -o "%%~dpnS.csv" -W
#pause
I ran that in a batch file and when it paused, the last line said, "The system cannot find the file specified."
Thinking it was perhaps the backslashes in my paths, I removed them and put a slash before the .sql in the for line, but I got the same results.
Removing the backslash altogether resulted in a "File not found" message when I ran it like that.
In case your output file name matches the script name (without extension)
and your parameters are the same for all scripts
#Echo off
For /F "tokens=*" %%S in ('Dir /B "%SCRIPTFOLDER%*.sql" '
) do echo sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P "%PASSWORD%" -i "%%~fS" -s "|" -o "%%~dpnS.csv" -W
The echo in front of sqlcmd prevents execution and allows to review the output. If all looks OK, remove the echo.
The for variable behaviour can be changed with ~ modifiers, see For /? or visit ss64.com/nt/for.html / syntax-args
To pass a folder to the batch you can input via set /P or hand over via command line arguments.

Adding a variable in CMD

I wrote a small script which intends to copy a file from our Servers, copy it to a local machine and run it. It works for me, however I would like to add parameters in order to make it easy for others to use it as well.
#echo off
pushd \\NetworkPath & copy batfile.bat \\ComputerName\c$\Users\UserName\Desktop & popd & psexec -i -s -d \\ComputerName -u UserName -p UserNamePassword "C:\Users\UserName\Desktop\batfile.bat"
As you can see it copies the file locally to the Desktop of the user and runs the file itself. Please tell Me how I can use variables for ComputerName,UserName and UserNamePassword in order to have a query each time asking me what are the values.
You need the SET /p command:
#echo off
SET /p pwd=password:
SET /p usr=user name:
SET /p compname=computer name:
pushd \\NetworkPath & copy batfile.bat \\%compname%\c$\Users\%usr%\Desktop & popd & psexec -i -s -d \\%compname% -u %usr% -p %pwd% "C:\Users\%usr%\Desktop\batfile.bat"

batch file creation pse

I'm trying to run a batch file that will connect to the list of ip's on my network, open up cmd then run a list of commands: like ipconfig /all, nbstat -c, arp -a. Then it must save the results into a folder renamed as that "computername".
I already have a batch file made that can do the commands I want and create a folder with the computer, then input the different commands into txt files within that folder.
Here is the WindowsCommands batch file:
md %computername%
echo off
echo ARP Command
arp -a >> %cd%\%computername%\arp-a.txt
echo NBSTAT Command
nbtstat -c >> %cd%\%computername%\nbstat.txt
echo Ipconfig Command
ipconfig /all >> %cd%\%computername%\ipconfig-all.txt
echo Ipconfig DNS Command
ipconfig /displaydns >> %cd%\%computername%\ipconfig-displaydns.txt
echo Netstat Command
netstat -ano >> %cd%\%computername%\netstat-ano.txt
echo Tasklist Command
tasklist /v >> %cd%\%computername%\tasklist.txt
echo LG Admin Command
net localgroup administrators >> %cd%\%computername%\netlocalgroupadmin.txt
echo Directory Command
dir C:\Windows\Prefetch >> %cd%\%computername%\prefetch.txt
exit
I also created a hosts.txt file that contains my local Ip addresses that I want to run the commands on.
I also created another batch file name psexec for running a For loop.
Now my troubles and arising when trying to run the psexec batch file.
Here is my psexec file:
for /f %%a in (hosts.txt) do (
psexec \\%%a C:\Users\ISSG\Documents\WindowsCommands.bat
)
Now that is just a rough draft I'm not entirely sure if that is how it should be coded. This is one of the first automated scripts I have ever wrote.
So in a nutshell i need to be able to run this batch file from my local computer- psexec into the IP's. Gather the information and output it into txt files on my local computer.
If anyone could point me in the right direction that would be great!
Thanks!
If your Batch file doesn't exist in the system directory on all of the computers, you have to use the -c switch to copy it to them in order to run it. It's a good idea to try to ping the computer first to save time trying to connect to it.
for /f %%a in (hosts.txt) do (
for /f "tokens=2 delims=:" %%b in ('ping -n 1 %%a ^| find "TTL="') do (
if errorlevel 0 (
psexec \\%%a -c -f -u username -p password C:\Users\ISSG\Documents\WindowsCommands.bat
)
)
)
Also, keep in mind that this will create the folder in the System32 directory on the remote computer. If you want it on your local drive, do something like this:
#echo off
FOR /F "tokens=2" %%A IN (
'net use * "\\computer\share"'
) DO IF NOT %%A.==command. SET dl=%%A
if not exist "%dl%\%computername%" md "%dl%\%computername%"
echo ARP Command
arp -a >> %dl%\%computername%\arp-a.txt
echo NBSTAT Command
nbtstat -c >> %dl%\%computername%\nbstat.txt
echo Ipconfig Command
ipconfig /all >> %dl%\%computername%\ipconfig-all.txt
echo Ipconfig DNS Command
ipconfig /displaydns >> %dl%\%computername%\ipconfig-displaydns.txt
echo Netstat Command
netstat -ano >> %dl%\%computername%\netstat-ano.txt
echo Tasklist Command
tasklist /v >> %dl%\%computername%\tasklist.txt
echo LG Admin Command
net localgroup administrators >> %dl%\%computername%\netlocalgroupadmin.txt
echo Directory Command
dir C:\Windows\Prefetch >> %dl%\%computername%\prefetch.txt
Net use %dl% /delete

how to execute all .sql file of current and sub folder using xp_cmdshell

I am using sql server 2008 , I am developing script that get all .sql file of given path ( also serch in subfolder recursively). Thanks in advance.
You could use a batch file like this. Call it ashwin.bat (or whatever you like) and it will look for all the files in C:\tmp\so\ashwin that have a .sql extension and then invokes sqlcmd against all of those files against a named instance database of localhost\localsqla and runs them in the master database.
#echo off
For /R "C:\tmp\so\ashwin\" %%i in (*.sql) DO CALL sqlcmd.exe -E -S localhost\localSQLA -d master -i %%i
A litle enhancement for logging purposes:
#echo off
For /R "C:\Deploy\SQL" %%i in (*.sql) DO CALL echo %%i && sqlcmd.exe -E -S DB_IP -d DATABASE -i %%i -j

Resources