I am writing a script to automate backup setup on new servers. I am not the batch guru, so it is kept rather simple (See here in complete for reference http://pastebin.com/D1zEP8dj). Everything works to the bit where I try to write another batch script to a file:
(
echo del "%BACKUP_SERVER%Snapshot-%%COMPUTERNAME%%\Week_before\*.*" /S /Q
echo.
echo move /Y "%BACKUP_SERVER%Snapshot-%%COMPUTERNAME%%\Full\*.*" "%BACKUP_SERVER%Snapshot-%%COMPUTERNAME%%\Week_before"
echo move /Y C:\Scripts\Backup.log "%BACKUP_SERVER%Snapshot-%%COMPUTERNAME%%\Week_before"
echo move /Y C:\Scripts\Backupdiff.log "%BACKUP_SERVER%Snapshot-%%COMPUTERNAME%%\Week_before"
echo.
echo.
echo C:\Scripts\snapshot.exe C: "%BACKUP_SERVER%Snapshot-$computername\Full\$disk-Partition-$type.sna" --LogFile:C:\Scripts\Backup.log -L50000 -GX --usevss --AllWriters
echo #echo off
echo if %%errorlevel%% NEQ 0 (
echo echo Error while Backup at %%COMPUTERNAME%%.%%USERDOMAIN%% ^| C:\Scripts\blat\blat.exe -server %BLAT_MAILSERVER% -to info#abc.com -f backup#%BLAT_DOMAIN% -s "Error while Backup at %%COMPUTERNAME%%.%%USERDOMAIN%%" -attach C:\Scripts\Backup.log
echo )
) >full-backup.bat
if %DO_DIFF_BACKUP%==y (
(
echo C:\Scripts\snapshot.exe C: %BACKUP_SERVER%\Snapshot-$computername\Diff\$disk-Partition-$type-$weekday.sna -h%BACKUP_SERVER%\Snapshot-$computername\Full\c-Partition-ful.hsh --LogFile:C:\Scripts\Backupdiff.log -L50000 -GX --usevss --AllWriters
echo #echo off
echo if %%%errorlevel%%% NEQ 0 (
echo echo Error while Backup at %%COMPUTERNAME%%.%%USERDOMAIN%% ^| C:\Scripts\blat\blat.exe -server %BLAT_MAILSERVER% -to info#abc.com -f backup#%BLAT_DOMAIN% -s "Error while Backup at %%COMPUTERNAME%%.%%USERDOMAIN%%" -attach C:\Scripts\Backupdiff.log
echo )
) >diff-backup.bat
)
So when executing the script, it stops on the last line of this excerpt. The file full-backup.bat doesn't exist at all, but won't throw an error at all, while the file diff-backup.bat is written with the following content:
C:\Scripts\snapshot.exe C: c:\test\Snapshot-$computername\Diff\$disk-Partition-$type-$weekday.sna -hc:\test\Snapshot-$computername\Full\c-Partition-ful.hsh -- LogFile:C:\Scripts\Backupdiff.log -L50000 -GX --usevss --AllWriters
#echo off
if %0% NEQ 0 (
The internet says, to escape percents double them, to escape pipes ^|-them, etc. But it does not seem to change anything if I quote things, use escape signs at all - batch won't listen to me :(. Any help would be greatly appreciated.
When you double the percent signs where you want to echo literally % and escaping all other special characters with ^ it should work.
As you can't know if your variables contains also special characters, it would be the best to use delayed expansion here instead of percent expansion.
This works as all content is safe when expanded by delayed expansion
setlocal EnabledDelayedExpansion
(
echo del "!BACKUP_SERVER!Snapshot-%%COMPUTERNAME%%\Week_before\*.*" /S /Q
echo.
echo move /Y "!BACKUP_SERVER!Snapshot-%%COMPUTERNAME%%\Full\*.*" "!BACKUP_SERVER!Snapshot-%%COMPUTERNAME%%\Week_before"
echo move /Y C:\Scripts\Backup.log "!BACKUP_SERVER!Snapshot-%%COMPUTERNAME%%\Week_before"
echo move /Y C:\Scripts\Backupdiff.log "!BACKUP_SERVER!Snapshot-%%COMPUTERNAME%%\Week_before"
echo.
echo.
echo C:\Scripts\snapshot.exe C: "!BACKUP_SERVER!Snapshot-$computername\Full\$disk-Partition-$type.sna" --LogFile:C:\Scripts\Backup.log -L50000 -GX --usevss --AllWriters
echo #echo off
echo if %%errorlevel%% NEQ 0 (
echo echo Error while Backup at %%COMPUTERNAME%%.%%USERDOMAIN%% ^| C:\Scripts\blat\blat.exe -server !BLAT_MAILSERVER! -to info#abc.com -f backup#!BLAT_DOMAIN! -s "Error while Backup at %%COMPUTERNAME%%.%%USERDOMAIN%%" -attach C:\Scripts\Backup.log
echo ^)
)
Related
I want to add multiple lines to the hosts file if they're not existing. My code works great but I want to do it for a list of 30 entries and thats where I don't know how to handle it. I thought about a list and a for-loop but I don't know how.
#ECHO OFF
ECHO Checking administrator right.
OPENFILES >NUL 2>&1
IF %ERRORLEVEL% EQU 0 (
ECHO Administrator right detected.
ECHO.
ECHO Hosts file : %WINDIR%\system32\drivers\etc\hosts
ECHO.
SETLOCAL ENABLEDELAYEDEXPANSION
SET BLOCKLINE=127.0.0.1 example.com
ECHO Checking : !BLOCKLINE!
FIND /C /I "!BLOCKLINE!" "%WINDIR%\system32\drivers\etc\hosts" >NUL 2>NUL
IF !ERRORLEVEL! NEQ 0 (
ECHO Line not found, adding to the hosts file.
ECHO !BLOCKLINE!>>%WINDIR%\system32\drivers\etc\hosts
) ELSE (
ECHO Line found.
)
ECHO.
ENDLOCAL
ECHO Patching is completed.
ECHO Check hosts file if you want to see the result.
ECHO.
) ELSE (
ECHO Administrator right not detected.
ECHO You need admin right to use this hosts patch!
ECHO.
)
PAUSE
A basic for loop will suffice.
Define the lines to test in the loop set
For %%L in (
"127.0.0.1 example1"
"127.0.0.30 example30"
) Do (
ECHO Checking : %%~L
FIND /C /I "%%~L" "%WINDIR%\system32\drivers\etc\hosts" >NUL 2>NUL
IF !ERRORLEVEL! NEQ 0 (
ECHO Line not found, adding to the hosts file.
>>"%WINDIR%\system32\drivers\etc\hosts" ECHO %%~L
) ELSE (
ECHO Line found.
)
)
I have a batch file which takes three parameters [log:path], [logok:path], [logerr:path]. The values of the respective parameter is log:c:\logs\install.log, logok:c:\logs\installok.log,
logerr:c:\logs\installerr.log.
I need to process these 3 parameters and set it on respective variable log=c:\logs\install.log, logok=c:\logs\installok.log, logerr=c:\logs\installerr.log so that I can use them in next step to create a file in those paths.
I have written below script but somehow each variable is printing "ECHO is on". It should actually print the location path. Any idea how to achieve this?
REM PARAMS ARE [LOG:PATH] [LOGOK:PATH] [LOGERR:PATH]
REM ACCESS WITH '%LOG%', '%LOGOK%' AND '%LOGERR%'
REM SETUP LOCALIZED ENVIRONMENT VARIABLES FOR THE LOG, LOGOK AND LOGERR PARAMS
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN (%*) DO (
ECHO %%A >> C:\LOGS\TEST1.TXT
FOR /F "TOKENS=1,2,3 DELIMS=:" %%G IN ("%%A") DO (
SET %%G=%%H:%%I
)
)
ENDLOCAL
ECHO %LOG% >> C:\LOGS\TEST2.TXT
ECHO %LOGOK% >> C:\LOGS\TEST3.TXT
ECHO %LOGERR% >> C:\LOGS\TEST4.TXT
START /WAIT MSIEXEC /I "%~DP0SETUP.MSI" /QN
SET EXIT_CODE=%ERRORLEVEL%
REM If the instalaltion is successful it should a create installok.log file in 'c:\logs'
IF %EXIT_CODE% EQU 0 ECHO INSTALLED >> %LOGOK%
REM If it fails then it should a create installerr.log file 'c:\logs')
IF NOT EXIST %LOGOK% ECHO FAILED >> %LOGERR%
Output of TEST1.TXT:
log:c:\logs\install.log
logok:c:\logs\installok.log
logerr:c:\logs\installerr.log
Output of TEST1.TXT,TEST2.TXT,TEST3.TXT:
ECHO is on.
Try this:
REM PARAMS ARE [LOG:PATH] [LOGOK:PATH] [LOGERR:PATH]
REM ACCESS WITH '%LOG%', '%LOGOK%' AND '%LOGERR%'
REM SETUP LOCALIZED ENVIRONMENT VARIABLES FOR THE LOG, LOGOK AND LOGERR PARAMS
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN (%*) DO (
ECHO %%A >> c:\Logs\TEST1.TXT
FOR /F "TOKENS=1,2,3 DELIMS=:" %%G IN ("%%A") DO (
SET %%G=%%H:%%I
)
)
ECHO !LOG! >> c:\LOGS\TEST2.TXT
ECHO !LOGOK! >> c:\LOGS\TEST3.TXT
ECHO !LOGERR! >> c:\LOGS\TEST4.TXT
START /WAIT MSIEXEC /I "%~DP0SETUP.MSI" /QN
SET EXIT_CODE=%ERRORLEVEL%
REM If the instalaltion is successful it should a create installok.log file in 'c:\logs'
IF %EXIT_CODE% EQU 0 ECHO INSTALLED >> !LOGOK!
REM If it fails then it should a create installerr.log file 'c:\logs')
IF NOT EXIST !LOGOK! ECHO FAILED >> !LOGERR!
ENDLOCAL
Basically this:
Extends the local scope so that the variables which were assigned inside the FOR loop will be available in the rest of the script
Moves variables to the !delayed! variable syntax in order to cause them to be evaluated as late as possible. See the documentation for "EnableDelayedExpansion" for more details
Note that "ECHO is on." is the output that you get when calling echo with no parameters:
C:\>type echo_test.bat
echo
echo %no_such_variable%
C:\>echo_test.bat
C:\>echo
ECHO is on.
C:\>echo
ECHO is on.
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 !
What is wrong with this code? It says ECHO is off.
#ECHO off
set /p pattern=Enter id:
findstr %pattern% .\a.txt > result
if %errorlevel%==0 (
set var2= <result
echo %var2%
set var1=%var2:~5,3%
echo %var1% > test.txt
echo %var1%
) else (
echo error
)
del result
pause
Any help is appreciated.
If your variable is empty somewhere, it will be the same as having the command "echo" on its own, which will just print the status of echo.
To avoid this, you should replace all your echo commands with something like this:
echo var2: %var2%
That way, if %var2% is empty it will just print "echo var2:" instead of "echo off".
As Laurent stated, it's not a problem of the ECHO, it's a problem of your code.
In batch files, blocks are completely parsed before they are executed.
While parsing, all percent expansion will be done, so it seems that your variables can't be changed inside a block.
But for this exists the delayed expansion, the delayed expansion will be evaluated in the moment of execution not while parsing the block.
It must be enabled, as per default the delayed expansion is disabled.
#ECHO off
setlocal EnableDelayedExpansion
set /p pattern=Enter id:
findstr %pattern% .\a.txt > result
if %errorlevel%==0 (
set var2= <result
echo(!var2!
set var1=!var2:~5,3!
echo(!var1! > test.txt
echo(!var1!
) else (
echo error
)
del result
I used here the construct echo( instead of echo as this will ensure echoing an empty line even if the variable is empty.
Not sure, if this post is still read, but nevertheless.
You should try the following:
On top of the code right after #echo off you have to put in
setlocal enabledelayedexpansion
Additionally anywhere you want to use variables changed in a block of brackets (like For-Loops or If's) you have to change the %into ! to get
!varname!
This should be helping...
Greetings
geisterfurz007
First create a file a.txt in the same directory u have this batch file ... write some text in that...Note: only Windows 2000
Windows ME
Windows XP
Windows Vista
Windows 7 supports FINDSTR
set /p pattern=Enter id:
findstr %pattern% a.txt > __query.tmp
set /p result=<__query.tmp
if %errorlevel%==0 (
set var2= %result%
echo %var2%
set var1= %var2:~5,3%
echo %var1% > test.txt
echo %var1%
) else (
echo error
)
del __query.tmp
pause
run this bath file .. you will find a substring(start=5,length=3) of the first line of string you have in a.txt in a newly created file test.txt. Finally got it working !
The solution for your problem is to put the "echo"s after the if block is completed.
Try this:
#ECHO off
set /p pattern=Enter id:
findstr %pattern% .\a.txt > result
if %errorlevel%==0 (
set var2= <result
set var1=%var2:~5,3%
goto print
) else (
echo error
goto result
)
:print
echo %var2%
echo %var1% > test.txt
echo %var1%
:result
del result
pause
This way you can see the solution as you wanted.
Cheers! ;]
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.