(Solved)Reading file from a Ruby Generated path


MarkMc
 Share

Recommended Posts

I can read a file using-

File.read ("D:/Mark/Documents/_ Current Kit/ _Price/test_1.csv")
works once backslashes are changed to forward -requires parenthesis and quote

 

So I copy and paste a path statemtent into an OIP filed (using supplier) and then use a macro to change the slashes and add parenthesis and quotes
that works fine using-

#first reads the file from the Supplier field in the cabinet DBX, then it changes the back slashes to forward slashes so Ruby can use it
myFile= owner.supplier.gsub(/\\/,'/')
# next need to parenthesis and quotes for the file to be found -looks like ("C:/some file...")
path= ('("')+myFile+('")')

 

I've tried a few variations on that which also work then I'm trying to read that file.

question is once I have that result how can I get Ruby to read the file?

 

Also if someone on a Mac can open the program and let me know if you get the same results as the posted image please?

macro test.plan

Results should be.png

Link to comment
Share on other sites

Thanks Glen, was afraid that would happen. Guess I either have to add some code or write a version specific to a Mac. That latter is easier for me but I'm going to be giving this away and may be confusing for others, and I guess it's better if it worked either way. Guess I have to check to see if Ruby in Chief can understand what OS it's on.

Link to comment
Share on other sites

29 minutes ago, solver said:

Not sure what you are asking, but this opens and reads the file into a.

Eric- I can read a file with a path as in2127973276_canreadthefilewiththis.thumb.png.98da719444eb775fb24fdf33965bacc3.png

For that I changed the backslashes to forward before placing in the macro-that is a nogo in the long run.

What I can't get to read is when I generate the altered path within the macro.

1236185316_makethisreadthefilelistedintheresult.thumb.png.489265ff51223435ca02e3280ad9db2e.png

Tried more than a few variations.

If what you post will work I don't understand how to use it and utilize the altered path in the result shown.

Link to comment
Share on other sites

48 minutes ago, glennw said:

Mark,

I get this on a mac.

New Image_32.jpg

Glen I just realized that I didn't include the file so naturally you get an error for the read macro. I think to find out if this works on both I first have to get the longer macro to read on windows. Then will try posting something again, thanks.

Link to comment
Share on other sites

7 minutes ago, solver said:

f="d:\\rb.txt"  <---- the value of f is d:\rb.txt -- you don't need to add the quotes or parenthesis.

fh=open f

a=fh.read

fh.close

a

I still don't see how to make that work even if I don't use the macro- tried this after making a new text file, copying the path and pasting

get error-

f=C:\\Users\mark\Documents\A_Current\test new.txt

fh=open f

a=fh.read

fh.close

a

 

are you saying to add this file as an rb to the scripts folder? prefer not to go there for this.

don't understand.png

Link to comment
Share on other sites

3 minutes ago, solver said:

 

f="C:\\Users\\mark\\Documents\\A_Current\\test new.txt"

 

Ruby, and other languages use the \ character with a string to allow the insertion of special characters. If you want the \ character, then it must be doubled.

 

The quotes delimit a string value.

Ok so I got that to work so long as I add the extra slashes and quotes manually.

Note that above I list a statement with File.read ("C:/somewhere/doc.text") that also reads when I do it manually.

So I have two ways to MANUALLY alter a path and get it to read.

However if I go and write something to automatically add the extra slashes and the quotes I'm pretty sure I'm going to end up with the same problem. How do I use the result to read?

 

I want to have it so someone can paste a path into the Supplier field and have it read properly without user needing to adjust the path statement.

Link to comment
Share on other sites

7 minutes ago, solver said:

The supplier field is C:\Users\mark\Documents\A_Current\test new.txt

 

This line gets that value and changes either \ or / into \\

 

Don't know if you need to test for both. If not change [/|\] to \

 

f=owner.supplier.gsub("[/|\]","\\")

 

 

Thanks Eric but I'm not getting anywhere and been at this all day- Did you open the file I posted? already had gsub written. I'll pick this up again tomorrow, fried, thanks again

Link to comment
Share on other sites

From reading your posts, I haven't the slightest idea of what your trying to accomplish.

 

Trying to understand why you don't just put the file in your scripts directory and let Chief handle the path automatically for the MAC or PC.

 

File.read ($LOAD_PATH[0] +"test_1.txt") Eliminates need for any path.

 

Note that macs do not use letter drives

  • Upvote 1
Link to comment
Share on other sites

28 minutes ago, GerryT said:

File.read ($LOAD_PATH[0] +"test_1.txt") Eliminates need for any path.

 

Thanks Gerry,  I am working on a slightly different project but this looks like a nice solution for me and I think it will work for Mark if I am following along correctly.  Any idea why X10 would be referencing my X8 scripts folder when the file path in Preferences is clearly marked as X10 ?

 

Also wondering if there is a way to deal with the extra " "" that the text file generates around a measurement in feet and inches ie. ( 6' 8" ) seems to automatically come out as ( " 6' 8""" ) in the text file originally sourced from excel.  Can ruby handle this within Chief and find and remove the three extra " 's ?

Link to comment
Share on other sites

18 minutes ago, Chopsaw said:

 Any idea why X10 would be referencing my X8 scripts folder when the file path in Preferences is clearly marked as X10 ?

Most likely it was copied from a previous upgrade.

 

Change the path in preferences/general/ruby from x8 to x10

Link to comment
Share on other sites

27 minutes ago, GerryT said:

Change the path in preferences/general/ruby from x8 to x10

 

Thanks,  that worked.

 

32 minutes ago, solver said:

Yes. You just need to write the code.

 

Would that search the entire imported text file and fix all instances ?   If so that would be great.  Could you help with that ?  Ruby is something I hope to learn in the future but have not begun to grasp it quite yet.

Link to comment
Share on other sites

Quote

 

Trying to understand why you don't just put the file in your scripts directory and let Chief handle the path automatically for the MAC or PC.

 

File.read ($LOAD_PATH[0] +"test_1.txt") Eliminates need for any path.

 

Note that macs do not use letter drives

 

 

I wanted to avoid the scripts directory if possible to keep it simpler for end user.

Re: File.read ($LOAD_PATH[0] +"test_1.txt")

That makes the path problem easier and likely simplifies the cross platform issues AND it works within the macro.

HOWEVER IF I place the file location or name into an OIP field  and try to get that data into a macro that reads the file it has the same problems I ran into already.

 

The problem is that while the OIP field output looks correct -be it a correctly formatted path OR any variation I can come up of ($LOAD_PATH[0] +"test_1.txt") I don't know how to include the output from the field or another macro into a "File.read" statement.

Since the text file can change on a regular basis AND I don't want end users changing code I want to be able to place the text file in an OIP field.

 

11 hours ago, GerryT said:

From reading your posts, I haven't the slightest idea of what your trying to accomplish.

 

 

Here's what I'm trying to do-

The short answer

This is for the cabinet pricng system I started last summer. I looked for ways to simplify it for the end user (I don’t need it myself but promised it to some folks). The purpose is to generate quick ball park pricing. Data goes to schedule which is copied to spreadhseet template. Final pricing can be done with some manipulation of the spreadsheet or copying from the sheet to an on line ordering system.

 

How-user places files somewhere on system (there will be multiple files), defines file in OIP field, macro looks up pricing for cabinet, output to schedule, copy schedule info to a spreadsheet template.

 

Limit what a user needs to do or not

  • No altering code
  • Generate text files and place somewhere they understand
  • Enter data in OIP fields in cabinet DBX or defaults
  • Occasionally add price into an OIP filed for a cabinet not included in files (but can be in library)
  • Process-temporarily remove columns from schedule, copy, paste into spreadsheet template, undo to restore shedule fields (be nice if two schedules could have user defined numbering in sync..)
  • For brands that use style/species pricing instead of columns enter those prices in sheet (could be in the spreadsheet template)

 

Considerations

The typical end user is used to other software for cabinet pricing where you install a catalog, select price category from a drop down list, click report, done. At the same time I have to deal with my limitations with Ruby and avoid as many pitfalls for end user as I can. OTOH I’m not interested in this being everything possible

A typical kitchen dealer will carry between 3 and 6 brands, some of those could have 2 to 4 lines with different pricing. More than half of the cabinet brands out there use groups for pricing (A,B,C,D…) The pricing changes annually. All that can make for either large complicated files (and macro) or a lot of files. To that end-

  • Text files will include only the most common cabinet styles and prices.
  • Text files will be limited to a single price group and only the most sold groups included (ROI for included something that is sold once a year or less is lousy)

 

For rarely sold categories get out your pencil. (hey, I’ve been getting prices out of CA for 5 years without it and could have done twice all those cabinet orders in the less time than spent on this :wacko:)


 

 

Link to comment
Share on other sites

19 minutes ago, solver said:

The contents of the file are displayed in the schedule in both the Label and custom field. 

 

I'm guessing that the macro should read the file, compare the cabinet identifier with data in the file, on finding a match it gets the price and returns that instead of the contents of the entire file as I've done.

Yabadabadoo- Thanks that works fine. Thanks so much.

I have no idea why it didn't work for me last night, tried it six ways to Sunday when after you posted-must have been pilot/syntax error or such, was at it for 10 hours by then and had written at least 3 dozen versions prior to posting-on a good day I struggle with this.

Your guess is correct-I now have to define where to find what to match and the lookup then formatting that result with extra determiners for output so columns sort properly in spreadsheet. Baby steps...

 

WOW- I was just about to ask to see if this would work on a Mac so copied the macro and eliminated gsub. It still works on Windows without the substitution!! I don't know why I could not get the originals to read without changing the slashes and I'd seen several complaints about folks having to do that on stackoverflow (where I understand very little). This is great. I'm guessing it's because you have the file open in there instead of just read?

Link to comment
Share on other sites

Depending on what are you trying to accomplish, below will give you a location of CA/csv/myL

17 hours ago, GerryT said:

From reading your posts, I haven't the slightest idea of what your trying to accomplish.

 

Trying to understand why you don't just put the file in your scripts directory and let Chief handle the path automatically for the MAC or PC.

 

File.read ($LOAD_PATH[0] +"test_1.txt") Eliminates need for any path.

 

Note that macs do not use letter drives

 

NOTE: THIS IS NOT THE CORRECT WAY TO USE PATH

 

libx1 = File.expand_path("test_1.txt", $LOAD_PATH[0])

=> "C:/Users/browntiger/Documents/Chief Architect Premier X10 Data/Scripts/test_1.txt"

Call can (also include backslashes) libx1 = File.expand_path("../test_1.txt", $LOAD_PATH[0])

 

or this

mycsv = File.expand_path("../csv/myLibrary.csv", __FILE__)

or 

File.read(File.expand_path...

Link to comment
Share on other sites

9 hours ago, solver said:
20 hours ago, Chopsaw said:

Would that search the entire imported text file and fix all instances ?   If so that would be great.  Could you help with that ? 

 

Post a representative example of the data and I'll have a look.

 

Thanks Eric,  this is just the sample file I have been tinkering with.

Siding Schedule.JPG

Siding Test.txt

Link to comment
Share on other sites

Ok the you explained the gsub and it makes perfect sense but where does it go ?  This is just for display of data as text not an actual schedule as I could not get that to work in chief for this particular situation if you did not pick that up already.

 

eric_gsub.JPG

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share