In log file many different date format - windows

I write a script to replace all date formats into a similar format in the file. I need to write a window script to find and replace date formates.
It is found only if the date is a separate word but if the date in the long word, can't find.
How I can replace it? in another format

#ECHO ON
for /f "tokens=1,2,3,* delims=/-" %%i in ('type "log.old"') do (
set "year=%%i"
set "month=%%j"
set "day=%%k"
findstr /N /O [1-2][0-9][0-9][0-9]/[0-3][0-9]/ ie8.log
ECHO i = %%i
ECHO j = %%j
ECHO k = %%k
echo %%k/%%j/%%i
)>>"new.log"

Related

Batch file to insert string in exact position in file txt

I have a test.txt file like this:
sylvester, stallone, 35,20, florida;
brad, pitt, 40,25, california;
sean, connery, 15,80, london;
I have to create a new one in which the surname begins with the 15th column and the name in the 30th.
I would like to do it with a batch file.
What I have managed to do is this:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=1,2 delims=," %%G IN (test.txt) DO (
SET "line=%%H"
SET "spaces= "
ECHO (!spaces!!line!!spaces!%%G
)
) >> output.txt
pause
But in this way %%G does not begin always from the same position, it depends on how many characters has %%H. And more, does not write on output.txt but it makes me see the results on the batch window.
I know it's probably a trivial question, but I'm new to programming.
#echo off
setlocal enabledelayedexpansion
del output.txt
set "spaces= " :: 15 spaces
(FOR /F "tokens=1,2 delims=, " %%G IN (test.txt) DO (
SET "line=%spaces%%%H %spaces%"
set "line=!line:~0,29!"
echo !line:~0,29!%%G
)) >output.txt
add 14 spaces in front of the surname (to let surename start at 15) and append another 15 spaces (to get a cumulated length of at least 29). Then trim it to the first 29 characters and append the name (at Pos 30).
(added a space to the delims for proper handling)

Result of each repeat of the loop to different file WIN BATCH

I would like to resolve simple problem - Saving to different file when loop repeats. I know im getting results because when I am doing >>file.txt
i am getting all restults into one file. It would be GREAT to save results to different files each time(and name this file by text from variable. but there is something wrong. It saves the results of last loop iteration.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /F "tokens=1* delims=;" %%A in (list2.csv) do (
SET /A vidx=!vidx! + 1
set var!vidx!=%%A
rxrepl -f temp.txt -s "xNAMEx" -r "%%A">file___%var!vidx!%.txt
)
Try replacing this
file___%var!vidx!%.txt by this file___!var!!vidx!!!.txt
You don't need any variables to get the result you are looking for. Also, the * is usesless in "tokens=1*" if you never reference variable %%B. And "tokens=1" is the default. So all you need is:
for /F "delims=;" %%A in (list2.csv) do rxrepl -f temp.txt -s "xNAMEx" -r "%%A">"file___%%A.txt"
If you really want to build an "array" of var.N values, you can use FINDSTR to prefix each line with an incrementing number (line number).
for /F "tokens=1,2 delims=:;" %%A in ('findstr /n "^" list2.csv') do (
set "var.%%A=%%B"
set "var.cnt=%%A"
rxrepl -f temp.txt -s "xNAMEx" -r "%%B">"file___%%B.txt"
)
:: Display the "array" values
for /l %%N in (1 1 %var.cnt%) do echo var.%%N=!var.%%N!
Use just ...>"file___%%~A.txt" instead of erroneous >file___%var!vidx!%.txt
#ECHO OFF
SETLOCAL enableextensions enabledelayedexpansion
set vidx=0
for /F "tokens=1* delims=;" %%A in (list2.csv) do (
SET /A "vidx+=1"
set "var!vidx!=%%~A"
echo loopvar %%%% A=%%~A "file___%%~A.txt"
rem next line shows how to treat array-like names
call set "filenamepart=%%var!vidx!%%"
echo filenamepart=!filenamepart! "file___!filenamepart!.txt"
rem rxrepl -f temp.txt -s "xNAMEx" -r "%%A">"file___%%~A.txt"
)

How to print a specific line (variable included) from a file (text) using cmd

I am really confused with something. I am making a batch file that can can display a specific line that I want. For example there is file hi.txt
Content of file -
ro.build.id=KOT49H
ro.build.display.id=XXX_ROM_v1
ro.build.version.incremental=eng.mudingyu.1408084583
ro.custom.build.version=XOLO_8x-1000_0815.v009
ro.internal.version=J608_XOLO_N1A_V0.8.7_S08015
ro.internal.version.rgk=J608_XOLO_N1A_V0.8.7_S08015
ro.build.version.sdk=19
ro.build.version.codename=REL
ro.build.version.release=4.4.4
ro.build.date=Fri Aug 15 14:39:45 CST 2014
ro.build.date.utc=1408084785
As here you can see the file content. Now say I want to print ONLY this piece of information on my screen when batch is executed
"4.4.4" from this line "ro.build.version.release=4.4.4"
I managed to pull of code for the 2nd line
ro.build.display.id=XXX_ROM_v1
When I execute batch I see this
XXX_ROM_V1
Code I used
set "build.prop="
for /F "skip=3 delims=" %%i in (WORKING_FOLDER\system\build.prop) do if not
defined build.prop set "build.prop=%%i"
echo.
echo "%build.prop%"
echo.
echo Enter the name you want to choose
set /p name=
echo Backing up old build.prop...
md C:\Kitchen\temp
copy C:\Kitchen\WORKING_FOLDER\system\build.prop C:\Kitchen\temp
cd C:\Kitchen\temp
cd C:\Kitchen\WORKING_FOLDER\system
build.prop.new (
for /f "tokens=1* delims==" %%A in (build.prop) do (
if "%%A" == "ro.build.display.id" (
echo ro.build.display.id=%name%
) else (
echo %%A=%%B
)
)
)
move /y build.prop.new build.prop >nul
build.prop.new (
for /f "tokens=1* delims==" %%A in (build.prop) do (
if "%%A" == "ro.build.id" (
echo ro.build.id=%name%
) else (
echo %%A=%%B
)
)
)
move /y C:\Kitchen\temp\build.prop.new
C:\Kitchen\WORKING_FOLDER\system\build.prop >nul
echo Changed value of ro.build.display.id to " %name% "
echo.
set "build.prop="
for /F "skip=3 delims=" %%i in (WORKING_FOLDER\system\build.prop) do if not
defined build.prop set "build.prop=%%i"
echo In build.prop now the current name of ROM is
echo.
echo Current ROM Name = %name%
Here as you see in first line I print the name using the line command function and also took input from user to change the name so it became easy to print the file name but in some other files the line "ro.build.display.id" is not always on 4th.
So my question is what logic or whatever can be done in order to print the particular term on screen that I wish to print without prompting user to enter new file name just print that specific piece of information
eg The OUTPUT I want
The Android version is "4.4.4"
EG Say I want to print "4.4.4" only from the file above
how can I do it?
Using findstr or anything please help really appreciated.
whew - that was a lot to read for a quite simple question.
for /f "tokens=2 delims==" %%i in ('find "ro.build.version.release" hi.txt') do set release=%%i
echo The Android version is "%release%"
Find the correct line and tokenize it with = as delimiter.

How to read and find the largest value from .csv with the use of window batch

In my .csv file,
abc,10/24/2013,ABC
cede,5/1/2013,ABCk
cdeh,7/27/2014,ABCf
cdedsf,1/27/2014,gfABC
.
.
.(1xx more lines with similar text)
I would like to find the latest date in the middle field (e.g. 7/27/2014 in above case) and save to a variable named as "latest_date".
However, I do not know how to read the specific field from a .csv file and find the latest date with such format(M/D/Y).
Can anyone teach me?
This simpler method should run faster:
#echo off
setlocal EnableDelayedExpansion
set latest_num=0
for /F "tokens=2-4 delims=,/" %%a in (theFile.csv) do (
set /A "new_num=((%%c*100)+%%a)*100+%%b"
if !new_num! gtr !latest_num! (
set latest_num=!new_num!
set latest_date=%%a/%%b/%%c
)
)
echo Latest date: %latest_date%
#echo off
setlocal enableextensions disabledelayedexpansion
for /f "tokens=1-3 delims=/ " %%a in (
' cmd /q /v:on /c "for /f "tokens^=2-4 delims^=^,/" %%a in (data.csv) do (set /a "x^=%%c*10000+%%a*100+%%b" >nul & echo(^!x:~0,4^!/^!x:~4,2^!/^!x:~-2^!)" ^| sort /r '
) do set "last_date=%%b/%%c/%%a" & goto done
:done
echo %last_date%
How does it work?
The input file is readed via a for /f loop (the inner one). Each record is tokenized, using the commas and slashes as delimiters. This leaves the tokens 2 to 4 as the elements of the date. This elements are normalized (month and days have one or two digits, years are at the end) with some arithmetics to get a yyyy/mm/dd date, and the resulting dates are echoed. This list of dates is sorted in inversed order (so the greatest date is in the first record).
As the process in defined as a pipe (each process inside a pipe run in a separate cmd instance), and as the left part of the pipe requires delayed expansion enabled, the for /f that reads the file is executed inside its own instance of cmd with the adecuated configuration: echo off (/q) and delayed expansion active (/v:on).
The sorted list, will be readed with another for /f loop (the outer one), that will tokenize the retrieved data, separating again the year, month and day, so the final variable have the required format (mm/dd/yyyy). As the greatest date is in the first record, once it is retrieved and the value assigned to the variable, a goto jump to a label is executed to skip the rest of the records.
To see it clear, this is the same code, more readable, but separated in steps and using a temporary file
#echo off
setlocal enableextensions enabledelayedexpansion
set "tempFile=%temp%\%~nx0.%random%.tmp"
( for /f "tokens=2-4 delims=,/" %%a in (data.csv) do (
set /a "x=%%c*10000+%%a*100+%%b"
echo(!x:~0,4!/!x:~4,2!/!x:~-2!
)
)> "%tempFile%"
for /f "tokens=1-3 delims=/ " %%a in (
' type "%tempFile%" ^| sort /r '
) do set "last_date=%%b/%%c/%%a" & goto done
:done
echo %last_date%
del /f /q "%tempFile%" >nul 2>nul
endlocal
The inner loop in original code is now the first loop. File is readed by the for, date elements extracted (see tokens and delims), date normalized (set /a arithmetics) and the list saved to a temporary file
The outter loop in original code is the second one here. The file is readed with a type command, the data piped to sort /r and the resulting lines are tokenized by the for command to reformat the date.
Edited to adapt to comments
Aacini is right, his code is faster, but given that i just started this way, .... Anyway, the changes in this code can be translated to his solution
This should handle differences in date fields (aditional spaces and aditional initial 0) and missing fields.
#echo off
setlocal enableextensions disabledelayedexpansion
for /f "tokens=2 delims=:" %%a in (
'cmd /q /v:on /c "for /f "delims^=" %%z in (data.csv) do for /f "tokens^=1-3 delims^=^," %%w in (" %%~z ") do if not "%%~y" equ "" for /f "tokens^=1-3 delims^=/ " %%a in ("%%~x") do (set /a "x^=%%c*10000 + 100%%a %% 100*100 + 100%%b %% 100" >nul & echo(^!x^!:%%~x)" ^| sort /r '
) do set "latest_date=%%a" & goto done
:done
echo %latest_date%
The inner for loops :
for %%z will read lines from file
for %%w will tokenize the readed line with an aditional space at the start and end of the line to prevent problems with adjacent delimiters removal.
for %%a handles the date normalization and outputs the calculated value used for sort and the readed date.
The outer for %%a loop will split the retrieved record to separate the calculated value from the readed date.
In the CMD the variable are displayes in alphabetical order and from the smaller to the bigger value.
So we transform all your dates in variables like this : set #142707=7/27/2014
Then looping against a set # we take the last value who is the latest date.
#echo off
setlocal enabledelayedexpansion
for /f "tokens=2 delims=," %%a in (data.csv) do call:checkDate %%a
for /f "tokens=2 delims==" %%a in ('set #') do set $LatestDate=%%a
echo The Latest Date : [!$LatestDate!]
exit/b
:CheckDate
set $out=
set "$Date=%1"
set "$Date=%$Date:/= %
for %%b in (%$Date%) do (
set $val=0%%b
set $out=!$val:~-2!!$out!)
set #!$out!=%1

Perform an action based on a specific text? A windows script

Consider that I have a textfile containing the following information:
TIMESTARTED=Thu Nov 17 09:43:47 GMT 2011
START-OF-DATA
TEXT TEXT TEXT
The TIMESTARTED line is on a specific line in the textfile and I need to create some sort of verification that the date here is correct before a specific task is being performed. I basically want it to look at the date on the computer and see if that corresponds to the date after TIMESTARTED=, if it does it should run a batscript and if it does not it should run a different bat script.
I do not know if this is possible or if it would require extensive programming? I would prefer if it's possible to do this just by scripting in Windows. Could anyone lead me to the right path? (Bear in mind that I am not a programmer, so as simple as possible would be preferred)
This is jeb's solution slightly modified to use an array instead of a map string, just for comparison purposes (and to promote the use of arrays in Batch):
setlocal EnableDelayedExpansion
for /F "tokens=2,3 delims= " %%A in ('findstr /B "TIMESTARTED" html_2.txt') do (
set "monthName=%%A"
set "day=%%B"
)
set "year=%date:~-4%"
for %%a in ("Jan=01" "Feb=02" "Mar=03" "Apr=04" "May=05" "Jun=06" "Jul=07" "Aug=08" "Sep=09" "Oct=10" "Nov=11" "Dec=12") do set %%~a
set "month=!%monthName%!"
set "timestarted=%day%.%month%.%year%"
if "%timestarted%" == "%DATE%" (
echo The date is correct
) ELSE (
echo wrong date %timestarted%
)
You could first pick the line with findstr and then compare the date with the current date from %date%.
setlocal EnableDelayedExpansion
for /F "tokens=2,3 delims= " %%A in ('findstr /B "TIMESTARTED" html_2.txt') do (
set "monthName=%%A"
set "day=%%B"
)
set "year=%date:~-4%"
SET map=Jan-01;Feb-02;Mar-03;Apr-04;May-05;Jun-06;Jul-07;Aug-08;Sep-09;Oct-10;Nov-11;Dec-12
SET "v=!%map:*%monthName%-=!"
set "month=!v:~0,2!"
set "timestarted=%day%.%month%.%year%"
if "%timestarted%" == "%DATE%" (
echo The date is correct
) ELSE (
echo wrong date %timestarted%
)

Resources