Page 1 of 1

create new file from an extract of another.....

Posted: 11 Mar 2011 06:13
by amichaelglg
Hi, here's another strange & difficult one I need help on please.

Search through all files in specific folder.
create a new file (in the same place) for each of those files using the following rule:
search for first entry in each file that contains the current date, in the form 2011-03-11.
copy from this point (whole line) to the very end of the text file into this new file.
The file name should have this date appended or prefixed in it's new name.
If the entry does not exist in the file then do not create a new file.


Thanks.

Re: create new file from an extract of another.....

Posted: 11 Mar 2011 16:18
by aGerman
Try:

Code: Select all

@echo off &setlocal

for /f "tokens=1,2*" %%a in ('reg query "HKCU\Control Panel\International"') do set "%%a=%%c"
set "date_=%date%"
for /f "tokens=2" %%i in ("%date_%") do set "date_=%%i"
for /f "tokens=1-3 delims=%sDate%" %%a in ("%date_%") do (
  if %iDate%==0 (set "mm=%%a"&set "dd=%%b"&set "yy=%%c")
  if %iDate%==1 (set "dd=%%a"&set "mm=%%b"&set "yy=%%c")
  if %iDate%==2 (set "yy=%%a"&set "mm=%%b"&set "dd=%%c")
)
if 1%yy% lss 200 set "yy=20%yy%"

for /f "tokens=* delims=" %%a in ('dir /a-d /b *.*^|findstr /vb "2[0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]_ %~nx0"') do (
  for /f "delims=:" %%b in ('findstr /nbc:"%yy%-%mm%-%dd%" "%%~a"') do (
    setlocal enabledelayedexpansion
    set /a n=%%b-1
    >"%yy%-%mm%-%dd%_%%a" more +!n!<"%%a"
    endlocal
  )
)


Regards
aGerman

Re: create new file from an extract of another.....

Posted: 14 Mar 2011 02:59
by amichaelglg
thanks for the above.
When I run it displays no output (errors) and creates no files.

I am running on Windows Server 2003 SP2 - does that make a difference on where you are getting the registry info or anything else ?


Additional (summary) note in case of confusion:
1.Search through all files in folder. repeat process below for each file.
2. look for current date, in the form 2011-03-11 in the content of file.
copy the data from this point (the line the date is on) to the end of the file into a new file.
3. The file name should have this date appended or prefixed in it's new name.
4. If the entry does not exist in the file then do not create a new file.

Re: create new file from an extract of another.....

Posted: 14 Mar 2011 04:59
by amichaelglg
Having entered the dos command: reg query "HKCU\Control Panel\International"
If this helps ?):

HKEY_CURRENT_USER\Control Panel\International
iCountry REG_SZ 1
iCurrDigits REG_SZ 2
iCurrency REG_SZ 0
iDate REG_SZ 0
iDigits REG_SZ 2
iLZero REG_SZ 1
iMeasure REG_SZ 1
iNegCurr REG_SZ 0
iTime REG_SZ 0
iTLZero REG_SZ 0
Locale REG_SZ 00000409
s1159 REG_SZ AM
s2359 REG_SZ PM
sCountry REG_SZ United States
sCurrency REG_SZ $
sDate REG_SZ /
sDecimal REG_SZ .
sLanguage REG_SZ ENU
sList REG_SZ ,
sLongDate REG_SZ dddd, MMMM dd, yyyy
sShortDate REG_SZ M/d/yyyy
sThousand REG_SZ ,
sTime REG_SZ :
DefaultBlindDialFlag REG_BINARY 00
sTimeFormat REG_SZ h:mm:ss tt
iTimePrefix REG_SZ 0
sMonDecimalSep REG_SZ .
sMonThousandSep REG_SZ ,
iNegNumber REG_SZ 1
sNativeDigits REG_SZ 0123456789
NumShape REG_SZ 1
iCalendarType REG_SZ 1
iFirstDayOfWeek REG_SZ 6
iFirstWeekOfYear REG_SZ 0
sGrouping REG_SZ 3;0
sMonGrouping REG_SZ 3;0
sPositiveSign REG_SZ
sNegativeSign REG_SZ

Example of contents of one file...
**[2011-03-10 20:27:17] LOGENTRYA**
V9.9.0 23JUL2010 NTI_ORA112/8 PID=4036
X1_ABC: processing 'close background' request
********

**[2011-03-11 20:27:17] LOGENTRYB**
V9.9.0 23JUL2010 NTI_ORA112/8 PID=4036
X1_ABC: processing 'close background' request
********

**[2011-03-14 20:27:17] LOGENTRYC**
V9.9.0 23JUL2010 NTI_ORA112/8 PID=4036
X1_ABC: processing 'close background' request
********

**[2011-03-14 20:27:17] LOGENTRYD**
V9.9.0 23JUL2010 NTI_ORA112/8 PID=4036
HG: abcd
X1_ABC: processing 'end background' request


Bear in mind , it's run today, then I expect a new file to be created with contents:
**[2011-03-14 20:27:17] LOGENTRYC**
V9.9.0 23JUL2010 NTI_ORA112/8 PID=4036
X1_ABC: processing 'close background' request
********

**[2011-03-14 20:27:17] LOGENTRYD**
V9.9.0 23JUL2010 NTI_ORA112/8 PID=4036
HG: abcd
X1_ABC: processing 'end background' request

Re: create new file from an extract of another.....

Posted: 15 Mar 2011 16:52
by aGerman
Geez :? DateTime values and batch -- always a game of pure chance :(

For the command line interpreter a DateTime value is nothing but a string of characters. It depends on your settings whether a date looks like 15.03.2011 (for me) or 3/15/2011 (for you, according your registry values) or maybe Tue 2011-03-15 (for somebody else). I try to find that out by reading the registry:
- iDate defines the order
0 means m d y
1 means d m y
2 means y m d

- sDate defines the separator (slash, dot, dash or what ever)

So far. The code above should work except of three points I was not aware / I didn't know:
1st) No leading zeros. For March I get 03, but I guess you will get only 3. Finally the script is searching for 2011-3-15 instead of 2011-03-15.
2nd) When you wrote "search for first entry" I also had in mind "search on the beginning of the line" for some reason.
3rd) We have to escape the loop.

Code: Select all

@echo off &setlocal

for /f "tokens=1,2*" %%a in ('reg query "HKCU\Control Panel\International"') do set "%%a=%%c"
set "date_=%date%"
for /f "tokens=2" %%i in ("%date_%") do set "date_=%%i"
for /f "tokens=1-3 delims=%sDate%" %%a in ("%date_%") do (
  if %iDate%==0 (set "mm=%%a"&set "dd=%%b"&set "yy=%%c")
  if %iDate%==1 (set "dd=%%a"&set "mm=%%b"&set "yy=%%c")
  if %iDate%==2 (set "yy=%%a"&set "mm=%%b"&set "dd=%%c")
)
if 1%dd% lss 100 set "dd=0%dd%"
if 1%mm% lss 100 set "mm=0%mm%"
if 1%yy% lss 200 set "yy=20%yy%"

for /f "tokens=* delims=" %%a in ('dir /a-d /b *.*^|findstr /vb "2[0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]_ %~nx0"') do (
  set "file=%%~a"
  call :proc
)
goto :eof

:proc
  for /f "delims=:" %%b in ('findstr /nc:"%yy%-%mm%-%dd%" "%file%"') do (
    setlocal enabledelayedexpansion
    set /a n=%%b-1
    >>"%yy%-%mm%-%dd%_%file%" more +!n!<"%file%"
    endlocal
    goto :eof
  )
goto :eof

Regards
aGerman

Re: create new file from an extract of another.....

Posted: 16 Mar 2011 08:42
by amichaelglg
looking good.

thanks. :wink: