Batch script to check the content and file name - batch-file

I have a code in batch script which will check if the name of the file is same or content of the file is same or not. Also it will updat the ini file to 0 or 1
0 - if the file names are different
1 - if the file names are different
Below is my code
#ECHO OFF
CLS
del /s C:\deep\output.log > NUL
for %%i in (C:\deep\*.DAT) do (
for /f "tokens=1,2 delims= " %%G in (app.ini) do set %%G=%%H
echo Rungmis %rungmis%
fc C:\deep\MAI_ZSYS_MOVE.DAT %%i > NUL
if errorlevel 1 (
CALL :error
echo C:\deep\MAI_ZSYS_MOVE.DAT and %%i are different >>output.log
set /a rungmis=0
echo Rungmis %rungmis%
timeout 5
) ELSE (
CALL :next
echo C:\deep\MAI_ZSYS_MOVE.DAT and %%i are same >>output.log
set /a rungmis=1
echo Rungmis %rungmis%
timeout 5
)
)
for %%I in (rungmis) do (
setlocal enabledelayedexpansion
type app.ini | find /v "%%I=">settings.tmp
move /y settings.tmp gmisapp.ini
echo %%I=!%%I!>>app.ini
)
type app.ini
timeout 5
It is updating the .ini flag (rungmis flag) to 0 or 1. But the problem which i am facing is whenever the ini is getting updated the flag (rungmis) is getting updated at the last line of the ini
Actual app.ini file
[TO_RUN_GMIS]
rungmis=1
;0 means GMIS will run
;1 means GMIS will not run
[Registry_Directories]
ArchivePath=D:\maibackup\
ImportPath=D:\gmisdata\
ExportPath=D:\www\GMIS\excel\
DataSource=GMIS_DEV_NEW
app.ini after getting updated
[TO_RUN_GMIS]
;0 means GMIS will run
;1 means GMIS will not run
[Registry_Directories]
ArchivePath=D:\maibackup\
ImportPath=D:\gmisdata\
ExportPath=D:\www\GMIS\excel\
rungmis=1
can anyone please help me out with that?

Please give this a try.
#ECHO OFF
CLS
del /s C:\deep\output.log > NUL
for %%i in (C:\deep\*.DAT) do (
for /f "tokens=1,2 delims= " %%G in (app.ini) do set %%G=%%H
echo Rungmis %rungmis%
fc C:\deep\MAI_ZSYS_MOVE.DAT %%i > NUL
if errorlevel 1 (
CALL :error
echo C:\deep\MAI_ZSYS_MOVE.DAT and %%i are different >>output.log
set /a rungmis=0
echo Rungmis %rungmis%
timeout 5
) ELSE (
CALL :next
echo C:\deep\MAI_ZSYS_MOVE.DAT and %%i are same >>output.log
set /a rungmis=1
echo Rungmis %rungmis%
timeout 5
)
)
for /f "tokens=*" %%a in ('type "app.ini" ^| find /v /n "" ^& break ^> "app.ini"') do (
set "str=%%a"
setlocal enabledelayedexpansion
set "str=!str:*]=!"
if "!str:~0,7!"=="rungmis" set "str=!str:~0,-1!%rungmis%"
>>app.ini echo(!str!
endlocal
)
type app.ini
timeout 5
So we simply do a type on the file and add some additional characters to ensure we replicate newlines as well. We then get rid of those characters before printing to file. But first we search for the string rungmis and replaces its value with the value you determined earlier in the script. Then each line gets printed back into the app.ini file with the replacement value.

Related

How can we split string using windows bat

How can we split string using windows bat script?
for below .bat code snippet
#echo off & setlocal EnableDelayedExpansion
set j=0
for /f "delims=""" %%i in (config.ini) do (
set /a j+=1
set con!j!=%%i
call set a=%%con!j!%%
echo !a!
(echo !a!|findstr "^#">nul 2>nul && (
rem mkdir !a!
) || (
echo +)
rem for /f "tokens=2" %%k in(config.ini) do echo %%k
)
)
pause
below config file
Q
What's wrong when I del rem at the begin of rem for /f "tokens=2" %%k in(config.ini) do echo %%k
How can I get the /path/to/case and value as a pair?
for /f xxxx in (testconfig.ini) do (set a=/path/to/case1 set b=vaule1)
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q43407067.txt"
set j=0
for /f "delims=""" %%i in (%filename1%) do (
set /a j+=1
set con!j!=%%i
call set a=%%con!j!%%
echo !a! SHOULD BE EQUAL TO %%i
(echo !a!|findstr "^#">nul 2>nul && (
echo mkdir !a!
) || (
echo +)
for /f "tokens=2" %%k IN ("%%i") do echo "%%k"
for /f "tokens=1,2" %%j IN ("%%i") do echo "%%j" and "%%k"
)
)
ECHO ----------------------------
SET con
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used a file named q43407067.txt containing your data for my testing.
(These are setting that suit my system)
SO - to address your problems:
because the ) on that line closes the ( on the previous. The ) on that line closes the ( on the one prior. (I changed the rem to an echo so that the code would produce something visible) The first ( on the (echo !a! line is closed by the ) on the line following the (now) two for /f commands. and the ( on the for..%%i..do( is closed by the final ) before the echo -----
You can't delete that ) because it's participating in a parenthesis-pair.
You need a space between the in and the (.
I've shown a way. See for /?|more from the prompt for documentation (or many articles here on SO)
In your code, !a! is the same as %%i - so I've no idea why you are conducting all the gymnastics - doubtless to present a minimal example showing the problem.
Note that since the default delimiters include Space then if any line contains a space in the /path/to/case or value then you'll have to re-engineer the approach.
I' not sure if I understand what exactly it is you need, so what follows may not suit your needs:
#Echo Off
SetLocal EnableDelayedExpansion
Set "n=0"
For /F "Delims=" %%A In (testConfig.ini) Do (Set "_=%%A"
If "!_:~,1!"=="#" (Set/A "n+=1", "i=0"
Echo=MD %%A
Set "con[!n!]!i!=%%A") Else (For /F "Tokens=1-2" %%B In ('Echo=%%A'
) Do (Set/A "i+=1"
Set "con[!n!]!i!=%%B"&&Set/A "i+=1"&&Set "con[!n!]!i!=%%C")))
Set con[
Timeout -1
GoTo :EOF
remove Echo= on line 6 if you are happy with the output and really want to create those directories

Windows Batch FOR Loop improvement

I have a batch to check the duplicate line in TXT file (over one million line) with 13MB, that will be running over 2hr...how can I speed up that? Thank you!!
TXT file
11
22
33
44
.
.
.
44 (over one million line)
Existing Batch
setlocal
set var1=*
sort original.txt>sort.txt
for /f %%a in ('type sort.txt') do (call :run %%a)
goto :end
:run
if %1==%var1% echo %1>>duplicate.txt
set var1=%1
goto :eof
:end
This should be the fastest method using a Batch file:
#echo off
setlocal EnableDelayedExpansion
set var1=*
sort original.txt>sort.txt
(for /f %%a in (sort.txt) do (
if "%%a" == "!var1!" (
echo %%a
) else (
set "var1=%%a"
)
)) >duplicate.txt
This method use findstr command as in aschipfl's answer, but in this case each line and its duplicates are removed from the file after being revised by findstr. This method could be faster if the number of duplicates in the file is high; otherwise it will be slower because the high volume data manipulated in each turn. Just a test may confirm this point...
#echo off
setlocal EnableDelayedExpansion
del duplicate.txt 2>NUL
copy /Y original.txt input.txt > NUL
:nextTurn
for %%a in (input.txt) do if %%~Za equ 0 goto end
< input.txt (
set /P "line="
findstr /X /C:"!line!"
find /V "!line!" > output.txt
) >> duplicate.txt
move /Y output.txt input.txt > NUL
goto nextTurn
:end
#echo off
setlocal enabledelayedexpansion
set var1=*
(
for /f %%a in ('sort q42574625.txt') do (
if "%%a"=="!var1!" echo %%a
set "var1=%%a"
)
)>"u:\q42574625_2.txt"
GOTO :EOF
This may be faster - I don't have your file to test against
I used a file named q42574625.txt containing some dummy data for my testing.
It's not clear whether you want only one instance of a duplicate line or not. Your code would produce 5 "duplicate" lines if there were 6 identical lines in the source file.
Here's a version which will report each duplicated line only once:
#echo off
setlocal enabledelayedexpansion
set var1=*
set var2=*
(
for /f %%a in ('sort q42574625.txt') do (
if "%%a"=="!var1!" IF "!var2!" neq "%%a" echo %%a&SET "var2=%%a"
set "var1=%%a"
)
)>"u:\q42574625.txt"
GOTO :EOF
Supposing you provide the text file as the first command line argument, you could try the following:
#echo off
for /F "usebackq delims=" %%L in ("%~1") do (
for /F "delims=" %%K in ('
findstr /X /C:"%%L" "%~1" ^| find /C /V ""
') do (
if %%K GTR 1 echo %%L
)
)
This returns all duplicate lines, but multiple times each, namely as often as each occurs in the file.

How to scan a folder and store all file names in a array variable and then loop through the array?

I am new to batch and I am trying to do the following:
reading or scanning a folder
hold all file names in folder in an array variable (need to hold file name with or without extension)
loop through that array and create a call to a specific file/bat based on the type of file using an IF or CASE statement condition. example: if the filename has the word person in it, call a specific file/bat.
This is what I have so far:
#echo off
setlocal EnableDelayedExpansion
rem Populate the array with existent files in folder
set i=0
for %%b in (*.*) do (
set /A i+=1
set list[!i!]=%%b
)
set Filesx=%i%
rem Display array elements
for /L %%i in (1,1,%Filesx%) do echo !list[%%i]!
... do (
echo !list[%%i]! | find /i "person" >nul && call specific.bat !list[%%i]!
)
echo !list[%%i]! | find /i "person": find the word
>nul: disregard the output (we don't need it, just the errorlevel)
&&: if previous command was successful (the word was found), then...
Do you really need that array? You can do it "on the fly":
for %%b in (*.*) do (
echo %%b | find /i "person" >nul && call specific.bat "%%b"
)
for filename only, use %%~nb, for full name (including path), use %%~fb (see for /? for more options)
This a quick modified version from : Open a file through cmd and display the selected in specific editor
It will scan on your desktop for any batch files which contains the word : person
#ECHO OFF
Title Scan a folder and store all files names in an array variables
:MenuLoop
Cls & Color 0A
SETLOCAL
SET "ROOT=%userprofile%\Desktop\"
SET "EXT=*.bat"
SET "Count=0"
Set "Word2Search=Person"
SETLOCAL enabledelayedexpansion
REM Iterates throw the files on this current folder.
REM And Populate the array with existent files in folder
FOR /f "delims=" %%f IN ('dir /b /s "%ROOT%\%EXT%"') DO (
find /I "%Word2Search%" "%%f" >nul 2>&1 && (
SET /a "Count+=1"
set "list[!Count!]=%%~nxf"
set "listpath[!Count!]=%%~dpFf"
) || (
Call :Scanning
)
)
echo wscript.echo Len("%ROOT%"^) + 20 >"%tmp%\length.vbs"
for /f %%a in ('Cscript /nologo "%tmp%\length.vbs"') do ( set "cols=%%a")
If %cols% LSS 50 set /a cols=%cols% + 15
set Files=%Count%
set /a lines=%Count% + 10
Mode con cols=%cols% lines=%lines%
ECHO *******************************************************
ECHO Folder : "%ROOT%"
ECHO *******************************************************
echo(
rem Display array elements
for /L %%i in (1,1,%Files%) do echo [%%i] : !list[%%i]!
SET /a "COUNT_TOT=%Count%"
ECHO.
ECHO Total of [%EXT%] files(s) : %Count% file(s)
echo(
echo Type the number of what file did you want to edit ?
set /p "Input="
set "sublimeEXE=%programfiles%\Sublime Text 3\sublime_text.exe"
For /L %%i in (1,1,%Count%) Do (
If "%INPUT%" EQU "%%i" (
Rem Testing if sublime_text.exe exist to open with it the text file
If Exist "%sublimeEXE%" (
Start "Sublime" "%sublimeEXE%" "!listpath[%%i]!"
Rem Otherwise we open the text file with defalut application like notepad
) else (
Start "" Notepad.exe "!listpath[%%i]!"
)
)
)
EndLocal
Goto:MenuLoop
:Scanning
mode con cols=75 lines=3
Cls & Color 0A
echo(
echo Scanning in progress ...
goto :eof

Some portions of code in batch files are not executing

I have a problem in this batch file:
#echo off
setlocal enableextensions EnableDelayedExpansion
for /f "tokens=*" %%l in (input1.txt) do (
ping %%l> "Result.txt"
set "var=HI"
set "var1=hi"
set "var2=1";
FIND /c "Destination host unreachable." Result.txt && ( set "var2=2") || ( echo HI)
FIND /c "Request timed out." Result.txt && ( set "var2=2" ) || (echo HI)
if "!var2!" EQU "2" (echo %%l>>"failure.txt")
# This block doesn't work
if "!var2!" EQU "1" (
for /f "tokens=*" %%i in (Result.txt) do ( set var=%%i)
for /f "tokens=9" %%j in ("%var%") do (set var1=%%j)
set var1="!var1:~0,-2!"
if "!var1!" LSS "1000" (echo %%l >> "success.txt") ELSE (echo %%l >>"timeout.txt")
)
)
endlocal
The above code is designed for ping bulk list of servers and redirect the servers to successful or failure text files based on the test results. Here the problem is the code marked by a rem remark is not working. It seems that portion is not executed. Also var1 is not being evaluated. Thanks in advance.
This will give you success or failure.
#echo off
for /f "delims=" %%a in (input1.txt) do (
ping %%l >nul
if errorlevel 1 (
>>"failure.txt" echo %%l
) else (
>>"success.txt" echo %%l
)
)
A problem in your code is that you are forcing string compares by using quotes around "!var1!" and the numeral.
You also have two sets of quotes around !var1!

Batch Script Substring from string

Ok, I have been playing around with this for a while and am getting no where. I need to pull the KB number out from a line.
The issue i am having is that some of the KB numbers are 6 characters and some are 7, and can't seem to find a way that will work to error detect the two.
The Two types of errors this makes is as follows
The First one should only have been displayed 6 characts so it added the extra "-" at the end.
x64 KB890830- 2012\MS12-000\WinSec-KB890830-006-P58310-Windows-KB890830-x64-V4.9.exe
While the second error shows the random "_" because it uses the first KB shown not the second.
ia64 KB_942288 2012\MS12-000\WinSec-KB_942288-007-P58312-WindowsServer2003-KB942288-v4-ia64.exe
EDIT
Batch File So Far
#ECHO OFF
SETLOCAL enableDelayedExpansion
IF EXIST Export.csv DEL Export.csv
FOR /F "tokens=*" %%I in ('dir /s /b *.*') DO CALl:Generate "%%I"
pause
:Generate
SETLOCAL
IF "%~x1" NEQ ".exe" (
If "%~x3" NEQ ".msu" (
GOTO:EOF
)
)
CALL:FindArchitecture %1
CALL:FindKB %1
CALL:PathFix %1
ECHO %Architecture%,%KB%,%FilePath%>>Export.csv
CALL:Cleanup
ENDLOCAL
GOTO:EOF
:FindArchitecture
ECHO %1 | FINDSTR "x64"
IF "%ERRORLEVEL%"=="0" (
SET Architecture=x64
SET Count+=1
)
ECHO %1 | FINDSTR "x86"
IF "%ERRORLEVEL%"=="0" (
SET Architecture=x86
SET Count+=1
)
ECHO %1 | FINDSTR "ia64"
IF "%ERRORLEVEL%"=="0" (
SET Architecture=ia64
SET Count+=1
)
IF "%Count%" GTR "1" (
SET Architecture=Error
)
SET Count=0
GOTO:EOF
:FindKB
set KBNum="%~1"
set "KBNum=!KBNum:*-KB=!"
ECHO !KBNum!|findstr /I /E /R /c:"-KB[0-9][0-9][0-9][0-9][0-9][0-9][0-9]" >nul
SET "KB=KB!KBNum:~0,7!"
IF "%KB%" NEQ "" GOTO:EOF
ECHO !KBNum!|findstr /I /E /r /c:"-KB[0-9][0-9][0-9][0-9][0-9][0-9]" >nul
SET "KB=KB!KBNum:~0,6!"
GOTO:EOF
:PathFix
set Path_to_convert=%~1
set Reference_path=%~dp0
set FilePath=!Path_to_convert:*%Reference_path%=!
GOTO:EOF
:Cleanup
SET KBNum=
SET KB=
SET Count=
SET Architecture=
set InstallerPath=
set PathRemoval=
set Path=
GOTO:EOF
OK - siginificant edit after seeing comments from Ken White and the OP.
I'm not sure if you need this, but FINDSTR with a regular expression can validate that the line has the pattern: "-KB" followed by 7 digits, followed by "-".
echo somestring|findstr /r /c:"-KB[0-9][0-9][0-9][0-9][0-9][0-9][0-9]-" >nul
Then use substitution to remove everything from the beginning through "KB-".
Then use FINDSTR to verify that the first 7 remaining characters are digits. If not, then loop back and replace up to the next "-KB", etc.
Then you just need to take the first remaining 7 characters.
#echo off
:parseVal
setlocal enableDelayedExpansion
set val="%~1"
echo !val!|findstr /r /c:"-KB[0-9][0-9][0-9][0-9][0-9][0-9][0-9]-" >nul || (
echo Invalid format - KB value not found
exit /b
)
:parseVal2
set "val=!val:*-KB=!"
echo !val!|findstr /brc:"[0-9][0-9][0-9][0-9][0-9][0-9][0-9]-" >nul || goto :parseVal2
set "val=KB!val:~0,7!"
echo val=!val!
exit /b
EDIT
I'm not sure what you did to accept the 6 digit numbers, but the following regex expressions will work with numbers of any length.
For the 1st regex: findstr /rc:"-KB[0-9]*-"
For the 2nd regex: findstr /brc:"[0-9]*-"
Then you can use either of the following to extract out the number when you don't know the length:
for /f "delims=-" %%A in ("!val!") do set "val=KB%%A"
or
set val=KB%val:-=&REM %"

Resources