Find a string and create new files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lakaraj
Posts: 7
Joined: 01 Mar 2012 09:55

Find a string and create new files

#1 Post by lakaraj » 01 Mar 2012 10:08

Hi,

I am new to DOS scripting and have a new requirement where I have a incoming.txt file which have multiple company names and i need to find a strings(company name) and create a new file as companyname.txt

C:\temp\incoming.txt has the below sample data in it

02-12-2012 00:00:0002-12-2012 00:00:00Hunter abc
02-12-2012 00:00:0002-12-2012 00:00:00Justin def
02-12-2012 00:00:0002-12-2012 00:00:00Tom ghi
02-12-2012 00:00:0002-12-2012 00:00:00Kristy jkl

Lets say abc, def, ghi and jkl and the company names; i need a batch script which searches the incoming.txt file for abc and create a file name abc.txt and then searches for def and create a def.txt file, like wise.

Can anyone help, please.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find a string and create new files

#2 Post by foxidrive » 01 Mar 2012 10:22

I don't think you have supplied enough information.

Where is the list of the company names?

What data is going inside the new files?

lakaraj
Posts: 7
Joined: 01 Mar 2012 09:55

Re: Find a string and create new files

#3 Post by lakaraj » 01 Mar 2012 10:42

we have company names as CBJ, LBU, SOH.

The script should search the incoming.txt file for the company name CBJ and create a new file called CBJ.txt with the output of findstr "CBJ" in it and then move it from C:\temp\incoming.txt to D:\temp\CBJ.txt.

And then search for company string LBU in the incoming.txt file and create a new file LBU.txt with theoutput of findstr "LBU" in itand then move it from C:\temp\incoming.txt to D:\temp\LBU.txt.

Instead of writing individual scripts for each search string, i wanted to have a single script.

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Find a string and create new files

#4 Post by alan_b » 01 Mar 2012 11:18

lakaraj wrote:we have company names as CBJ, LBU, SOH.

The script should search the incoming.txt file for the company name CBJ and create a new file called CBJ.txt with the output of findstr "CBJ" in it and then move it from C:\temp\incoming.txt to D:\temp\CBJ.txt.

And then search for company string LBU in the incoming.txt file and create a new file LBU.txt with theoutput of findstr "LBU" in itand then move it from C:\temp\incoming.txt to D:\temp\LBU.txt.

Instead of writing individual scripts for each search string, i wanted to have a single script.


Your first post said nothing about prior knowledge that "abc, def, ghi and jkl" are company names,
and implied that after a fixed format "02-12-2012 00:00:0002-12-2012 00:00:00" there would be two separate names,
the last of which is where a company name is expected.

Now it seems you have prior knowledge of CBJ, LBU, SOH.

Which of the two is your requirement ?

If an unknown second name XYZ should occur,
do you still require creation of "D:\temp\XYZ.txt" ? AND / OR
Should there be an error report issued ?

lakaraj
Posts: 7
Joined: 01 Mar 2012 09:55

Re: Find a string and create new files

#5 Post by lakaraj » 01 Mar 2012 11:37

Hi Alan,

This the format of the file we receive is

Date Time stamp Date Time stamp Employee Name COMPANY
02-12-2012 00:00:00 02-12-2012 00:00:00 Kathy CBJ
02-12-2012 00:00:00 02-12-2012 00:00:00 Nick LBU
02-12-2012 00:00:00 02-12-2012 00:00:00 Hunter SOH

So after the fixed time-stamp format we have the employee name and then the company name. If an unknow company pops up in the text file, then i still need to create a new file with the company name.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Find a string and create new files

#6 Post by Squashman » 01 Mar 2012 11:49

So there is always six fields separated by Spaces?
Do the Employee names or Company Names ever have spaces in them?

lakaraj
Posts: 7
Joined: 01 Mar 2012 09:55

Re: Find a string and create new files

#7 Post by lakaraj » 01 Mar 2012 11:58

Yes, the fields are separated by spaces. Employee names have space in them, but company doesn't have any space.

lakaraj
Posts: 7
Joined: 01 Mar 2012 09:55

Re: Find a string and create new files

#8 Post by lakaraj » 01 Mar 2012 12:01

Sometimes we do get more than 6 fields but any additional field would be after the company name, like employee ID, job title which are separated by spaces.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Find a string and create new files

#9 Post by Squashman » 01 Mar 2012 12:53

Kind of confused by that.
If you have a file with the field names defined in the first line that are separated by spaces why would you let people put spaces into the data for a particular field. You should have the fields separated by commas.

lakaraj
Posts: 7
Joined: 01 Mar 2012 09:55

Re: Find a string and create new files

#10 Post by lakaraj » 01 Mar 2012 13:18

If some employees have a middle initials then they are separated by a space.

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Find a string and create new files

#11 Post by alan_b » 01 Mar 2012 13:25

The requirement seems impossible
How about
02-12-2012 00:00:00 02-12-2012 00:00:00 Kathy CBJ LBU SOH

The Possible interpretations include :-
Name = "Kathy CBJ LBU" ; Company = "SOH"
Name = "Kathy CBJ" ; Company = "LBU" ; ID = "SOH"
Name = "Kathy" ; Company = "CBJ" ; ID = "LBU" ; Title = "SOH"

Would you really want that one line of text to create 3 new files named SOH,TXT, LBU.TXT, CBJ.TXT

Incidentally your specification requires that the new file will replace / delete the previous file with the same name.
Is that the actual requirement ?
Might it not be appropriate to instead APPEND new information to the relevant file should one already exist ?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find a string and create new files

#12 Post by foxidrive » 01 Mar 2012 23:06

lakaraj, do you have a separate list of the company names? Not to post here necessarily, but your task is not possible unless you have a list of the companies.

The problem is that your fields are all space delimited but the names can have spaces in them too, and the number of fields is not fixed so we can't simply use the last field as the company name.

lakaraj
Posts: 7
Joined: 01 Mar 2012 09:55

Re: Find a string and create new files

#13 Post by lakaraj » 05 Mar 2012 08:38

If we move the COMPANY field as the first column of the text file we receive, then would it be easier to write the script for each company.

COMPANY Date Time stamp Date Time stamp Employee Name
CBJ 02-12-2012 00:00:00 02-12-2012 00:00:00 Kathy
LBU 02-12-2012 00:00:00 02-12-2012 00:00:00 Nick
SOH 02-12-2012 00:00:00 02-12-2012 00:00:00 Hunter

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find a string and create new files

#14 Post by foxidrive » 05 Mar 2012 09:32

Easier, but not error proof. We have no way of knowing if a company name is like this:

Commonwealth Bank of Australia 02
Woolworths Ltd
Coles
Enron Pty Ltd

If you can change the file format easily then also make it comma delimited, and the script will be easy having the company name as the first field.

Post Reply