batch script - comparing two arrays - batch-file

I am trying to compare two arrays and if any array element is same then I am printing. I am not getting the proper output. Only one element is displaying (rm.war). See the below batch file and the output.
Code:
#echo off
setlocal EnableDelayedExpansion
echo "Application list"
set all_apps=(jts.war rm.war ccm.war dm.war relm.war qm.war)
set apps_len=0
for %%b in %all_apps% do (
set /A apps_len=apps_len+1
set apps[!apps_len!]=%%b
echo %%b
)
echo "Installed Applications"
cd /d D:\IBM\WebSphere\AppServer\profiles\AppSrv01\bin
set cmd=call wsadmin.bat -lang jython -f C:/temp/tt.py -user wsadmin -password wsadmin
set insapps_len=0
for /f %%a in ('%cmd%') do (
set /A insapps_len=insapps_len+1
set inslist[!insapps_len!]=%%a
echo %%a
)
echo "Compare arrays"
for /l %%i in (1,+1,%apps_len%) do (
for /l %%j in (%insapps_len%,-1,1) do (
if !apps[%%i]!==!inslist[%%j]! echo !inslist[%%j]!
)
)
Output:
"Application list"
jts.war
rm.war
ccm.war
dm.war
relm.war
qm.war
"Installed Applications"
WASX7209I:
DefaultApplication
admin.war
ccm.war
clmhelp.war
converter.war
ivtApp
jts.war
qm.war
query
rm.war
"Compare arrays"
rm.war

Test this to see if it functions:
#echo off
set all_apps="jts.war" "rm.war" "ccm.war" "dm.war" "relm.war" "qm.war"
set "installed="
cd /d "D:\IBM\WebSphere\AppServer\profiles\AppSrv01\bin"
set cmd=call wsadmin.bat -lang jython -f C:/temp/tt.py -user wsadmin -password wsadmin
for /f %%a in ('%cmd%') do call set installed="%%a" %%installed%%
echo duplicates:
for %%a in (%all_apps%) do (
for %%b in (%installed%) do (
if "%%~a"=="%%~b" echo %%~a
)
)
pause

Related

How can I delete last "\" from file's path in batch script

I was trying this, it'll count file's line after I copy the file's path (Shift+right click >copy as path) and put it in batch file, but.... how do I fix it??
the last \ in %path% is causing problem.
#echo off
Setlocal EnableDelayedExpansion
set /p ifilename=Enter file name:
for %%f in (%ifilename%) do (
set paath=%%~df%%~pf
set ifilename=%%~nf%%~xf
)
echo %paath%
echo %ifilename%
for /f "usebackq" %%a in (`dir /b /s %1 "%paath%"`) do (
for /f "usebackq" %%b in (`type %ifilename% ^| find "" /v /c`) do (
set lines= %%b
)
)
echo %lines%
pause
>> the last \ in %path% is causing problem
It's easy to solve this , the code is :
set TempDir=C:\0TEMP
#echo off
md %TempDir%
cd /d %TempDir%
::------
#echo off
#echo on
Setlocal EnableDelayedExpansion
::set /p ifilename=Enter file name:
SET DUMMYexe=%TempDir%\DUMMY.exe
IF EXIST "%DUMMYexe%" goto ll123
ECHO ---------writing
pause
(
ECHO pause1
ECHO pause2
ECHO pause3
) > %DUMMYexe%
:ll123
SET ifilename=%DUMMYexe%
for %%f in (%ifilename%) do (
set fpath=%%~df%%~pf
set ifilename=%%~nf%%~xf
)
echo %fpath%
echo %ifilename%
SET v=asdf1234
SET vv=\%v%
SET vvv=%fpath%%vv%
CALL SET v=%%vvv:\%vv%=%%
echo 111---%v%
pause
set fpath=%v%
for /f "usebackq" %%a in (`dir /b /s %1 "%fpath%"`) do (
for /f "usebackq" %%b in (`type %ifilename% ^| find "" /v /c`) do (
set lines= %%b
)
)
echo %lines%
echo on
pause
goto
But of course, if I use the var 'path', my win10 will report :
'find' is not recognized as an internal or external command, operable program or batch file.
BTW, Maybe you'd be interested in the code below :
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (`dir `) DO (
SET var!count!=%%F
SET /a count=!count!+1
)
ECHO -------------- %count%
ECHO
ECHO %var1%
ECHO %var2%
ECHO %var3%
ENDLOCAL
pause
which I tested after copying from:
How to set commands output as a variable in a batch file
Very useful info about "Setlocal EnableDelayedExpansion" can be found in:
How do SETLOCAL and ENABLEDELAYEDEXPANSION work?

Batch file to automate STM32 flashing

In order to flash using ST-Link Utility, I wrote the following command which works:
"C:\Program Files\STMicroelectronics\STM32 ST-Link Utility\ST-Link_CLI.exe" -c SN=XXXXXXXXXXXXX SWD UR FREQ=400 -P "C:\flash STM32\test.hex" -V -HardRst
I would like to automate this by getting the serial number using the ST-Link_CLI.exe commands and the .hex file in the folder where the batch file sits, so that I can just click this .bat file to flash using any debugger (SN) that is connected.
I can get the serial number using this:
#echo off
setlocal EnableDelayedExpansion
set "output_cnt=0"
for /F "delims=" %%f in ('"C:\Program Files\STMicroelectronics\STM32 ST-LINK Utility\ST-LINK Utility\ST-LINK_CLI.exe" -List') do (
set /a output_cnt+=1
set "output[!output_cnt!]=%%f"
)
for /L %%a in (1 1 !output_cnt!) do call echo !output[%%a]!
pause
set "str=SN"
for /L %%n in (1 1 !output_cnt!) do (
echo !output[%%n]!|find "SN" >nul
if errorlevel 1 (echo notfound) else (echo found at!output[%%n]!)
)
pause
!output[%%a]! is " SN:XXXXXXXXXXX" when the line in the output_cnt contains the string SN
I am a complete novice when it comes to writing effective batch scripts and was wondering if anyone could help me automate this task.
Updated code:
set "STfilepath=C:\Program Files\STMicroelectronics\STM32 ST-LINK Utility\ST-LINK Utility\ST-LINK_CLI.exe"
set "output_cnt=0"
for /F "delims=" %%f in ('"!STfilepath!" -List') do (
set /a output_cnt+=1
set "output[!output_cnt!]=%%f"
)
::iterate thru output array to find SN and parse it to get the serial number
set "serial=NONE"
for /L %%n in (1 1 !output_cnt!) do (
echo !output[%%n]!
FOR /F "tokens=1,* delims=: " %%1 in ("!output[%%n]!") do (
if "%%1" == "SN" (
set "serial=%%2"
)
)
)
echo serial number is !serial!
set "hexfile=NONE"
for /r %%i in (*.hex) do (set "hexfile=%%i")
echo hexfile is !hexfile!
set "CMD="!STfilepath!" -c SN=!serial! SWD UR FREQ=400 -P "!hexfile!" -V -HardRst"
echo %CMD%
%CMD%
pause
You could split the string with a FOR /F loop.
...
set "serial=NONE"
for /L %%n in (1 1 !output_cnt!) do (
echo !output[%%n]!
FOR /F "tokens=1,* delims=: " %%1 in ("!output[%%n]!") do (
if "%%1" == "SN" (
set "serial=%%2"
)
)
)
echo Found serial: !serial!

Get output of findstr in variable

Im absolutely new to batch scripting and wondering how can i get the output of findstr into a variable?
#echo off
set /p assetPath="Set asset path: "
setlocal enableDelayedExpansion
for /r %assetPath% %%p in (*.png) do (
findstr "guid" "%%p".meta
if not !errorlevel!==0 (
echo Could not find guid of file: %%p!
)
)
pause
findstr will output
"guid: d46decd9d3bbf0d46b31a3d4ae0f18ff"
for each file which contains a guid, how can i do operations on this and save the guid into a variable?
#echo off
set /p assetPath="Set asset path: "
setlocal enableDelayedExpansion
SET /A COUNT=0
for /r %assetPath% %%p in (*.png) do (
FOR /F "TOKENS=1*" %%f in (' findstr "guid" "%%p".meta ') DO (
if not !errorlevel!==0 (
echo Could not find guid of file: %%p!
) ELSE (
SET /A COUNT+=1
SET "FILE[!COUNT!]=%%p"
SET "GUID[!COUNT!]=%%g"
))
)
FOR /L %%x in (1,1,%COUNT%) DO echo !GUID[%%x]! is guid for !FILE[%%x]!
pause
This should set FILE[*] and GUID[*] for the files found and report the results.

Speed up batch code

Can you advice what else I can do for speeding up my batch, please?
Works quite well apart takes ages to finish :)
Sorry for this text but I'm not able to post my question due to the message 'it looks like your post is mostly code bla bla bla. Admin- can you turn this verification OFF!!!
#echo off
c:
cd \
pushd \\ftp\ftp$
cls
echo ________________________________________________________________
echo.
color f9
:WPIS
set /p moje=Please enter required LOGIN NAME:
if exist "\\ftp\ftp\Transfer\%moje%" echo USER ALREADY EXIST TRY ANOTHER ONE && GOTO WPIS
:KOD
set mojep=%random%%random%%random%
setlocal enabledelayedexpansion
set input=default2015.Archive
set output2=%moje%.Archive2
set output1=%moje%.Archive1
set output=%moje%.Archive
set text2searchfor=default2015
set password2searchfor=szukajpassword
set folder2search=F:\\Transfer\\default2015
set newfolder=F:\\Transfer\\%moje%
del %output1%
cls
echo Wait....
for /F "tokens=*" %%f in ('type %input%') do (
set line=%%f
if "!line!"=="%text2searchfor%" (
set NAME=%moje%
echo !NAME!>> %output2%
) else (
echo !line!>> %output2%
)
)
for /F "tokens=*" %%f in ('type %output2%') do (
set line=%%f
if "!line!"=="%folder2search%" (
set NAME=%newfolder%
echo !NAME!>> %output1%
) else (
echo !line!>> %output1%
)
)
for /F "tokens=*" %%f in ('type %output1%') do (
set line=%%f
if "!line!"=="%password2searchfor%" (
set NAME=%mojep%
echo !NAME!>> %output%
) else (
echo !line!>> %output%
)
)
del %output1%
del %output2%
pushd \\ftp\ftp\Transfer\
md %moje%
popd \\ftp\ftp\Transfer\
popd \\ftp\ftp$
...
Try this:
#echo off
c:
cd \
pushd \\ftp\ftp$
cls
echo ________________________________________________________________
echo.
color f9
:WPIS
set /p moje=Please enter required LOGIN NAME:
if exist "\\ftp\ftp\Transfer\%moje%" echo USER ALREADY EXIST TRY ANOTHER ONE && GOTO WPIS
:KOD
set mojep=%random%%random%%random%
setlocal
set input=default2015.Archive
set output2=%moje%.Archive2
set output1=%moje%.Archive1
set output=%moje%.Archive
set text2searchfor=default2015
set password2searchfor=szukajpassword
set folder2search=F:\\Transfer\\default2015
set newfolder=F:\\Transfer\\%moje%
cls
echo Wait....
(for /F "delims=" %%f in (%input%) do (
if "%%f"=="%text2searchfor%" (
echo %moje%
) else (
echo %%f
)
)) > %output2%
(for /F "delims=" %%f in (%output2%) do (
if "%%f"=="%folder2search%" (
echo %newfolder%
) else (
echo %%f
)
)) > %output1%
(for /F "delims=" %%f in (%output1%) do (
if "%%f"=="%password2searchfor%" (
echo %mojep%
) else (
echo %%f
)
)) > %output%
del %output1%
del %output2%
pushd \\ftp\ftp\Transfer\
md %moje%
popd \\ftp\ftp\Transfer\
popd \\ftp\ftp$
If more speed is needed, a much faster solution may be written via a Batch-JScript hybrid script.
This uses a native Windows batch script called Jrepl.bat (by dbenham)
- download from: https://www.dropbox.com/s/4otci4d4s8x5ni4/Jrepl.bat
and it can also be found here: http://www.dostips.com/forum/viewtopic.php?f=3&t=6044
It is significantly faster for large files than plain vanilla for loops.
This assumes that your strings to be replaced are not embedded in any other lines, and just occur by themselves.
As you are changing the directory, placing jrepl.bat on the system path is wise so the script can find it, or hard code the path to jrepl.bat
#echo off
cd /d c:\
pushd \\ftp\ftp$
cls
echo ________________________________________________________________
echo.
color f9
:WPIS
set /p moje=Please enter required LOGIN NAME:
if exist "\\ftp\ftp\Transfer\%moje%" echo USER ALREADY EXIST TRY ANOTHER ONE && GOTO WPIS
:KOD
set mojep=%random%%random%%random%
setlocal enabledelayedexpansion
set input=default2015.Archive
set output2=%moje%.Archive2
set output1=%moje%.Archive1
set output=%moje%.Archive
set text2searchfor=default2015
set password2searchfor=szukajpassword
set folder2search=F:\\Transfer\\default2015
set newfolder=F:\\Transfer\\%moje%
cls
echo Wait....
call jrepl "%text2searchfor%" "%moje%" /L /f %input% /o %output%
call jrepl "%folder2search%" "%newfolder%" /L /f %output% /o -
call jrepl "%password2searchfor%" "%mojep%" /L /f %output% /o -
pushd \\ftp\ftp\Transfer\
md %moje%
popd \\ftp\ftp\Transfer\
popd \\ftp\ftp$

batch file which creates a new batch file

i need a batch file "master.bat" which on executing creates a new batch file "children.bat" with the given doce on it:
Code is
#echo off
"pre.txt" (for /f "tokens=3,4" %%a in (
'net statistics workstation ^| find "since"'
) do (
echo %%a %%b
))
exit
To create a new batch file containg that code:
Just echo the code and send it into the file you want (>)
echo #echo off "pre.txt" (for /f "tokens=3,4" %%a in ( 'net statistics workstation ^| find "since"' ) do ( echo %%a %%b )) exit > mybat.bat
To just run that code in a way that works:
for /f "tokens=3,4" %%a in ( 'net statistics workstation ^| find "since"' ) do ( echo %%a %%b >> pre.txt )
Create and run batch file
echo #echo off > children.bat
echo for /f "tokens=3,4" %%%%a in ( 'net statistics workstation ^^^| find "since"' ) do ( echo %%%%a %%%%b ^>^> pre.txt ) >> children.bat && children.bat
#echo off
title Just a sandbox test
color f
:start
cls
set "file=%userprofile%\Desktop\children.bat
(
echo ^#echo off
echo ^"pre.txt^" ^(for /f ^"tokens=3,4^" ^%^%a in ^( ^'net statistics
workstation ^^^| find echo ^"since^"^' ^) do ^( echo ^%^%a ^%^%b ^)^) exit
) >%file%
::rest of your code goes here I guess
pause
Just tested and this will work. Adding ^ before characters that will normally conflict with the syntax will force the script to ignore it. :)
Ps. Sorry it's a bit late lol

Resources