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
Related
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
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
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
)
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.
I want to make a batch file that sends certain values to a output.txt. I have the following code:
for /F "delims=" %%a in ('adb logcat -v threadtime | grep -m 1 'VAC: App Name is:'') do set appName=%%a
echo App Name= %appName% > output.txt
The problem is that the grep statement doesn't immediately return a value--not until a user performs a certain action on the Android device. So I need to create a loop around the "for" statement. I'll make an attempt below, but I know it's wrong.
do
while
%appName% = ""
for /F "delims=" %%a in ('adb logcat -v threadtime | grep -m 1 'VAC: App Name is:'') do set appName=%%a
if "%appName% != "" leave
enddo
echo App Name= %appName% > output.txt
set "appName="
:loop
for /F "delims=" %%a in (
'adb logcat -v threadtime ^| grep -m 1 "VAC: App Name is:"'
) do set "appName=%%a"
if not defined appName goto loop
It just clears the variable and keep looping until it gets any value in it
Here you go (although MC ND's answers looks good too):
Please note pipe needs to be escaped by caret^, I added a 1 secs wait ping -n 1 -w 1000 1.1.1.1 >nul
:while_loop
for /F "usebackq delims=" %%a in (`adb logcat -v threadtime ^| grep -m 1 'VAC: App Name is:'`) do set appName=%%a
ping -n 1 -w 1000 1.1.1.1 >nul
if not "%appName% == "" goto exit_loop
goto while_loop
:exit_loop
echo App Name= %appName% > output.txt