#186719 - 2008-04-04 05:03 PM
Re: Open all files in dir, find phrase, insert text, write file
[Re: Glenn Barnas]
|
Curious
Getting the hang of it
Registered: 2004-02-16
Posts: 61
|
I was troubling a little bit with that run-part of the script, and working with another way to fix the zip-extraction, when I looked in here and saw your post Glenn. I was working on something in that direction, byt thanks (once again..) for your time and help.
Now my script is doing almost everything I want it to, I am sure the code can be written much better, but anyhow, it does what I need it to. I will look a bit on trying to make udf of some parts of the script, see what I can make out of it.
Here's the script for now ( Right now it runs and has finished unpacking 200 zip-files, giving a bit under thousand files (totally abouut 10 gigs), now being rewritten and inserted the correct line. Works like a charm.. Not far from it anyway  Some text is written in norwegian.
; Routine to search given directory for textfiles
; Uses DirList()Udf to search through Directory.
; Scans files in spesific directory and search for specific text
; When found - Dependent on file-naming, Insert line with different text ($InsertData) from var $Kommunenr
; every time text is found (numerous times in one single file).
; Version 1.0 - 20080402
; Necessary File-structure:
; d:\Sosi - Root-Dir
; d:\Sosi\Zip - Zip-files is placed here
; Rest of needed dirs is created in the script
;Dependecies: Dirlist()udf - Embedded in script
;7z.exe must reside in zip-dir
;Defined Vars
$ZipDir = 'd:\sosi\zip\'
$7z = $ZipDir + '7z.exe'
$zipOutDir = $ZipDir +'Utpakket\'
$ZipSyntax = $7z + ' e ' + $ZipDir +'*.zip *.sos -y -o'+$ZipOutDir
$SearchData = 'objtype' ; insert when line containing this is found
$RootPath = 'd:\sosi\' ; root path where files live
$Original = 'Zip\Utpakket\'
$Files = DirList($RootPath+$Original, 2) ; return a list of all files with complete paths
$LogDir = 'Log\' ; Log-dir
$LogFile = $RootPath+$logdir+'logfile.txt' ; Logfile-placement
$counter = 0 ; Counter for printing # of files on screen
$WorkDir = 'WorkDir\'
$WorkFile = $RootPath+$Workdir+'WorkFile.tmp'
$OutPutDir = $RootPath + 'OutDir\'
;Create Necessary directories.. if already existing, empty thees ones.
If NOT Exist ($ZipOutDir) MD ($ZipOutDir)
Else Del $ZipDir+$ZipOutDir
EndIf
If Exist ($logfile) Del ($logfile) ; Check if log-file exists, if it does, delete it
Else MD ($RootPath+$logdir) ; If log-dir doesn't exist, create it
EndIf
If NOT Exist ($RootPath+WorkDir) MD ($RootPath+WorkDir)
EndIf
If NOT Exist ($OutPutDir) MD ($OutPutDir)
Else Del $OutPutDir
EndIf
;Running zip-routine:
;7zip kommando for å pakke gitte filtyper til ett fast område:
; '7z e zipfil.zip *.sos -od:\sosi\zip\utpakket' (eller bare -outpakket om du står i Dir over)
$Cmd = '%COMSPEC% /c'
$Cmd = $Cmd + $ZipSyntax
Shell $Cmd
?
? "Zip-filer pakket ut og skrevet til fil.." ; Confirmation after zip-ectraction
Sleep 10
;Checking each file for given text, starts here..
CLS
For Each $File In $Files
$counter = $counter+1 ; Counter to use on screen and in logfile.
$FileProcessed = SubStr ("$file",22,30) ;Extract filename from $file
? "Skriver Fil# " $counter " " $fileprocessed " til fil.." ; Display on screen file-progress.
Open(1, $logfile, 5 ) ;Write all on screen to log-file
WriteLine(1 , "Skriver Fil# "+ $counter + " "+ $fileprocessed + " til fil.." + @CRLF)
;Sjekke if file is N50-data
$kommunenr = SubStr ("$file", 22,4) ; File is probably n50
; Check if file is Fkb-data
If SubStr("$file",22,2)='32' $kommunenr = SubStr("$file",25,4)
EndIf
;Check if file is N250-data
If SubStr("$file",22,3)='08_' OR SubStr("$file",22,3)='06_'
If SubStr("$file",22,3)='08_' $kommunenr = SubStr("$file",22,2)+'00'
EndIf
If SubStr("$file",22,3)='06_' $kommunenr = SubStr("$file",22,2)+'00'
EndIf
EndIf
$InsertData = '..KOMM '+ $kommunenr ;$kommunenr will decide which text to put in written out-file
Copy $File $WorkFile ; rename the file
If Open(2, $WorkFile)=0
$ = RedirectOutput($OutPutDir+$FileProcessed,1) ; output to output dir with original file-name
$Line = ReadLine(2)
While NOT @ERROR
If InStr($Line, $SearchData)
$InsertData ? ; output extra (inserted) line
EndIf
$Line ? ; output original line
$Line = ReadLine(2)
Loop
$ = Close(2)
$ = RedirectOutput('') ; close output file
Del $WorkFile
EndIf
Next
?
?
? "Skrevet " $counter " filer til lagringsmappe " $OutPutDir ; Write confirmation of written files to output-dir
? "Loggfil 'Logfile.txt' ligger i mappen " $LogFile ; Display the placement of the log-file
WriteLine(1 ,@crlf + $crlf + "Skrevet "+ $counter +" filer til lagringsmappe "+ $OutPutDir + @CRLF) ; Same info written in the log-file
Close (1)
Sleep 15
_________________________
I was placed on earth to do a job. Right now I have so much to do, I will never die..
|
|
Top
|
|
|
|
Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-03-25 03:48 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Mart
|
2008-03-25 04:50 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
NTDOC
|
2008-03-25 06:29 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Mart
|
2008-03-25 08:24 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-03-26 11:06 AM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Mart
|
2008-03-26 01:35 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Glenn Barnas
|
2008-03-26 03:36 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-04-01 04:05 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-04-01 08:57 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-04-01 11:27 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Glenn Barnas
|
2008-04-02 12:32 AM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-04-03 01:20 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Glenn Barnas
|
2008-04-03 03:59 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-04-04 05:03 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Glenn Barnas
|
2008-04-04 08:17 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-04-05 12:11 AM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Glenn Barnas
|
2008-04-05 01:45 AM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-04-05 04:21 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Les
|
2008-04-03 05:06 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-04-01 09:32 AM
|
Re: Open all files in dir, find phrase, insert text, write file
|
NTDOC
|
2008-03-26 08:10 PM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Curious
|
2008-04-01 09:57 AM
|
Re: Open all files in dir, find phrase, insert text, write file
|
Glenn Barnas
|
2008-04-01 01:05 PM
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 846 anonymous users online.
|
|
|