1.

Solve : extract line data from files and insert original file creation date?

Answer»

I have a collection of files that contain order LINE DATA.

I need to extract a particular field (that resides in position 13, length 10) from the first line of each file and put it into a new file.

I'm kind of a noob, so I spent a couple of hours searching this forums to finally manage to "suck" each first line from the order line files and put them into a new file with this code:

Code: [Select]setlocal enabledelayedexpansion
if exist orders_merged.txt del orders_merged.txt
set count=0
for %%v in (order*.txt) do (
set /P firstline=<%%v
set ordernumber=!firstline:~12,10!
echo !ordernumber! >> orders_merged.txt
set /a count+=1
)
I also want the new file to begin each line with the file creation date of the order file that contained the original line data.


My date format is 2009-09-14 (yyyy-mm-dd).

show your input file, describe how the output LOOKS like using that input file Quote from: gh0std0g74 on September 14, 2009, 02:36:32 AM

show your input file, describe how the output looks like using that input file

Input files:

Order_1.txt (file creation date 2009-09-04)
90000 40216608110520900033012641
90000 40216608110525900033090641
90000 40216608110555100140001000

Order_2.txt (file creation date 2009-09-07)
90000 40169008310351900315090000
90000 40169008310342800026001002

Order_3.txt (file creation date 2009-09-14)
90000 40365401110325000219001651


...and so on. There are thousands of more files..

This is the output I want in the file orders_merged.txt
200909044021660811
200909074016900831
200909144036540111you can run this vbscript
Code: [Select]Set objFS=CreateObject("Scripting.FileSystemObject")
strFolder = "c:\test"
Set objFolder = objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files
strFileName = strFile.Name
If InStr(strFileName,"Order_") > 0 Then
strDateCreated = strFile.DateCreated
y = DatePart("yyyy",strDateCreated)
mth = DatePart("m",strDateCreated)
dy = DatePart("d",strDateCreated)
If Len(mth) <2 Then
mth="0"&mth
End If
If Len(dy) <2 Then
dy="0"&dy
End If
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfLine
strLine=objFile.ReadLine
s = Mid(strLine,13,10)
WScript.Echo y&mth&dy&s
Loop
objFile.Close
End If
Next


output
Code: [Select]c:\test> cscript /nologo myscript.vbs > orders_merged.txt
Quote
output
Code: [Select]c:\test> cscript /nologo myscript.vbs > orders_merged.txt

I fixed up the paths, but I end up with an empty orders_merged.txt file. Am I doing something wrong?Quote from: swede on September 14, 2009, 04:49:55 AM

Am I doing something wrong?
most obviously. what did you change when using my code? make sure you have the files in the path c:\test. As you can see, my code checks for files with "Order_" in their filename. Remove the ">" output redirection and just run the script. show the output here if any.
Thanks for your quick response.

Quote from: gh0std0g74 on September 14, 2009, 04:54:59 AM
most obviously. what did you change when using my code?
I found the cause - the below string is case sensitive so I changed the code according to the file names. Code: [Select]If InStr(strFileName,"order_") > 0 Then

Your code and my sample order files from above result in the output file orders_merged.txt:

200909144021660811
200909144021660811
200909144021660811
200909144016900831
200909144016900831
200909144036540111

This isn't correct.
As I wrote in previous post, I want the output to be the first line of each file together with the file creation date.

The output I'm looking for is this:

orders_merged.txt
200909144021660811
200909144016900831
200909144036540111Quote from: swede on September 14, 2009, 05:37:37 AM
I want the output to be the first line of each file together with the file creation date.
I want you to try yourself. since you only want the first line, then remove the Do Until loop, but leave the inside of the loop intact.... Quote from: gh0std0g74 on September 14, 2009, 05:43:48 AM
I want you to try yourself.
That's how I like it.

Quote
since you only want the first line, then remove the Do Until loop, but leave the inside of the loop intact....

Works like a charm!

Unfortunately I haven't done VBS since 1995, so this will take a while to grasp.

Can I run this from a batch file? I usually schedule batch files with the built-in event scheduler in XP. Or is it possible to schedule the script itself?

Thanks again for your help!Quote from: swede on September 14, 2009, 08:02:30 AM
Can I run this from a batch file?
if you can run something on the command line, you can most certainly put them into a file and name it .bat.

Quote
I usually schedule batch files with the built-in event scheduler in XP. Or is it possible to schedule the script itself?
yes of course you can schedule the script itself. it is just executing another PROGRAM (which in this case , its cscript.exe ). When you set your scheduler, just put in the EXACT command you executed on the command line into the scheduler....


Discussion

No Comment Found