Read content of files and check if values are same - windows

I have one file located at C:\Users\abc\Desktop named BUIBNESSDATE.
File contents are
Updated on :
Thu Jan 23 04:05:00 IST 2014
ProfileBusinessDate=23/1/2014
NucleusBusinessDate=23/01/2014
I want script which will check if both have same date(ProfileBusi nessDate & NucleusBusinessDate )date are same. If both date are same then script should give message as OK else NotOk.
how to do?

Test this:
#echo off
set "file=C:\Users\abc\Desktop\BUIBNESSDATE"
for /f "tokens=2 delims==" %%a in (' find /i "profile" ^< "%file%" ' ) do set "p=%%a"
for /f "tokens=2 delims==" %%a in (' find /i "nucleus" ^< "%file%" ' ) do set "n=%%a"
if "%p%"=="%n%" (echo OK) else (echo NotOK)

#ECHO OFF
SETLOCAL
SET "ppd="
SET "nbd="
FOR /f "delims=" %%a IN (q21312106.txt) DO (
SET "line=%%a"
CALL :process
)
IF NOT DEFINED ppd ECHO ProfileBusinessDate missing&GOTO :EOF
IF NOT DEFINED nbd ECHO NucleusBusinessDate missing&GOTO :EOF
FOR /f "tokens=1-3delims=/" %%a IN ("%ppd%") DO CALL :slz ppdd %%a&CALL :slz ppdm %%b&CALL :slz ppdy %%c
FOR /f "tokens=1-3delims=/" %%a IN ("%nbd%") DO CALL :slz nbdd %%a&CALL :slz nbdm %%b&CALL :slz nbdy %%c
IF "%ppdd%-%ppdm%-%ppdy%"=="%nbdd%-%nbdm%-%nbdy%" (ECHO OK) ELSE (ECHO NotOk)
GOTO :EOF
:process
SET "$1=%line:*ProfileBusinessDate=%"
IF NOT "%$1%"=="%line%" SET "ppd=%$1:~1%"&GOTO :EOF
SET "$1=%line:*NucleusBusinessDate=%"
IF NOT "%$1%"=="%line%" SET "nbd=%$1:~1%"&GOTO :EOF
GOTO :EOF
:: suppress a leading 0
:slz
SET "$1=%2"
IF "%$1:~0,1%"=="0" (SET "%1=%$1:~1%") ELSE (SET "%1=%2")
GOTO :eof
Should do the task. I used q21312106.txt with your data as a source file.

#echo off
setlocal enableextension disabledelayedexpansion
set "pdb="
set "nbd="
for /f "tokens=1,2 delims==" %%a in (
'findstr /b /l /c:"ProfileBusinessDate=" /c:"NucleusBusinessDate=" "c:\users\abc\desktop\buibnessdate"'
) do (
if "%%a"=="ProfileBusinessDate" (set "pbd=%%b" ) else (set "nbd=%%b")
)
if "%pdb%"=="%nbd%" (
echo OK
) else (
echo NOT OK
)
Search the file for the required lines, splitting them on equal sign and assigning the values to the adecuated variables. Then check if both variables hold the same content.

Related

Batch incremental name change only updates a few files at a time

I am attempting to create a batch file that will rename a series of file based on an XML file. It works until it encounters a file name with the same name at which point it skips the file. I was able to amend the script to create an incremental version of the file, the issue that I am having now is the script will cycle through just a few files and then it exits. Any ideas why it's doing that?
for %%z in ("C:\Recordings\AT1*.WAV") do (
for /f tokens^=4^,8^,10^,12^ delims^=^" %%a in ('type "C:\Recordings\index.xml"^|find /i "%%~nxz"') do (
for /f "tokens=1,2 delims=:" %%t in ("%%b") do (
ren "%%z" "%%c-%%t-%%u_%%d%%~xz" 2>nul
if errorlevel 1 set "Number=2" & call :NumberedRename "%%z" "%%c_%%t-%%u_%%d%%~xz"
goto :EOF
)
)
)
)
:NumberedRename
if exist "%~n2_%Number%%~x2" set /A "Number+=1" & goto NumberedRename
ren %1 "%~n2_%Number%%~x2"
goto :EOF
Here is an example of the output that I am able to see at the moment:
C:\Recordings>for %z in ("C:\Recordings\AT1*.WAV") do (for /F tokens=4,8,10,12 delims=" %a in ('type "C:\Recordings\index.xml"|find /i "%~nxz"') do (for /F "tokens=1,2 delims=:" %t in ("%b") do (
ren "%z" "%c-%t-%u_%d%~xz" 2>nul
if errorlevel 1 set "Number=2" & call :NumberedRename "%z" "%c_%t-%u_%d%~xz"
goto :EOF
) ) )
C:\Recordings>(for /F tokens=4,8,10,12 delims=" %a in ('type "C:\Recordings\index.xml"|find /i "AT1_ID1_TT3_ID6-1626034093.52156.WAV"') do (for /F "tokens=1,2 delims=:" %t in ("%b") do (
ren "C:\Recordings\AT1_ID1_TT3_ID6-1626034093.52156.WAV" "%c-%t-%u_%d.WAV" 2>nul
if errorlevel 1 set "Number=2" & call :NumberedRename "C:\Recordings\AT1_ID1_TT3_ID6-1626034093.52156.WAV" "%c_%t-%u_%d.WAV"
goto :EOF
C:\Recordings>if exist "John Doe 1_2021-07-11 15-08_9394056960_2.WAV" set /A "Number+=1" & goto NumberedRename
C:\Recordings>ren "C:\Recordings\AT1_ID1_TT3_ID6-1626034093.52156.WAV" "John Doe 1_2021-07-11 15-08_9394056960_2.WAV"
) ) )
Shortly after posting the question I tinkered with the script and realized that one of the goto :EOF was causing the script to end prematurely. I removed the goto :EOF and it seems to be working as intended.
Updated Script
for %%z in ("C:\Recordings\AT1*.WAV") do (
for /f tokens^=4^,8^,10^,12^ delims^=^" %%a in ('type "C:\Recordings\index.xml"^|find /i "%%~nxz"') do (
for /f "tokens=1,2 delims=:" %%t in ("%%b") do (
ren "%%z" "%%c-%%t-%%u_%%d%%~xz" 2>nul
if errorlevel 1 set "Number=2" & call :NumberedRename "%%z" "%%c_%%t-%%u_%%d%%~xz"
)
)
)
)
:NumberedRename
if exist "%~n2_%Number%%~x2" set /A "Number+=1" & goto NumberedRename
ren %1 "%~n2_%Number%%~x2"
goto :EOF

Batch - get content between two strings

I have string like this
<AdaptationSet maxHeight="576" maxWidth="1024" mimeType="video/mp4" id="0" segmentAlignment="true" startWithSAP="1">
<ContentProtection schemeIdUri="urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed" cenc:default_KID="bcf950eb-a062-5b0d-b1f2-fed53f20ba10">
<cenc:pssh>AAAATHBzc2gAAAAA7e+LqXnWSs6jyCfc1R0h7QAAACwiJGFlNWU5NTk4LWU5ODktNWM1Ny1iOTk2LTg3OTA4NWIyYzUxNUjj3JWbBg==</cenc:pssh>
</ContentProtection>
<ContentProtection schemeIdUri="urn:mpeg:dash:mp4protection:2011" value="cenc" cenc:default_KID="bcf950eb-a062-5b0d-b1f2-fed53f20ba10" />
<SegmentTemplate duration="6000" initialization="$RepresentationID$/video/init.mp4" media="$RepresentationID$_184201/segment_$Number$.m4s" startNumber="0" timescale="1000" />
<Representation bandwidth="247600" codecs="avc1.64001F" height="144" width="256" id="ae5e9598-e989-5c57-b996-879085b2c515-0_video_frag_auto.idx~video_frag_auto~481" scanType="progressive" />
I want to get output like below and put it into a variable.
ae5e9598-e989-5c57-b996-879085b2c515
Note that there is already id="0" in the first line and I want to extract value of id partially for the last line.
Is there an easy way to do this in batch?
If you must use batch, here's one example to get started. This extracts all values id from file ooo.txt that contains your string.
#echo off
setlocal enabledelayedexpansion
set x=0
for /f "eol=> tokens=* usebackq delims= " %%i in (`type ooo.txt`) do (
set x=0
set XXX=XXX
for %%I in (%%i) do (
if "%%I"=="id" (
set x=1
) else (
if !x! EQU 1 (
set x=0
set XXX=%%I
)
)
)
set XXX=!XXX:"=!
if not "!XXX!"=="XXX" (
set XXX=!XXX:"=!
rem echo !XXX!
call :enumerate !XXX! YYY
echo !YYY!
)
)
goto :eof
:enumerate
set pos=0
set Y=%1
set result=
set part=
:countchar
if not "!Y:~%pos%,1!"=="" (
set /a pos=pos+1
if "!Y:~%pos%,1!"=="-" (
set result=!result!!part!
set part=
)
set part=!part!!Y:~%pos%,1!
goto :countchar
)
set %~2=!result!
exit/b
:eof
endlocal
This outputs
ae5e9598-e989-5c57-b996-879085b2c515
and, within the loop, stores each id in variable YYY (although the first one is empty).
This assumes you want to keep everything up to but not including the last hyphen -.
Here's a 'for fun' example, which, as you've provided no code yourself, will not be modified post answer, and no support, or explanation, will be undertaken either.
#For /F Delims^= %%G In ('%__AppDir__%findstr.exe /i "id=\"[a-e0123456789]*-[a-e0123456789]*-[a-e0123456789]*-[a-e0123456789]*-[a-e0123456789]*-" "xmlfile.mpd"'
) Do #Set "}=%%G" & SetLocal EnableDelayedExpansion & For /F UseBackTokens^=2-6Delims^=-^" %%H In ('!}:* id^=^"^=!') Do #Endlocal & Set "id=%%H-%%I-%%J-%%K-%%L"
#(Set id) 2>NUL & Pause
You will probably want to change the source filename from xmlfile.mpd, and can optionally remove the last line, which was included just to allow you to see if it worked!
#ECHO OFF
SETLOCAL
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q65892027.txt"
SET "capture="
SET "captured="
FOR /f "usebackqdelims=" %%b IN ("%filename1%") DO (
FOR %%c IN (%%b) DO (
IF /i "%%~c"=="scantype" SET "capture="&IF DEFINED captured GOTO done
IF DEFINED capture SET "captured=%%~c"
IF /i "%%~c"=="id" SET "capture=Y"
)
)
:done
ECHO captured "%captured%"
GOTO :EOF
setlocal enabledelayedexpansion
echo:
set "td=%~dp0"
set "pattern= id="
set "id=id"
call:getPatternLength "!id!" plen
set /a "slen=!plen!-1"
set "opt=tokens=1,2,* delims=<"
for /f "tokens=* delims=" %%z in (' type "!td!usedata\id.txt"^| findstr /c:"!pattern!" ') do (
for /f "%opt%" %%b in ("%%z") do (
for /f "tokens=* delims=" %%y in ('echo/%%b^|findstr /v [0-9][a-z]') do (
set "bempty=true"
call:findit "%%c" "!id!" strfound
)
if not defined bempty call:findit "%%b" "!id!" strfound
set "strfound=!strfound:"=!"
if defined strfound (
call:handlefound "!strfound!" result
if defined result (echo Finally: !result! & exit /b 0)
)
)
)
:end
endlocal
exit /b 0
:getPatternLength
set "str=%~1"
set /a "count=0"
:next
for /f "tokens=* delims=" %%z in ('echo/!str!') do (
set /a "count+=1"
set "str=!str:~1!"
if not "!str!"=="" goto:next
)
set "%2=!count!"
if /i "%0"==":getPatternLength" (exit /b 0)
:handlefound
set "str=%~1"
for /f "tokens=* delims=" %%a in ('echo/!str!^| findstr [a-z]') do (
:notyet
for /f "tokens=1,2,* delims=-" %%d in ("!str!") do (
for /f "tokens=* delims=" %%g in ('echo/%%e^|findstr "_"') do (
set "res=!res!-%%d"
set "%2=!res!"
exit /b 0
)
if not defined res ( set "res=%%d" ) else ( set "res=!res!-%%d")
set "str=%%e-%%f" & goto:notyet
)
)
echo Not the right line.. next.
exit /b 1
:findit
set "str=%~1"
set "find=%~2"
set "opts=tokens=1,* delims= "
set "opts2=tokens=1,* delims=="
:nexttoken
for /f "%opts%" %%m in ("!str!") do (
for /f "%opts2%" %%o in ("%%m") do (
if "%%o"=="!find!" (
set "%3=%%p"
if "%0"==":findit" exit /b 0
)
set "str=%%n" & goto:nexttoken
)
)
exit /b 1

Output filename and count of a directory in a text file - windows

I have a batch to output filename and count of filenames with the same prefix and date in a text file.
E.g.
Q1231111.zip
Q1241111.zip
where:
Q123 - is a prefix
1111 - is a date.
I want an output such as:
123 : 1
124 : 1
125 : 0
But the batch file unable to output the last one. I wanna see the file is present so i need the 0 output.
Here's my code:
#echo off
setlocal EnableExtensions
for %%I in ("Z:\StoreDataJDA\Q1231111.zip") do call :CountFile "%%~nI"
for %%I in ("Z:\StoreDataJDA\Q1241111.zip") do call :CountFile "%%~nI"
for %%I in ("Z:\StoreDataJDA\Q1251111.zip") do call :CountFile "%%~nI"
for /F "tokens=2,3 delims=#=" %%I in ('set Group# 2^>nul') do echo %%I: %%J >>count.txt
endlocal
goto :EOF
:CountFile
set "FileName=%~1"
set "FileGroup=%FileName:~1,4%"
if "Group#%FileGroup%" == "" (
set "Group#%FileGroup%=1"
) else (
set /A Group#%FileGroup%+=1
)
goto :EOF
pause
Thanks in advance!
From your description, it's hard to tell exactly what you are looking for. But by looking at your very similar question from a year ago, and from Gerhard's comment, I think what you're looking for is:
Count the number of files with a given prefix, each file having a different date suffix.
I think this should do it:
#echo off
setlocal EnableExtensions
set "Group#123=0" & for %%I in ("Z:\StoreDataJDA\Q123????.zip") do call :CountFile "%%~nI"
set "Group#124=0" & for %%I in ("Z:\StoreDataJDA\Q124????.zip") do call :CountFile "%%~nI"
set "Group#125=0" & for %%I in ("Z:\StoreDataJDA\Q125????.zip") do call :CountFile "%%~nI"
(for /F "tokens=2,3 delims=#=" %%I in ('set Group# 2^>nul') do echo %%I: %%J)>count.txt
endlocal
goto :EOF
:CountFile
set "FileName=%~1"
set "FileGroup=%FileName:~1,3%"
set /A Group#%FileGroup%+=1
goto :EOF

read then write delimited file - Batch cmd

Hi I have a pipe | delimited file.
I need to reverse all the numbers in there
The file looks like this
Entity|Division|Channel|200|300|800
I need to read the file an create a new one with reversed numbers
Entity|Division|Channel|-200|-300|-800
trying to make this work but, not entirely sure how to amend the text I'm getting. I need help on the :processToken procedure. How to output the tokens into a new file and add "-" and add back the delimiter |
for /f "tokens=* delims= " %%f in (M:\GPAR\Dev\EQ\Upload_Files\eq_test.txt) do (
set line=%%f
call :processToken
)
goto :eof
:processToken
for /f "tokens=* delims=|" %%a in ("%line%") do (
)
if not "%line%" == "" goto :processToken
goto :eof
Thanks
#echo off
set "ent_file=c:\entity.txt"
break>"%tmp%\rev.txt"
for /f "usebackq tokens=1,2,3,4,5,6 delims=|" %%a in ("%ent_file%") do (
echo %%a^|%%b^|%%c^|-%%d^|-%%e^|-%%f
)>>"%tmp%\rev.txt"
move /y "%tmp%\rev.txt" "%ent_file%"
del /q /f "%tmp%\rev.txt"
Should work if your file contains only lines like the one you've posted.
EDIT output the part after the 6th token:
#echo off
set "ent_file=c:\entity.txt"
break>"%tmp%\rev.txt"
for /f "usebackq tokens=1,2,3,4,5,6,* delims=|" %%a in ("%ent_file%") do (
echo %%a^|%%b^|%%c^|-%%d^|-%%e^|-%%f|%%g
)>>"%tmp%\rev.txt"
move /y "%tmp%\rev.txt" "%ent_file%"
del /q /f "%tmp%\rev.txt"
#echo off
setlocal EnableDelayedExpansion
set digits=0123456789
(for /F "delims=" %%f in (eq_test.txt) do (
set "input=%%f"
set "output="
call :processToken
set /P "=!output:~1!" < NUL
echo/
)) > new_file.txt
goto :EOF
:processToken
for /F "tokens=1* delims=|" %%a in ("%input%") do (
set "token=%%a"
set "input=%%b"
)
if "!digits:%token:~0,1%=!" neq "%digits%" set "token=-%token%"
set "output=%output%|%token%"
if defined input goto processToken
exit /B

How to create a unique output filename for Windows Script?

I am trying to create a windows script that should generate this kind of filename everytime I run it: filename1, filename2, filename3 and so on. Here is what I have so far:
(
#echo off
wmic logicaldisk get size,freespace,caption
) > disk.txt
I hope you can help me. Thanks!!
:: make a tempfile
:maketemp
SET "tempfile=%temp%\%random%"
IF EXIST "%tempfile%*" (GOTO maketemp) ELSE (ECHO.>"%tempfile%a")
You now have any number of filenames available.
%tempfile%a exists and is empty, but %tempfile%anythingelse should be available for use.
#ECHO OFF
SETLOCAL
SET "basename=filename"
SET /a outname=0
:genloop
SET /a outname+=1
IF EXIST "%basename% %outname%.txt" GOTO genloop
SET "outname=%basename% %outname%.txt"
ECHO %outname%
GOTO :EOF
Ah - increment the destination filename on each run. This should do that. It's not actually creating a file - you'd need to create the file %outname% each time to have it increment...
(the space between %basename% and %outname% is optional, of course - omit it if desired.)
edited to include .txt
This will give you up to 1000 filenames but you can go higher, up to 2 Billion, but the higher you go the longer the delay will be before it picks a filename.
#echo off
for /L %%a in (1,1,1000) do if not defined filename if not exist "filename%%a.txt" set "filename=filename%%a.txt"
(
wmic logicaldisk get size,freespace,caption
) > "%filename%"
#echo off
setlocal enableextensions
call :getNextFilename "filename*.txt" nextFilename
echo %nextFilename%
echo test > "%nextFilename%"
call :getNextFilename "%cd%\filename*.txt" nextFilename
echo %nextFilename%
echo test > "%nextFilename%"
endlocal
exit /b
:getNextFilename whatToSearch returnVariable
setlocal enableextensions enabledelayedexpansion
for /f %%a in ("$\%~1"
) do for /f "tokens=1,* delims=*" %%b in ("%%~nxa"
) do ( set "left=%%b" & set "right=%%c" )
set "max=0"
for %%a in ("%~1"
) do for /f "tokens=1 delims=%left%%right% " %%b in ("%%~nxa"
) do for /f "tokens=* delims=0 " %%c in ("0%%~b"
) do if %%~c geq !max! set /a "max=%%c+1"
endlocal & set "%~2=%~dp1%left%%max%%right%" & exit /b
This should find the next file in sequence independently of the existence of holes in the numeration of the files. A path can be included or omitted. The * will be used as the placeholder for the numeration. BUT this will not work if files or included paths have "problematic" characters.
If the date/time of creation of the file can be considered, then this version can be optimized as
:getNextFilename whatToSearch returnVariable
setlocal enableextensions disabledelayedexpansion
for /f %%a in ("$\%~1"
) do for /f "tokens=1,* delims=*?" %%b in ("%%~nxa"
) do ( set "left=%%b" & set "right=%%c" )
set "max=0"
for /f "delims=" %%a in ('dir /tc /o-d /b "%~1" 2^>nul'
) do for /f "tokens=1 delims=%left%%right% " %%b in ("%%~nxa"
) do for /f "tokens=* delims=0 " %%c in ("0%%~b"
) do set /a "max=%%c+1" & goto done
:done
endlocal & set "%~2=%~dp1%left%%max%%right%" & exit /b
that will take the latest created instance of the file set.
I finally figured out where to put the .txt extension. This is from #Magoo's answer but I wanted the file to be a text file so I placed the .txt twice in order for it to work properly.
#ECHO OFF
SETLOCAL
SET "basename=DISK-OUT"
SET /a outname=0
:genloop
SET /a outname+=1
IF EXIST "%basename% %outname%.txt" GOTO genloop
SET "outname=%basename% %outname%.txt"
(
wmic logicaldisk get size,freespace,caption
) > "%outname%"
GOTO :EOF

Resources