"ECHO is on" is setting in variable - windows

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.

Related

Reference element by index in a list using Batch Script

Trying to obtain an element in a list by its index, using batch script. Here is the code:
#Echo off
setlocal EnableDelayedExpansion
set acc[0]=default
set acc[1]=Account_2
set acc[2]=Account_3
set acc[3]=Account_4
set acc[4]=Account_5
if exist interator.txt (
set /p i=<interator.txt
echo "read: !i!"
echo "!acc[%i%]!"
REM start cmd /c setx AWS_PROFILE !acc[%i%]!
REM start cmd /k python script.py
set /A i=i+1
(echo !i!)>interator.txt
echo "write: !i!"
) else (
(echo 0)>interator.txt
)
Output Received:
"read: 0"
""
"write: 1"
As setx requires the CMD session to be closed, for affect to take place. I am trying a different approach to automate some regular stuff.
Expected Output:
"read: 0"
"default"
"write: 1"
#Echo off
setlocal EnableDelayedExpansion
set "acc[0]=default
set "acc[1]=Account_2"
set "acc[2]=Account_3"
set "acc[3]=Account_4"
set "acc[4]=Account_5"
if exist q65771965.txt (
set /p i=<q65771965.txt
echo "read: !i!"
FOR %%a IN (acc[!i!]) DO (
ECHO "!%%a!"
echo start cmd /c setx AWS_PROFILE "!%%a!"
echo start cmd /k python script.py
)
set /A i=i+1
(echo !i!)
echo "write: !i!"
) else (
(echo 0)
)
GOTO :EOF
OK - small changes to allow this to work on my test environment:
Changed name of file from interator.txt to q65771965.txt (suits my environment)
Removed updating of data file so the modifications are shown on-screen.
Replaced REM start with ECHO start to show the start commands on-screen.
Subtle syntax-oriented change : Use set "var1=data" for setting values - this avoids problems caused by trailing spaces.
Significant change : insert a for loop to transfer indirect values to a metavariable (%%a) and use these.
Possibly-required : I don't use setx much, but I've some memory of the argument's needing to be "quoted"
The problem is, you used echo "%acc[!i!]%" within a codeblock. You need another layer of parsing, like call echo "%%acc[!i!]%%"
As an alternative, restructure your code, so the critical part isn't in a code block:
#Echo off
setlocal EnableDelayedExpansion
set acc[0]=default
set acc[1]=Account_2
set acc[2]=Account_3
set acc[3]=Account_4
set acc[4]=Account_5
if not exist interator.txt (
(echo 0)>interator.txt
goto :eof
)
set /p i=<interator.txt
echo "read: !i!"
echo "%acc[!i!]%"
set /A i=i+1
(echo !i!)>interator.txt
echo "write: !i!"
(this code is functionally identically to yours, just structured in another way)
(btw: it should probably iterator, not interator - but that's only spelling)

log file of script batch

#echo off
call :checkFTP1 %* > all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
call :checkFTP2 %* >> all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
call :checkFTP3 %* >> all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
call :doCommands1 %* >> all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
call :doCommands2 %* >> all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
call :doCommands3 %* >> all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
call :doCommands4 %* >> all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
exit /b
:checkFTP1
#echo off
Setlocal
:: Is folder empty
set _TMP=
for /f "delims=" %%a in ('dir /b "C:\test\folder1"') do set _TMP="%%a"
IF {%_TMP%}=={} (
goto :Exit1
) ELSE (
goto :checkFTP2
)
Endlocal
:checkFTP2
#echo off
Setlocal
:: Is folder empty
set _TMP=
for /f "delims=" %%a in ('dir /b "C:\test\folder2"') do set _TMP="%%a"
IF {%_TMP%}=={} (
goto :Exit2
) ELSE (
goto :checkFTP3
)
Endlocal
:checkFTP3
#echo off
Setlocal
:: Is folder empty
set _TMP=
for /f "delims=" %%a in ('dir /b "C:\test\folder3"') do set _TMP="%%a"
IF {%_TMP%}=={} (
goto :Exit3
) ELSE (
goto :doCommands1
)
Endlocal
:doCommands1
call script1.bat
if %errorlevel% EQU 0 (goto :doCommands2 ) Else ( ECHO error on script 1 ,2,3,4)
exit
:doCommands2
call script2.bat
if %errorlevel% EQU 0 (goto :doCommands3 ) Else ( ECHO Script 1 Completed Successfully , ERRORS on 2,3,4)
exit
:doCommands3
call script3.bat
if %errorlevel% EQU 0 (goto :doCommands4) Else ( ECHO Script 2 Completed Successfully , ERRORS on 3,4)
exit
:doCommands4
call script4.bat
if %errorlevel% EQU 0 (goto :completed1) Else ( ECHO Script 3 Completed Successfully , ERRORS on 4)
exit
:Exit1
Echo Today Date %DATE% at %Time%
Echo ###################FTP-1 FILES MISSING #########################
Exit
:Exit2
Echo Today Date %DATE% at %Time%
Echo ###################FTP-2 FILES MISSING (#########################
Exit
:Exit3
Echo Today Date %DATE% at %Time%
Echo ###################FTP-3 FILES MISSING #########################
Exit
:completed1
Echo Today Date %DATE% at %Time%
Echo ###################all scripts Completed Successfully#########################
Exit
I have above batch file which calls multiple bat files. I have tested the script and it worked fine.
My only issue is that the log file generated contains all information, and it's a large file.
Is it possible to just log comments and echo, and exclude what executed in screen?
For example I don't want 1 file moved to be showing in log file.
I've taken your code and re-written it to use generic functions which deduplicates the code into an a more easily managed form and allows you to add or remove any steps to FTP and Script sections as needed by editing the list of variables.
I also only return output that does not include the "file(s) moved".
Alternatively if you really only want the status lines to print you could change this to just have those print to the log and not have all info print to the log (this is being done because you are putting the redirection to the log on the calls to other steps.
Also the way this was written before it was actually going through all the goto commands and not needing the calls to each at the top.
Here is the refactored code, I haven't tested it but it should be fine I have to step out for a few hours and you can ask any questions and I'll be happy to answer when I have time.
#( Setlocal EnableDelayedExpansion
echo off
SET "_FolderList="C:\test\folder1" "C:\test\folder1" "C:\test\folder1" "
SET "_ScriptList="c:\test\script1.bat" "E:\script2.bat" "C:\Path\To\script3.bat" "C:\Path\To\script4.bat" "
SET "_Scripts_Failed=1,2,3,4"
SET "_Scripts_Completed="
SET "_Continue=0"
CALL :GetDateTime
SET "MasterLog=all_log_all_log_!IsoDate!_!IsoTime!.log"
)
CALL :Main
( Endlocal
EXIT /B
)
:Main
SET /A "_Counter=0"
FOR %%_ IN (%_FolderList%) DO (
IF DEFINED _Continue (
SET /A "_Counter+=1"
CALL :CheckFTP_List %%~_
)
)>> "%MasterLog%"
SET /A "_Counter=0"
FOR %%_ IN (%_FolderList%) DO (
IF DEFINED _Continue (
SET /A "_Counter+=1"
CALL :DoCommands_List %%~_
)
)>> "%MasterLog%"
IF DEFINED _Continue (
Echo Today Date %DATE% at %Time%
Echo ###################all scripts Completed Successfully#########################
)
GOTO :EOF
:CheckFTP_List
REM Is folder empty
for /f "delims=" %%a in ('dir /b /A-D "%*') do (
set "_Continue=%%a" )
IF NOT Defined _Continue (
Echo.Today Date on %DATE% at %Time%
Echo.###################FTP-%_Counter% FILES MISSING #########################
)
GOTO :EOF
:DoCommands_List
call "*%" | FIND /I /V "file(s) moved" &REM only outputs lines that don't contain files moved.
if %errorlevel% NEQ 0 (
SET "_Continue="
SET "_Scripts_Completed=%_Scripts_Completed:,1=1%"
SET "_Scripts_Failed=!_Scripts_Failed:%_Scripts_Completed%=!"
Echo Today Date %DATE% at %Time%
ECHO. Error encountered on Script %Counter%! -- Completed Scripts: !_Scripts_Completed! -- Failed Scripts: !_Scripts_Failed!
) ELSE (
SET "_Scripts_Completed=%_Scripts_Completed:,1=1%,%Counter%"
)
GOTO :EOF
:GetDateTime
FOR /F "Tokens=1-7 delims=MTWFSmtwfsouehrandit:-\/. " %%A IN ("%DATE% %TIME: =0%") DO (
FOR /F "Tokens=2-4 Delims=(-)" %%a IN ('ECHO.^| DATE') DO (
SET "%%~a=%%~A"
SET "%%~b=%%~B"
SET "%%~c=%%~C"
SET "HH=%%~D"
SET "Mn=%%~E"
SET "SS=%%~F"
SET "Ms=%%~G"
)
)
SET "IsoTime=%HH%.%Mn%.%SS%.%Ms%"
SET "IsoDate=%yy%-%mm%-%dd%"
GOTO :EOF

Get a MD5 value from a file using findstr in CMD

I have the following file contents from which I want to extract the MD5 hash value thats on line 2.
How can I do this in a Windows batch file? Unfortunately using powerscript is not an option.
Herewith the input file ( file.txt )
MD5hashoffile20160613190010_Address
f4f855c5cb40767a7227b506f641ceef
CertUtil:-hashfilecommandcompletedsuccessfully.
I wanted to use the findstr utility but the regex I use must be wrong since it's not returning anything.
findstr /R "[a-fA-F0-9]{32}" file.txt
I appreciate any advice.
Thanks
UPDATE :
I have added the full solution in the answer section.
After some trial and error I found a solution that works :
findstr /R "^[a-fA-F0-9]*$" file.txt
Updated Answer with full solution :
SETLOCAL ENABLEDELAYEDEXPANSION
SET "FILESIZE=0"
SET "RECORDCOUNT=0"
SET "MD5HASH="
SET "TEMPHASH="
SET "FILESETREPORT=_UNSET"
SET "LINE=0"
SET "COLUMNS=TableName|RecordCount|FileSize|FileHash"
echo "============================="
echo "= Starting FileSize process ="
echo "============================="
for %%F in (*.csv) do (
echo "Process A CSV File"
for /f "tokens=1,2,3,4 delims=^_" %%A IN ("%%~nF") do (
SET "TABLENAME=%%D"
SET "FILESIZE=%%~zF"
if "!FILESETREPORT!" == "_UNSET" (
SET "FILESETREPORT=%%A_%%B_%%C_FilesetReport.csv"
:: Initialize the headers
echo !COLUMNS! > !FILESETREPORT!
)
)
echo "Get RecordCount in CSV"
if "!TABLENAME!" NEQ "FilesetReport" (
for /f "tokens=*" %%I in ('more +1 %%F ^| find /v /c ""') do SET "RECORDCOUNT=%%I"
echo "Generate File MD5 Hash"
certUtil -hashfile %%~nxF MD5 > hashfile
echo "Get the hash from the file"
for /f "skip=1 tokens=*" %%A in (hashfile) do (
SET "TEMPHASH=%%A"
:: call :cleanHash
if !LINE! == 0 SET "LINE=1" && call :cleanHash
)
:: Reset the Line Number
SET "LINE=0"
echo "Save File Details to FieldsetReport"
echo "RecordCount : !RECORDCOUNT!"
echo "FileSize : !FILESIZE!"
echo "MD5Hash : !MD5HASH!"
echo "TableName : !TABLENAME!"
SET "OUTPUTLINE=!TABLENAME!|!RECORDCOUNT!|!FILESIZE!|!MD5HASH!"
echo !OUTPUTLINE! >> !FILESETREPORT!
:: Cleanup
del hashfile
)
)
echo "File Processing Completed"
exit /b 0
:cleanHash
echo "Remove all spaces from the MD5 hash"
:: for /f "delims=" %%a in ('findstr /R "^[a-fA-F0-9]*$" !TEMPHASH!') do SET TEMPHASH=%%a
SET "MD5HASH=!TEMPHASH: =!"
echo "********************************************************************"
echo !MD5HASH!
ENDLOCAL
You're better off using this:
#echo off
for /f "skip=1" %%a in (file.txt) do set "hash=%%a" &goto breakLoop
:breakLoop
echo %hash%
pause
This should work even if another line also contains only hexadecimal characters, and has the added benefit of putting the md5 hash in a variable, ready to be used.

windows batch script - setting variable inside for loop

maybe i'm not representing my question clear, here's the actual code i did:
#echo off
set /p keywords="Enter keywords to search: " %=%
dir /b *.dat > filelist.txt
for /f "delims=." %%f in (filelist.txt) do (
for /f "delims= " %%g in (%%f.dat) do (
7z e %%g *sec.evtx
dir /b *.evtx > evtfile.txt
set /p tmpvar1=<evtfile.txt
del *.evtx
)
)
filelist.txt
tsnint1.dat
webint1.dat
tsnint1.dat
TSNINT1-201312091700.zip
TSNINT1-201312091600.zip
TSNINT1-201312091500.zip
TSNINT1-201312091400.zip
TSNINT1-201312091300.zip
TSNINT1-201312091200.zip
webint1.dat
WEBINT1-201312091300.zip
WEBINT1-201312091200.zip
the problem i'm facing is, evtfile consists of the right content but tmpvar1 is not assigned correctly as expected, what is my mistake and how to correct it? many thanks
You need delayed expansion to use a variable inside a block when this variable has been set (or changed) inside the same block. But you can set a variable without delayed expansion.
See this little demonstration (I used a simple if construct instead of for, but the effect is the same (not with if or for, but with blocks (inside ( and )).
#echo off
REM SETTING a variable inside a block
set "var=ONE"
echo start: %var%
if 1==1 (
echo inside block: %var%
set var=TWO
echo var has a new value:
set var
echo inside block is still old: %var%
)
echo after block: %var%
echo ----------
REM USING a variable inside a block
setlocal enabledelayedexpansion
set "var=ONE"
echo start: %var%
if 1==1 (
echo inside block: %var%
set var=TWO
echo var has a new value:
set var
echo new value inside block: !var!
echo just to demonstrate: %var%
)
echo after block: %var%
endlocal
echo ----------
echo working fine: %var%
echo not available after endlocal: !var!
This might help, using your code as a base. FWIW I hope that you have more than one copy of the .evtx files, if they are important to you.
#echo off
setocal enabledelayedexapnsion
for /f "delims=" %%f in ('dir /b *.dat') do (
for /f "delims=" %%g in ('type "%%f" ') do (
7z e "%%g" "*sec.evtx"
dir /b *.evtx > evtfile.txt
set /p tmpvar1=<evtfile.txt
echo !tempvar!
del *.evtx
)
)
One issue is that the *.evtx files are being deleted after the first archive is created, so the subsequent ones will have nothing to archive.

Execute SQL from batch file

I'm new to batch files scripting.
All I want is creating a batch file that calling SQL file and store results in text file .
Can anyone help me, your help is highly appreciated,
This is the first time I need to create such files.
using a batch file:
save and run this:
#echo off
sqlplus -s -l user/pass#yourdb #yoursql.sql>your_log.log
p.s. be sure to have the last line of your sql script as exit; or the batch file will hang.
sqlcmd -S sqlservername -i yoursqlfile.sql -U username -P password -o outputfile.txt
I created a more advanced launcher, try it out (you can find the latest version at http://www.unix.com/windows-and-dos-issues-and-discussions/256021-windowss-batch-launcher-oracle-sql-linux-sh-scripts-available-here.html)
Here's the code anyway:
launcher.cmd
#ECHO OFF
rem Script Launcher by Fr3dY v1.4
rem ##############################
rem Version History:
rem 1.4 - Misc. fixes
rem 1.3 - Merged with 'server launcher', now accepts both SQL and SHELL SCRIPTS
rem 1.2 - Interactive prompt to show the file on screen
rem 1.1 - No need to add 'quit;' or 'exit;' in the .sql file anymore
rem Fixed sqlplus waiting for username/password if the first attempt was unsuccessful
rem Log file is generated automatically, including date and time in name
rem Misc. fixes
rem 1.0 - Initial Version
:MAIN
::Path of PLINK
set PLINK="C:\Program Files (x86)\PuTTY\plink.exe"
::List with TNS NAMES
set dbservers=launcher-databases.txt
::List with LINUX SERVERS
set linuxservers=launcher-servers.txt
set dt=%DATE:~6,4%_%DATE:~3,2%_%DATE:~0,2%__%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%
set dt=%dt: =0%
echo Choose launcher mode:
echo 1) Database scripts (.sql files)
echo 2) Shell scripts (.sh files)
set /p launchermode="Insert value: "
echo.
if %launchermode%==1 (
set extension=sql
set servers=%dbservers%
set mode=DB
) else (
if %launchermode%==2 (
set extension=sh
set servers=%linuxservers%
set mode=OS
) else (echo "Incorrect value, exiting..." & goto :END)
)
if exist %servers% (
goto :LISTFILES
) else echo FILE %servers% NOT FOUND, ABORTING & goto :END
:LISTFILES
echo Listing *.%extension% files...
echo.
dir /b *.%extension%
echo.
set /p file=Name of the file to be launched (without extension)?
if exist %file%.%extension% (
goto :CONFIRMSHOW
) else echo FILE %file%.%extension% NOT FOUND, ABORTING & goto :END
:CONFIRMSHOW
echo.
set /p confirm=Show the script %file%.%extension% on screen now?
if %confirm%==y (
goto :SHOWSCRIPT
) else goto :CONFIRMEXEC
echo.
:SHOWSCRIPT
echo.
echo Content of %file%.%extension%
echo ######################
type %file%.%extension%
echo.
echo ######################
echo.
:CONFIRMEXEC
set /p confirm=Are you sure you want to execute this script?
if %confirm%==y (
set /p user=%mode% username?
goto :HInput
) else echo ABORTED & goto :END
echo.
echo Output saved to %file%_%dt%.log
echo.
Goto :END
:HInput
::Hidden.cmd
::Tom Lavedas, 02/05/2013, 02/20/2013
::Carlos, 02/22/2013
::https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/f7mb_f99lYI
::Version 3.0
SetLocal DisableDelayedExpansion
echo.
Echo Enter password:
Set "Line="
Rem Save 0x08 character in BS variable
For /F %%# In (
'"Prompt;$H&For %%# in (1) Do Rem"'
) Do Set "BS=%%#"
:HILoop
Set "Key="
For /F "delims=" %%# In (
'Xcopy /L /W "%~f0" "%~f0" 2^>Nul'
) Do If Not Defined Key Set "Key=%%#"
Set "Key=%Key:~-1%"
SetLocal EnableDelayedExpansion
If Not Defined Key echo. & Goto :HIEnd
If %BS%==^%Key% (Set /P "=%BS% %BS%" <Nul
Set "Key="
If Defined Line Set "Line=!Line:~0,-1!"
) Else Set /P "=*" <Nul
If Not Defined Line (EndLocal &Set "Line=%Key%"
) Else For /F delims^=^ eol^= %%# In (
"!Line!") Do EndLocal &Set "Line=%%#%Key%"
Goto :HILoop
:HIEnd
if %launchermode%==1 (
goto :EXECDB
) else goto :EXECLINUX
:EXECDB
FOR /f %%A IN (%servers%) DO CALL ECHO DATABASE: %%A & ECHO DATABASE: %%A >> %file%_%dt%.log & sqlplus -S -L %user%/!Line!#%%A < %file%.%extension% >> %file%_%dt%.log
goto :END
:EXECLINUX
FOR /f %%A IN (%servers%) DO CALL ECHO SERVER: %%A & ECHO SERVER: %%A >> %file%_%dt%.log & echo y | %PLINK% %user%#%%A -pw !Line! "exit" & %PLINK% %user%#%%A -pw !Line! -batch -m %file%.%extension% >> %file%_%dt%.log & echo. >> %file%_%dt%.log
goto :END
:END
pause

Resources