Moving lines of a Text File up or down in a list

Started by oradba4u, October 11, 2020, 04:17:04 PM

Previous topic - Next topic

oradba4u

ALL:
I'd like to know if anyone has any code that they have developed (or point me to something in the WinBatch archives) that allows the moving of a line of text in a file being edited, to be moved up or down in the file, and then re-saved? I've seen several of these editors out there in the wild, but I'd like to see how it's done in WinBatch.

Thanks in advance,
stymied in Stubenville

kdmoyers

Questions:

1. are we talking a plain ascii text file?

2. how are we identifying the From and To positions? 
is it ordinal, like "Move 7th line to be the 2nd line"
The mind is everything; What you think, you become.

oradba4u

ASCII text file
I'd prefer to highlight the line and either use the keyboards up/down arrow keys to move it up or down the list, or push button arrows provided in the window/screen.

JTaylor

How are you editing the file?   In a WinBatch control of some kind?  If so, which control?   

Jim

oradba4u

I'd like to know if anyone has any code that they have developed (or point me to something in the WinBatch archives) that allows the moving of a line of text in a file being edited, to be moved up or down in the file, and then re-saved?

Not using anything at the moment... Looking for ideas/Methods

JTaylor

Your question was how to move a line of text within a file that is being edited.    In order to answer that question a context will be required.   You have now stated there is no context which makes the answering of your question practically impossible.  If you figure out how you are going to edit the file and post your best try at moving a line, assuming you can't make it work, I am sure someone here will be happy to help.

Jim

oradba4u

All I'm asking is does anyone have any experience doing this, and if so, REPLY to this thread to tell me how they did it. Either with code examples, or where to look in the archives (references) to do this action. (wish I could find and example of what I'm talking about, but cannot at the moment)

FOUND ONE! From MS Word "Customize Ribbon"

JTaylor

To answer it properly you need a file editing context.    What you show is easily done in that context but it doesn't fit what you are asking.

I knew responding to this thread was a mistake but I am a bit of a compulsive helper.  I would be happy to help but, once again, you refuse to put any effort into what you want and simply demand others provide solutions without even providing the needed information to answer.   

In any event, I will leave this thread alone and allow someone else to respond if they wish.  Good Luck.

Jim

oradba4u

Thanks for your reply/feedback, but I guess I don't understand what you're asking for.
"I'd like to know if anyone has any code that they have developed (or point me to something in the WinBatch archives)" is not demanding anything! Like everyone else, I'm seeking help for something I know nothing about! Isn't that the purpose of this forum? And why should I re-invent the wheel if someone else has already solved this (or similar) problem and is willing to share their code/ideas? Again, isn't that the purpose of this forum?

I thought it quite clear that I want to be able to move an entire line of text up and down in an ASCII text file and then re-save the contents. Call it an editor. Call it anything you'd like. I'm NOT going to modify or edit anything in the line itself, except it's position (of a selected/highlighted line) in the file (with regards to where it was in the file to begin). It's very similar to the example I included in my previous thread.
If I knew how to do that, there would be no reason for this post.
To me, it's like asking me to tell you what I don't know.

jmburton2001

Quote from: oradba4u on October 13, 2020, 07:44:08 PM
Thanks for your reply/feedback, but I guess I don't understand what you're asking for...

To me, it's like asking me to tell you what I don't know.

I've been coming to this forum on and off since the mid 1990s (back in the Marty and Deana days) and it's always been customary to provide SOME information, or at least a mockup of what you're trying to accomplish. JTaylor has been a member of these forums for years and I consider him one of the best sources of information, even for obscure tasks. In short, he's an expert.

There are hundreds, if not thousands, of ways to accomplish tasks in Winbatch. You need to provide examples of what you're trying to accomplish, not just words. Attach a copy (or snippet) of the text file you want to modify and then explain how you'd like to manipulate it.

As far as I can tell you have a text file that you'd like to open and move a line up or down and then save it. That sounds simple but what if the text file we create (to help you) isn't in the format of the file you're working with or we don't interpret your words correctly? If you don't know what you want... How are we supposed to know? ;)

No one here has ever owned a working crystal ball or has been able to "divine" what a user wants. When someone like JTaylor offers you help, you'd be a fool to alienate him.

kdmoyers

It's genuinely a hard-learned skill to perceive the difference between an idea in your mind, and the idea being conveyed by your words.  Even when we read back our own writing, we uncontrollably fill in context and facts from our head that are not present on the page. No malice or even carelessness is required, it's just human nature. I struggle with it all the time.

That said, it is of course a skill we can improve with practice and encouragement!  Posting a problem description here in this forum is a great example of a place where we need to add plenty of surrounding information in order to succeed.  Readers here know **nothing** about your context, platform, requirements or goals.  We come from a dizzying array of different businesses here; the only common denominators are that we use winbatch or webbatch, and that we are willing to help. (smile)

$0.02
-Kirby
The mind is everything; What you think, you become.

stanl

My .02.  In September you posted about scraping a website. In that you provided a URL and a screenshot with the same caveat about 'well if some one has already done it.....'  I had free time and in addition to my code there was interest from others on this board. Sort of emphasizes the 'you can get help mantra' that has kept me involved with this board over the years.


Now to specifics. My initial reaction to your first post was 'who cares'. Moving a line in a text file is something Notepad++ can handle, why write a program. Then I started thinking, maybe the goal is to fill out a PDF with boxes for
First Name   Middle Initial  Last Name
but your text file comes as
Bob
Smith
P.
John
Doe
J,


so you want to change to
Bob
P.
Smith   etc....


So your script to fill out PDF or other forms or reports or whatever would work. That kind of request would work on this board and probably someone, my bet would be Kirby, would reply with solution code.


Your call.   

oradba4u

THIS POST IS CLOSED as far as I am concerned.

Sometimes, gentlemen, there are reasons that explicit details cannot be devulged, as to the nature of what is trying to be accomplished. This is such an occaision. To me, moving a line of ASCII text up and down a list through the use of a winbatch program, whether it be to fill out a PDF form or other reason is not important.

I simply inquired how it's done, gave an example, and if someone had examples or could direct me to the archives.
for samples would be appreciated. Failure to do that, I will traverse other avenues to find the solution. Thank you for your time.

stanl

Well, if all you wanted is an example - use WB CLR and create a List... code below moves line 3 to line 8



List<string> lines = File.ReadAllLines("/file/path").ToList();
// change line position
String line = lines[3];
lines.RemoveAt(3);
lines.Insert(7, line);
File.WriteAllLines("/file/path", lines.ToArray());