Batch script for executing sql files that does nothing - sql-server

I've got the following batch script for executing sql scripts contained in many files, using sqlcmd utility:
#echo off
setlocal enabledelayedexpansion
set /p servername=Enter DB Servername :
set /p dbname=Enter Database Name :
set /p spath=Enter Script Path :
set /p usr=Enter the username :
set /p pw=Enter the password :
set hr=%time:~0,2%
if "%hr:~0,1%" equ " " set hr=0%hr:~1,1%
set logfilepath= %spath%\output_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%hr%%time:~3,2%%time:~6,2%.log
set cmd='dir "%spath%\*.sql" /b'
FOR /f "delims=" %%f IN (%cmd%) DO (
echo ******PROCESSING %%f FILE******
echo ******PROCESSING %%f FILE****** >> %logfilepath%
SQLCMD -S%servername% -U%usr% -P%pw% -d%dbname% -s" -i%%f -b >> %logfilepath%
IF !ERRORLEVEL! NEQ 0 GOTO :OnError
)
GOTO :Success
When I run this script a prompt with "1>" will wait for command. I can understand what is going wrong.
Thank you very much

You are getting SQLCMD started and not doing what you desired. For starters I would change this line
SQLCMD -S%servername% -U%usr% -P%pw% -d%dbname% -s" -i%%f -b >> %logfilepath%
to
SQLCMD -S %servername% -U %usr% -P "%pw%" -d%dbname% -s ^" -i "%%f" -b >> "%logfilepath%"
Not sure if spaces I added are required, but easier to read
Use quotes in case there are poison characters or spaces in password, filename, and path
Escape the column separator ("). This is probably what is causing your problem.

Related

How to write a batch file calling sqlcmd to query SQL Server and if instance unreachable, it would echo the server name to a log file?

The idea is to write a batch file to execute a query against a list of SQL servers and get some information from them, if any SQL Server is not reachable, I want this batch file to write servername\instancename to a text file for my reference.
I have around 100 SQL Server instances (in text file), when I get the result, it is hard to know which instance was unreachable during the batch file execution.
This is the script I use to get the info from the SQL Servers in (listed in text file) and it works:
for /F "tokens=*" %%S in (SQLLIST.txt) do sqlcmd -E -h -1 -W -M -S %%S -i C:\Foldername\Query.sql >> Destination\QueryResult.csv -s ","
I figure I may need to add an IF statement to do that? Please help.
NOTE I do not have Sqlcmd on this device, so not able to test this.
I would say using -b switch to get errorlevel This should simply echo to screen the instance and errorlevel 0 is success and 1 is error:
#echo off
setlocal enabledelayedexpansion
for /F "tokens=*" %%S in (SQLLIST.txt) do (
sqlcmd -b -E -h -1 -W -M -S %%S -i C:\Foldername\Query.sql >> Destination\QueryResult.csv -s ","
echo %%S !errorlevel!
)
So you could log it to file:
#echo off
setlocal enabledelayedexpansion
(for /F "tokens=*" %%S in (SQLLIST.txt) do (
sqlcmd -b -E -h -1 -W -M -S %%S -i C:\Foldername\Query.sql >> Destination\QueryResult.csv -s ","
echo %%S !errorlevel!
)
)2>&1>mysqlcmd.log
or log it to seperate
#echo off
setlocal enabledelayedexpansion
(for /F "tokens=*" %%S in (SQLLIST.txt) do (
sqlcmd -b -E -h -1 -W -M -S %%S -i C:\Foldername\Query.sql >> Destination\QueryResult.csv -s ","
echo %%S !errorlevel!
)
)2>&1>mysqlcmd.log
Or you can specify success or failure:
#echo off
setlocal enabledelayedexpansion
(for /F "tokens=*" %%S in (SQLLIST.txt) do (
sqlcmd -b -E -h -1 -W -M -S %%S -i C:\Foldername\Query.sql >> Destination\QueryResult.csv -s ","
if !errorlevel! equ 0 set outp=Success
if !errorlevel! equ 1 set outp=Failed
echo %%S !outp!
)
)2>&1>mysqlcmd.log

Find all .sql-Files in a Directory and execute them with sqlcmd

I'm still a CMD beginner and would like to import all SQL files I have in a directory structure with sqlcmd to my DB.
If I copy all *.sql into the root folder the following command works:
#ECHO ON
FOR %%G in (*.sql) DO sqlcmd /S *SERVER* /d *DB* /U *USER* /P *PW* -i"%%G" > LOG_lastrun.txt 2> LOG_errors.txt
pause
#ECHO ON
FOR /F "tokens=1 delims=sql" %%G in (*.sql) DO sqlcmd /S *SERVER* /d *DB* /U *USER* /P *PW* -i"%%G" > LOG_lastrun.txt 2> LOG_errors.txt
pause
Unfortunately, it doesn't work for me FOR /F loops. Can you help me here?
The FOR /F works differently. It runs a command. Jusr *.sql is not a command. Also, I suspect that you want to concatenate stdout and stderr output using >>.
#ECHO ON
SET "SERVER_NAME=*SERVER*"
SET "DB_NAME=*DB*"
SET "LOGIN_ID=*USER*"
SET "PASS=*PW*"
FOR /F "delims=" %%G IN ('DIR /B "*.sql"') DO (
sqlcmd -S %SERVER_NAME% -d %DB_NAME% -U %LOGIN_ID% -P %PASS% -i "%%~G" >> LOG_lastrun.txt 2>> LOG_errors.txt
)
pause

CMD search results send to console

I would like to ask if anyone has an idea how to send search result to console in single command line but with some specific code before and after the search results output
I need to run program Xconverter that is made to be operated by console, and has parameters:
-s :silent mode (only mode suported yet)
-i [files] : list location of the files in quotes: {-i "a.txt" "b.txt"..}
-t [tool file] : tool file location
-m 5 : conversion style
syntax accepted for example (single command for XConverter):
"C:\XConverter.exe" -s -i "C:\a.pgm" "C:\abc\b.pgm" -t "tool.tlgx" -m 5
but I need about 500 -i [files] to list
so i created search that fill look for those .pgm in tree mode where .bat is located and adds search result (file locations) one after other
my code is working but the only way i made it to work is that i outputed echo result in to extra .bat file and started the file. Is there i way to reprogram my code that it will send these outpust in to single one line command?
echo off
SET mypath=%~dp0
::THIS STARTS PROGRAM
(
echo | set /p="""
echo | set /p="C:\Program Files (x86)\Scm Group\Maestro\XConverter.exe" -s -i "
)>seznam_konvertovanych_suboru.bat
::THIS SEARCHERS FOR PGM FILES AND ADDS PATH OF EACH FILE TO THE LINE
for /f "delims=" %%A in ('forfiles /s /m *.pgm /c "cmd /c echo #relpath"') do (
set "file=%%~A"
setlocal enableDelayedExpansion
echo | set /p="""
echo | set /p="%mypath:~0,-1%\!file:~2!"
echo | set /p="" "
)>>seznam_konvertovanych_suboru.bat
::THIS ADDS SOME EXTRA FILE PATH TO THE LINE OF TOOL FILE, THAT IS NEEDED
(
echo | set /p=-t "Tlgx\def.tlgx" -m 5
)>>seznam_konvertovanych_suboru.bat
seznam_konvertovanych_suboru.bat
This code will output file seznam_konvertovanych_suboru.bat exactly in this format
"C:\XConverter.exe" -s -i "C:\a.pgm" "C:\abc\b.pgm" -t "tool.tlgx" -m 5
Anyone knows how to reprogram this to send that output to console in single line without creating extra file to store that code?
Edited:
What i need is having START_CONVERTING.BAT file that will send to console one command :
"C:\XConverter.exe" -s -i "C:\a.pgm" "C:\abc\b.pgm" -t "tool.tlgx" -m 5
but "C:\a.pgm" "C:\abc\b.pgm" are the search results of all .pgm files in that folder and child folders. So i will copy this START_CONVERTING.BAT anywehere where i store .pgm files. (some of them are in folders and those folders in other folders) so i will just copy STAR_CONVERTING.BAT in major folder that will thake care of all files with all files in child folders.
I will just click START_CONVERTING.BAT and it will convert those .pgm files. (without creating creating any other output to any other .bat file.
SOLVED (code)
#echo off & setlocal enableDelayedExpansion
PushD "%~dp0"
Set "XC=C:\Program Files (x86)\Scm Group\Maestro\XConverter.exe"
Set "TOOLFILE=Tlgx\def.tlgx"
Set "Files="
:: Concatenate all files in one string
for /f "delims=" %%A in ('Dir /B /S *.pgm') do set Files=!Files! "%%~A"
:: command, command line length may be an issue
"%XC%" -s -i%Files% -t "%TOOLFILE%" -m 5
If I understand right this might work
#echo off & setlocal enableDelayedExpansion
PushD "%~dp0"
Set "XC=C:\Program Files (x86)\Scm Group\Maestro\XConverter.exe"
Set "Files="
:: Concatenate all files in one string
for /f "delims=" %%A in ('Dir /B /S *.pgm') do set Files=!Files! "%%~A"
:: store command in batch file, command line length may be an issue
>"seznam_konvertovanych_suboru.bat" Echo "%XC%" -s -i %Files% -t "tool.tlgx" -m 5
try with that batch
Xconverter is for xxl to PGMX
1 set all path for converter
2 creat to do list of xxl files on desktop on this exemple or desktop sub folder!
3 Masetro can't be install as Office PC you must run it on CN PC!
#echo OFF
COLOR F1
TITLE "Convertor XXL to PGMX"
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
echo º Converteur Xconverter º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
echo.
echo "Stop command with Ctrl+C"
echo.
Echo "Path SET"
TIMEOUT /t 5 >nul
Set todo="%userprofile%\desktop\todo.txt"
Set def="%PROGRAMFILES(x86)%\Scm Group\Maestro\Tlgx\def.tlgx"
Set Compil="%PROGRAMFILES(x86)%\Scm Group\Maestro\Xconverter.exe"
Set desktop="%userprofile%\desktop"
echo "To do list of *.XXL files"
TIMEOUT /T 10 >nul
cd %userprofile%\desktop
dir /a /b /o /s *.xxl > %todo%
echo "Files convert command"
TIMEOUT /T 10 >nul
for /f "delims=" %%a in (%todo%) do %Compil% ^ -s -m 4 ^
-i "%%a" ^
-t %def%
echo "Rapport files"
Timeout /T 10 >nul
cd %desktop%
dir /a /b /o /s *.pgmx > Rapport.txt
Echo "Delete to do list"
TIMEOUT /T 10 >nul
DEL %todo% /F /S /Q >nul
But haven't go detail of Convesion style -m X
if you can replie me detail of -m number it was great!
All the best, and hope this is helpful
D-Alexandre V.

Batch Script ErrorLevel Output

I need help - I am trying to output any ErrorLevel that is 1 or greater into a log file. When I issue the command the log file never get's generated. Any help would be greatly appreciated.
Script:
for /f "delims=" %%i in (C:\_\Restart\Computer.txt) do (
start "%%i" \_\PStools\psexec \\%%i -u Administrator -p Password -i c:\restart.cmd
if not %errorlevel%==0 echo %errorlevel% > error.log
)
This script allows me to use PSEXEC and issue a restart command to all the computer at once. However several of them fail and I'd like to know which ones fail.
Thanks!
Is this the format I should use?
setlocal EnableDelayedExpansion
for /f "delims=" %%i in (C:\_\Restart\Computer.txt) do (
start "%%i" \_\PStools\psexec \\%%i -u Administrator -p Password -i c:\restart.cmd
if errorlevel 1 echo !errorlevel! > error.log
)
Script V3:
setlocal EnableDelayedExpansion
for /f "delims=" %%i in (C:\temp\list.txt) do (
start "" "shutdown" /m \\%%i -r -f -t 900
echo !errorlevel! && echo %%i
if errorlevel 1 echo !errorlevel! >> c:\temp\log.txt && echo %%i >> c:\temp\log.txt
)
-m = use remote computer
-r = reboot
-f = Force reboot
-t = delay of time before rebooting
you can use shutdown -? for more help on argument that can be passed to the reboot command.
Script v4 without the start command:
setlocal EnableDelayedExpansion
for /f "delims=" %%i in (C:\temp\list.txt) do (
shutdown /m \\%%i -r -f -t 900
echo !errorlevel! && echo %%i
if errorlevel 1 echo !errorlevel! >> c:\temp\log.txt && echo %%i >> c:\temp\log.txt
)

How to verify root from adb shell?

In my script I have the following code:
for /f "delims=" %%a in ('adb -s devicename shell su') do #set res=%%a
echo %res%
But I get no output. How to properly check for su availability from a batch script?
set uid=
for /f "delims=" %%a in ('adb -s devicename shell "su 0 id -u 2>/dev/null"') do set uid=%%a
echo %uid%
%uid% will be 0 if su is available

Resources