I made a simple Windows batch file to decode JARs and APKs, and it worked:
#ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
SET listejar=:core.jar:core-junit.jar:bouncycastle.jar:ext.jar:framework.jar:framework2.jar:android.policy.jar:services.jar:apache-xml.jar:filterfw.jar:java.awt.jar:svc.jar:am.jar:android.test.runner.jar:bmgr.jar:bu.jar:com.android.future.usb.accessory.jar:com.android.location.provider.jar:com.google.android.maps.jar:com.google.android.media.effects.jar:com.google.widevine.software.drm.jar:com.samsung.device.jar:com.yamaha.android.media.jar:twframework.jar:send_bug.jar:GlobalConfig.jar:ime.jar:input.jar:javax.obex.jar:libvtmanagerjar.jar:minimode.jar:monkey.jar:pm.jar:seccamera.jar:secframework.jar:sechardware.jar:secmediarecorder.jar:sec_feature.jar
SET baksmali=c:\DO\baksmali.jar
SET smali=c:\do\smali.jar
SET BTP1=c:\do
SET BTP2=c:\do\framework
FOR %%F IN ("*.odex") DO (
#echo.
#echo.
#echo.
ECHO Decompiling %%F
java -Xmx1024m -jar %baksmali% -a 15 -d %btp1% -d %btp2% -c %listejar% -x "%%F" -o "%%~nF"
ECHO Compiling %%~nF classses.dex
md "%%~dpnF"_o
java -Xmx1024m -jar %smali% -a 15 %%~nF -o "%%~dpnF"_o\classes.dex
RD /s/q "%%~dpnF"
C:\DO\7z.exe u "%%~dpnF".jar %%~dpnF"_o\classes.dex >NUL
RD /s/q %%~dpnF"_o
DEL %%~dpnxF
ECHO %%~nF is Deodexed
)
cd..
CD app
FOR %%F IN ("*.odex") DO (
#echo:
#echo:
#echo:
ECHO Decompiling %%F
java -Xmx1024m -jar %baksmali% -a 15 -d %btp1% -d %btp2% -c %listejar% -x "%%F" -o "%%~nF"
ECHO Compiling %%~nF classses.dex
md "%%~dpnF"_o
java -Xmx1024m -jar %smali% -a 15 %%~nF -o "%%~dpnF"_o\classes.dex
RD /s/q "%%~dpnF"
C:\DO\7z.exe u "%%~dpnF".apk %%~dpnF"_o\classes.dex >NUL
RD /s/q %%~dpnF"_o
DEL %%~dpnxF
ECHO %%~nF is Deodexed
)
echo ALL DONE press any key to exit
pause >NUL
exit
But then I tried to make it a bit better, and I don't know what i did wrong. It now exits right away. Help on the matter will be really appreciated.
This is what I tried to do in the revised version:
#ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
SET listejar=:core.jar:core-junit.jar:bouncycastle.jar:ext.jar:framework.jar:framework2.jar:android.policy.jar:services.jar:apache-xml.jar:filterfw.jar:java.awt.jar:svc.jar:am.jar:android.test.runner.jar:bmgr.jar:bu.jar:com.android.future.usb.accessory.jar:com.android.location.provider.jar:com.google.android.maps.jar:com.google.android.media.effects.jar:com.google.widevine.software.drm.jar:com.samsung.device.jar:com.yamaha.android.media.jar:twframework.jar:send_bug.jar:GlobalConfig.jar:ime.jar:input.jar:javax.obex.jar:libvtmanagerjar.jar:minimode.jar:monkey.jar:pm.jar:seccamera.jar:secframework.jar:sechardware.jar:secmediarecorder.jar:sec_feature.jar
SET baksmali=c:\DO\baksmali.jar
SET smali=c:\do\smali.jar
SET BTP1=c:\do
SET BTP2=c:\do\framework
CD framework
IF EXIST *.odex ( GOTO STARTFW ) ELSE ( ECHO Odex Not Found && GOTO FINISHEDFW )
:STARTFW
FOR %%F IN ("*.odex") DO (
#echo.
#echo.
#echo.
ECHO Decompiling %%~nxF
java -Xmx1024m -jar %baksmali% -a 15 -d %btp1% -d %btp2% -c %listejar% -x "%%F" -o "%%~nF"
ECHO Compiling %%~nF classses.dex
MD "%%~dpnF"_o
java -Xmx1024m -jar %smali% -a 15 %%~nF -o "%%~dpnF"_o\classes.dex
RD /s/q "%%~dpnF"
IF EXIST "%%~dpnF"_o\classes.dex (GOTO CONTINIUEFW) ELSE (GOTO CHECKFW)
:CHECKFFW
IF NOT EXIST C:\DO\Framework\ERORR ( MD C:\Framework\Error )
MOVE /Y "%%~dpnF".* C:\Framework\Error
ECHO Error With %%~nxF & %%~nF.jar
ECHO Moved To Error Folder
GOTO SKIPFW
:CONTINIUEFW
ECHO Injecting classes.dex to "%%~nF".jar
C:\DO\7z.exe u "%%~dpnF".jar %%~dpnF"_o\classes.dex >NUL
RD /s/q %%~dpnF"_o
DEL %%~dpnxF
ECHO %%~nF.jar is Deodexed
IF EXIST *.odex (
GOTO STARTFW
) ELSE (
ECHO Odex Not Found
GOTO FINISHEDFW
)
:SKIPFW
)
:FINISHEDFW
CD..
CD app
IF EXIST *.odex (
GOTO STARTAPP
) ELSE (
ECHO Odex Not Found
GOTO FINISHEDAPP
)
:STARTAPP
FOR %%F IN ("*.odex") DO (
#echo.
#echo.
#echo.
ECHO Decompiling %%~nxF
java -Xmx1024m -jar %baksmali% -a 15 -d %btp1% -d %btp2% -c %listejar% -x "%%F" -o "%%~nF"
ECHO Compiling %%~nF classses.dex
MD "%%~dpnF"_o
java -Xmx1024m -jar %smali% -a 15 %%~nF -o "%%~dpnF"_o\classes.dex
RD /s/q "%%~dpnF"
IF EXIST "%%~dpnF"_o\classes.dex ( GOTO CONTINIUEAPP ) ELSE ( GOTO CHECKAPP )
:CHECKFAPP
IF NOT EXIST C:\DO\app\ERORR ( MD C:\app\Error )
MOVE /Y "%%~dpnF".* C:\app\Error
ECHO Error With %%~nxF & %%~nF.apk
ECHO Moved To Error Folder
GOTO SKIPAPP
:CONTINIUEAPP
ECHO Injecting classes.dex to "%%~nF".apk
C:\DO\7z.exe u "%%~dpnF".apk %%~dpnF"_o\classes.dex >NUL
RD /s/q %%~dpnF"_o
DEL %%~dpnxF
ECHO %%~nF.apk is Deodexed
IF EXIST *.odex (
GOTO STARTAPP
) ELSE (
ECHO Odex Not Found
GOTO FINISHEDAPP
)
:SKIPAPP
)
echo ALL DONE press any key to exit
pause >NUL
exit
I haven't traced your code to see why you are getting the syntax error. But I see a serious problem - You are attempting to use GOTO within a loop and branch to a label within the same loop. That simply will not work the way you want it to work. The moment you use GOTO, the loop is broken. The GOTO finds the label OK, but the script no longer knows anything about the loop. Your results are bound to be other than what you intend unless you are really clever and want to obfuscate your code.
Generally you should never GOTO a label within a parenthesized block of code. This is true for both FOR loops and IF statements. See (Windows batch) Goto within if block behaves very strangely for a similar problem within an IF/ELSE block.
Related
I'm a newbie to batch scripting. i have written a below script but it was not running. Please let me know whether i'm missing any syntax or something.
Script Procedure :- The variable %MESSAGEFILE% will contain two lines of strings Example :- PID-PROJECTID DEV SFRNUM:0123
Review:123
If missing anyone line should throw error in block :Commitmsg Otherwise it should proceed.
#echo off
setlocal enabledelayedexpansion
set BASEDIR=%CD%
set URL=https://*******:443/reviewboard/
set YES=YES
set MESSAGEFILE=%2%
:: Checking If the API File Exists or Not. If Not Prompt Users to Create
IF NOT EXIST C:\api (
echo Please Create a File api under C:\ and provide ur API details in it 1>&2
exit 1
)
for /f "tokens=*" %%i in ('type "C:\api"') do (
set API=%%i
)
:: Comment Message Validations
type %MESSAGEFILE% > tmpFile
set "file=tmpFile"
call :Commitmsg "%file%"
type %BASEDIR%\tmpFile |findstr /IB "PID" > tmpFile1
set "file=tmpFile1"
call :Commitmsg "%file%"
type %BASEDIR%\tmpFile |findstr /IB "Review" > tmpFile2
set "file=tmpFile2"
call :Commitmsg "%file%"
:: Checking If SVN Command Client is Installed. If yes Store Some svn info into Variables
svn info > svninfo
IF ERRORLEVEL 1 (
echo Seems like SVN Command-line Client Not Installed. Please Install it. Contact Vercon For Support 1>&2
goto :delFiles
)
for /f "tokens=4 delims=//" %%i in ('findstr /ilc:"Repository Root" "svninfo"') do (
set REP=%%i
)
for /f "tokens=3 delims=^/" %%j in ('findstr /ilc:"Relative URL" "svninfo"') do (
set TARGET_GROUPS=%%j
)
FOR /F "tokens=*" %%F IN (tmpFile1) DO (
set msg=%%F
)
FOR /F "tokens=2 delims=:" %%F IN (tmpFile2) DO (
set rev=%%F
)
FOR /F "tokens=* delims= " %%a IN ("%rev%") DO (
SET rev=%%a
)
:: Creating .reviewboardrc file
echo REVIEWBOARD_URL = "%URL%" >> .reviewboardrc
echo REPOSITORY = "%REP%" >> .reviewboardrc
echo DISABLE_SSL_VERIFICATION = "%YES%" > .reviewboardrc
echo API_TOKEN = "%API%" >> .reviewboardrc
echo TARGET_GROUPS = "%TARGET_GROUPS%" >> .reviewboardrc
echo PUBLISH = "%YES%" >> .reviewboardrc
echo OPEN_BROWSER = "%YES%" >> .reviewboardrc
:: Posting Reviews Based On Comment Message in SVN
if /I "%rev:new=%" neq "%rev%" (
rbt post --summary %msg% --description %msg%
goto :delFiles
) else (
rbt post -r %rev% --summary %msg% --description %msg%
goto :delFiles
)
:: Error Message in Case Of Wrong Commit Message in SVN
:Commitmsg
if %~z1 == 0 (
echo Please Provide the Commit Message in the First Line, Followed by review. 1>&2
echo The First Line will be Taken as a Review Summary. 1>&2
echo Follow the Below Example 1>&2
echo ************************************************************ 1>&2
echo PID-PROJECTID DEV SFRNUM:01234 1>&2
echo Review:New / Review:01 1>&2
echo To Create a New Review -- Review:New 1>&2
echo To Update a Existing Review -- Review:01 1>&2
echo ************************************************************ 1>&2
echo Contact Vercon Team For Support 1>&2
goto :delFiles
)
:delFiles
IF EXIST tmpFile del tmpFile
IF EXIST tmpFile1 del tmpFile1
IF EXIST tmpFile2 del tmpFile2
IF EXIST .reviewboardrc del .reviewboardrc
IF EXIST svninfo del svninfo
exit 1
A small Correction Worked !. Please dont waste your valuable time on this as i found the solution.
Thanks !
I am having an issue with the following Windows batch script. I have multiple XML files in the %indir% that I want to import one at a time and email the report. Basically the section starting with "if /I %v_continue% == y" is not executing as expected. I did some debugging step by step with echo on, and it goes through the script till the %BLAT% command without executing any of the steps, and then it starts execution with the first "copy %script_path%.....". It executes the import.exe correctly, but then nothing gets assigned to %subj% and subsequently the %BLAT% command fails. Any advice?
Thanks!
#echo off
set environment=%1
set domain=%2
if [%environment%] == [] goto :endofscript
rem - Get the script path
set script_path=%~dp0
rem - Get the script name without the extension
set script_name=%~n0
rem - Get the script name with the extension
rem set script_name=%~nx0
rem - get the script extension
set script_ext=%~x0
rem - Set environment variables
call %script_path%\setenv.cmd
set cnt=0
set filemask=*.xml
for /f %%a in ('dir /b /a-d %indir%\%filemask%') do call :procfile %%a
goto :EOF
:procfile
set impfile=%1
set v_continue=n
set emailyn=y
set trset=%impfile:~0,3%
set /A cnt + = 1
if 1%cnt% lss 100 set cnt=0%cnt%
if %trset% == RCT (
set "subtxt=Receipt Confirmation"
set v_continue=y
)
if %trset% == SHP (
set "subtxt=Shipment Confirmation"
set v_continue=y
setlocal EnableDelayedExpansion
set MOFound=
for /f "tokens=3 delims= " %%f in ('find /i /c "<RefID>MO-ORD</RefID>" %indir%\%impfile%') do (set MOFound=%%f)
if !MOFound! GTR 0 (
copy %indir%\%impfile% %inarchdir%
move %indir%\%impfile% %S_INDIR%\FX%dttmstamp%%cnt%.xml 2>NUL
goto :EOF
)
endlocal
)
if /I %v_continue% == y (
copy %script_path%\%script_name%.dat %infile%
cscript %REPLACEVBS% %infile% "DOMAIN" "%domain%" 1>NUL 2>&1
cd /d %rptdir%
%DLC%\bin\import.exe -b -T d:\tmp -p %pfile%
rem - Check for errors
find /i /c "ERROR:" %rptfile% > NUL
if %ERRORLEVEL% NEQ 0 (
set "subj=SUCCESS: %subtxt% Import Report (%environment%/%domain%)"
set emailyn=y
) else (
set "subj=ERROR: %subtxt% Import Report (%environment%/%domain%)"
set emailyn=y
)
move %rptfile% %logdir%\%script_name%_%datestamp%_%cnt%.prn 2>NUL
move %outfile% %logdir%\%script_name%_%datestamp%_%cnt%.out 2>NUL
if /I %emailyn% == y (
echo Report location: %logdir%\%script_name%_%datestamp%_%cnt%.prn > %msgfile%
%BLAT% %msgfile% -server abc-com.mail.protection.outlook.com -f donotreply#abc.com -s "%subj%" -t %INBEMAIL% -attachi %logdir%\%script_name%_%datestamp%_%cnt%.prn
)
)
del /f /q %infile%
del /f /q %pfile%
del /f /q %msgfile%
:delfiles
rem - Delete log files that are older than 10 days.
PushD "%logdir%" && (
forfiles /M %script_name%_*.prn /D -10 /C "CMD /C del /f /q #PATH" 2>NUL
) & PopD
:endofscript
exit /B
I need to read a file in outer loop line by line, take this value and use it in inner loop. But currently I am able to read first line from this file and do some required processing in inner loop but outer loop runs only once.
Why does the outer loop run only once?
myfile.txt contains:
AWC00201
AWC00202
AWC00203
DDDD
#echo off
setlocal EnableDelayedExpansion
for /F %%D in (myfile.txt) do (
echo %D%
S:
cd \#vantage\AFG\AWC\AWCU\simulation\WRO_Regression_results\%%D
echo %%D
FOR /F %%i IN ('dir /b /ad-h /o-d') DO (
echo After Nested For
echo %%D
SET test=%%D
SET b=%%i
GOTO found
)
echo No subfolder found
goto done
:found
echo %D%
echo Most recent subfolder: %b%
cd %b%
echo %%D
find /c "O K" tooling.report
echo %D%
if %errorlevel% equ 1 goto notfound
echo found
goto done
:notfound
echo notfound
goto done
:done
echo %D%
echo now go up
echo !test!
echo %test%
)
pause
I am getting following output:
ECHO is off.
AWC00201
After Nested For
AWC00201
ECHO is off.
Most recent subfolder: 20141103_170658_wro_awc
%D
____________ TOOLING.REPORT: 0
ECHO is off.
notfound
ECHO is off.
now go up
AWC00201
AWC00201
Press any key to continue . . .
Your code has one big problem and one thing to change
The problem is that it is not possible to use goto while inside a for loop and keep the loop iterating. goto cancels the for looping.
The thing to change is your use of variables. You have the information you need inside the for replaceable parameters. Use them. Move the value to a variable when the replaceable parameters does not offer what you need, but this is not the case
#echo off
setlocal enableextensions disabledelayedexpansion
for /F "delims=" %%D in (myfile.txt) do (
cd /d "s:\#vantage\AFG\AWC\AWCU\simulation\WRO_Regression_results\%%D"
for /d %%a in (.) do echo Current folder is "%%~fa"
set "file="
FOR /F "delims=" %%i IN ('dir /b /ad-h /o-d 2 >nul ') DO if not defined file (
set "file=1"
echo subfolder found : %%i
find /c "O K" ".\%%i\tooling.report" >nul 2>nul
if errorlevel 1 (
echo O K found
) else (
echo O K not found or file does not exist
)
)
if not defined file (
echo subfolder not found
)
)
pause
Hi I am having problems with one of my first CMD scripts, any suggestions on how to go about this loop with grabbing the results outputted the command window with the if statements?
#echo off
:BEGIN
T:
cd js
cd ddf
cd invent
for %%s in ("invstk" "labmas") do (
Call Isutl -r %%s
if result contains "%ISUTL=20: Cannot open index file" goto SendEmail else if the result = "Indexing Complete" continue to next file in
list, if result not contains "%ISUTL=20: Cannot open index file" or "Indexing Complete" then call Isutl -r on that file again
)
:SendEmail
:DONESCAN
Exit
See if this does what you need:
#echo off
:BEGIN
cd /d "t:\js\ddf\invent"
for %%s in ("invstk" "labmas") do call :routine "%%s"
del file.log 2>nul
pause
goto :EOF
:routine
Call Isutl -r "%~1" >file.log 2>&1
find /i "ISUTL=20: Cannot open index file" <file.log >nul && goto SendEmail
find /i "Indexing Complete" <file.log >nul && goto :EOF
goto :routine
:SendEmail
rem send email
goto :EOF
So I created a windows batch script to convert videos using the HandbrakeCLI
Batch script:
#echo off
::SET TO CURRENT CODE PAGE:::::::::::::::::::::::::::
FOR /F "tokens=4 delims= " %%G in ('chcp') DO (
chcp %%G >nul
)
:::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal EnableExtensions
title AniCoder v2.5 by Nightsanity
color 0a
cd "%~d0%~p0"
::PREVENT MULTIPLE HANDBRAKE PROCESSES::::::::::::::::
set ignore=INFO:
for /f "usebackq" %%A in (`tasklist /nh /fi "imagename eq HandBrakeCLI.exe"`) do if not %%A==%ignore% (
exit
)
::::::::::::::::::::::::::::::::::::::::::::::::::::::
::PREREQUISITES CHECK:::::::::::::::::::::::::::::::::
if not exist "HandBrakeCLI.exe" (
echo HandBrakeCLI is missing!
pause
exit
)
if exist "Jobs.txt" del "Jobs.txt"
if not exist "ToConvert" mkdir "ToConvert"
if not exist "Converted" mkdir "Converted"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
::CREATE A LIST OF VIDEOS TO CONVERT:::::::::::::::::::
for /F %%i in ('dir /s /b "ToConvert\*.*"') do (
dir/s/b "ToConvert\*" >> "Jobs.txt"
goto MAIN
)
goto NOFILES
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
::LOOP THROUGH JOBS LIST AND CONVERT FILES:::::::::::::
:MAIN
for /f "tokens=* delims= " %%a in (Jobs.txt) do (
HandBrakeCLI -i "./ToConvert/%%~nxa" -f mp4 -o "./Converted/%%~na.mp4" -q 22 -e x264 -x cabac=0:ref=2:me=hex:bframes=0:weightp=0:subme=6:8x8dct=0:trellis=0 -E faac --mixdown mono -B 64 -X 480 -l 272 -s 1 --subtitle-burn -a 1
)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
::CLEAN UP FILES:::::::::::::::::::::::::::::::::::::::
if exist "Jobs.txt" del "Jobs.txt"
:Question
cls
echo Encode Done!
echo.
set DELFILES=
set /p DELFILES="Do you want to delete original files (Y/N)?:"
if "%DELFILES%" == "y" goto DELNOW
if "%DELFILES%" == "Y" goto DELNOW
if "%DELFILES%" == "n" goto ENDNOW
if "%DELFILES%" == "N" goto ENDNOW
goto Question
:DELNOW
if exist "ToConvert\*.*" del /Q "ToConvert\*.*"
:ENDNOW
endlocal
exit
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
::WHEN NO FILES ARE INSIDE TOCONVERT FOLDER::::::::::::
:NOFILES
echo No video files found in:
echo ToConvert folder
echo.
pause
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
HandbrakeCLI reference:
trac.handbrake.fr/wiki/CLIGuide
I have ran into a problem where if there is a special character in the path for example a German Umlaut it will not interpret it or something as it wont convert and close the window.
How can I workaround this issue?
Running Windows 7 Home Premium Sp1
I'd suggest you change
for /F %%i in ('dir /s /b "ToConvert\*.*"') do (
dir/s/b "ToConvert\*" >> "Jobs.txt"
to
for /F "delims=" %%i in ('dir /s /b "ToConvert\*.*"') do (
echo(%%~dspnxi >> "Jobs.txt"
to output the shortname to jobs.tst.
or perhaps
echo(%%~dspi%%~nxi >> "Jobs.txt"
may be better.
(can't test - no test directories available to me)