Link to home
Start Free TrialLog in
Avatar of E=mc2
E=mc2Flag for Canada

asked on

Script searches for a code then looks in a position and writes a new number in the content

I am working with a .csv file and I need to change some of the contents.  - let's name it newfile.csv
Below is an example of the content.  These are only 2 lines, however there could be multiple lines in one .csv file.
In this sample, each line starts with 9,J.. and in this case ends with 002.. however each line can be different..

I would like a simple .bat script which will look at position 4 first - if it sees 65-ABCDEF then it looks at position 101.
If position 101 is blank, then it looks in position 100, and looks at tast 6 digits of position 100,  takes those six numbers - removes the last didit, and
write the 5 digits in position 101 - without touching the rest of the data and saving it to a new file called newfile1.csv

So in the examples below, since in position 4 it sees 65-ABCDEF, it then looks in position 101 and sees that it's blank.. therefore it looks at position 100.
There in the first line it sees the number 12345678123452 - therefore it will look at the last 6 digits.. being 123452, remove the last digit, and then it will write those digits
in position 101.. so side by side positions 100 and 101 would look like this before it moves on to the next line..   12345678123452,12345,

Thanks in advance for any assistance you can provide.


9,J,01234,65-ABCDEF,N,1234/2,12345612314,,20190220,20190319,20190319,,1234567890000,Company Inc.,987 Street,,Somewhere,AB,ABC DEF,AM,,,1234567891234,ABC 1234,1 Boulevard,,Here,AB,ABC FGH,AM,,,9876543219876,CORP,0001-,AMA,Y,,,,,,,,,,N,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,20190313,,,,,,,12345678123452,,123456,Desc,,,,10.00,AM,0.00,BC,0.00,AK,10.0100,0.0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,001
9,J,01234,65-ABCDEF,N,1234/2,12345612314,,20190220,20190319,20190319,,1234567890000,Company Inc.,987 Street,,Somewhere,AB,ABC DEF,AM,,,1234567891234,ABC 1234,1 Inkster Boulevard,,Here,AB,ABC FGH,AM,,,9876543219876,ORP,0001-,AMA,Y,,,,,,,,,,N,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,20190313,,,,,,,1234567867892,,789123,Desc,,,,11.00,AM,0.00,BC,0.00,AK,11.0200,0.0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,002
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Check this script  VBScript (untested...maybe needs some tweaking)
dim inputFilePath,outputFilePath
dim outputFile,fso,inputFile
Set fso = CreateObject("Scripting.FileSystemObject") 
inputFilePath = GetFileName("C:\Temp\", "Csv files|*.csv")
set inputFile = fso.OpenTextFile(inputFilePath)
outputFilePath = mid(inputFilePath,1,InStr(strReverse(inputFilePath),"\")-2)
outputFilePath=outputFilePath & "output.csv"

Set outputFile = fso.CreateTextFile(outputFilePath,  True)
Do While inputFile.AtEndOfStream <> True
    arr = Split(inputFile.ReadLine, ",")
	
    if arr(3) = "65-ABCDEF" Then
	if len(arr(100))=0 then 
	
	arr(100) =left(right(arr(99),6),5)
	
	end if
	end if
    outputString = Join(arr, ",")
    outputFile.WriteLine outputString
Loop

Function GetFileName( myDir, myFilter )
  ' Written by Rob van der Woude
  ' http://www.robvanderwoude.com

  ' Standard housekeeping
  Dim objDialog

  ' Create a dialog object
  Set objDialog = CreateObject( "UserAccounts.CommonDialog" )

  ' Check arguments and use defaults when necessary
  If myDir = "" Then
    ' Default initial folder is "My Documents"
    objDialog.InitialDir = CreateObject( "WScript.Shell" ).SpecialFolders( "MyDocuments" )
  Else
    ' Use the specified initial folder
    objDialog.InitialDir = myDir
  End If
  If myFilter = "" Then
    ' Default file filter is "All files"
    objDialog.Filter = "All files|*.*"
  Else
    ' Use the specified file filter
    objDialog.Filter = myFilter
  End If

  ' Open the dialog and return the selected file name
  If objDialog.ShowOpen Then
    GetFileName = objDialog.FileName
  Else
    GetFileName = ""
  End If
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of E=mc2

ASKER

Worked perfectly Bill.  My next project will be to do the same thing using a Powershell .ps1 file which will be my next question.  As usual many thanks for your assistance.