Find a string and create new files
Moderator: DosItHelp
Find a string and create new files
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.
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.
Re: Find a string and create new files
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?
Where is the list of the company names?
What data is going inside the new files?
Re: Find a string and create new files
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.
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.
Re: Find a string and create new files
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 ?
Re: Find a string and create new files
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.
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.
Re: Find a string and create new files
So there is always six fields separated by Spaces?
Do the Employee names or Company Names ever have spaces in them?
Do the Employee names or Company Names ever have spaces in them?
Re: Find a string and create new files
Yes, the fields are separated by spaces. Employee names have space in them, but company doesn't have any space.
Re: Find a string and create new files
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.
Re: Find a string and create new files
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.
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.
Re: Find a string and create new files
If some employees have a middle initials then they are separated by a space.
Re: Find a string and create new files
The requirement seems impossible
How about
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 ?
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 ?
Re: Find a string and create new files
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.
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.
Re: Find a string and create new files
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
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
Re: Find a string and create new files
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.
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.