...

View Full Version : Text file -- Need help please!



misery
07-12-2012, 02:36 AM
Hello,

I have a long text file with names of .mp3 files, with each mp3 name on its own line. Some of the file names have unwanted words/characters attached to the end.

I would like a code that will take a text file and remove unwanted characters on its output. I'd like the code to replace "_" with " " -- in other words, replace underscores with a space. Also, I'd like the code to keep the mp3 file name the same up until it finds 4 numbers in a row and then allow one more character and stop. Then repeat this for every line and output it as a new text file.

I'd love some feedback guys! Code suggestions or anything! Thanks :thumbsup:

Old Pedant
07-12-2012, 03:21 AM
Ummm...you won't do this with JavaScript. Unless perhaps you want to use JScript under Windows and use the Microsoft Windows Script Host.

JavaScript in the browser can't write files. And it can only read files from the SERVER (and only then by using the XMLHTTP object and only then if they are web-accessible).

misery
07-12-2012, 03:49 AM
Darn, I have no idea what you mean by "use the Microsoft Windows Script Host." Is this to be installed in addition to a software such as JCreator or Eclipse? I'm running Windows 7. I suppose I was looking for somebody to post a template Javascript code the would do the 2 functions I mentioned above.

I can get bits and pieces of the skeleton code from reading forums, but I need help compiling these specific functions. Would I use a combination of if loops, basically reading a text file and check "if finds 4 numbers in a row, leave space and remove all characters after". Then it would do this for each line.

Thanks for any feedback. I'll go elsewhere if you think that'll be best... I'm so lost!

Philip M
07-12-2012, 08:51 AM
Assuming that you have your text file, then you can use this Javascript code to
change the _ to a space and then, if 4 consecutive digits are found followed by a space, delete everything which follows the space:-


var n = "abc1234_xyz";
n = n.replace(/_/g," "); // replace all underscores by space
n = n.replace(/(.*\d{4}\s).*/i,"$1");
alert (n);

Naturally you will need to loop around and repeat for each line of the text file.

But as Old Pedant has explained Javascript cannot write to a file.


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

DrDOS
07-12-2012, 03:54 PM
OK, I try to help you, but first you have to show a couple of typical lines of your text file and exactly what you want changed in those lines, you can use the colors function to mark the 'to be changed items' when you post it. And exactly how many lines are there on this text.

misery
07-12-2012, 06:03 PM
Thanks for the replies!

My text file is very large, with about 2500 lines.

Some of the files include movie files, here's a sample list of them:

Signs 2002 TLKDA9ALKADNCLA0
Sleuth.1972.882.27fps.562kbs.v7vbr.wunseedee
Snabba.Cash.2010.SWEDiSH.VID.WpLA

As you can see, there's a date which includes 4 numbers after each file name. I want the red text to be removed...
I need a dynamic code that will identify 4 numbers in a row (a date) and remove all characters after that.
This must be repeated for each line. A space after is not needed anymore I realize, sorry.

The second function I need done is to replace underscores with a space, and also I'd like to replace periods "." with a space.

So ultimately, the 3 file names above should end up like:

Signs 2002
Sleuth 1972
Snabba Cash 2010

If I can't use Javascript to write a file, what's my best alternative?
Thanks for any code suggestions whatsoever.. I greatly appreciate it :thumbsup:

Philip M
07-12-2012, 07:36 PM
Thanks for the replies!

My text file is very large, with about 2500 lines.

Some of the files include movie files, here's a sample list of them:

Signs 2002 TLKDA9ALKADNCLA0
Sleuth.1972.882.27fps.562kbs.v7vbr.wunseedee
Snabba.Cash.2010.SWEDiSH.VID.WpLA

As you can see, there's a date which includes 4 numbers after each file name. I want the red text to be removed...
I need a dynamic code that will identify 4 numbers in a row (a date) and remove all characters after that.
This must be repeated for each line. A space after is not needed anymore I realize, sorry.

The second function I need done is to replace underscores with a space, and also I'd like to replace periods "." with a space.

So ultimately, the 3 file names above should end up like:

Signs 2002
Sleuth 1972
Snabba Cash 2010

If I can't use Javascript to write a file, what's my best alternative?
Thanks for any code suggestions whatsoever.. I greatly appreciate it :thumbsup:


<script type = "text/javascript">

var n = "Sleuth.1972.882.27fps.562kbs.v7vbr.wunseedee"
n = n.replace(/(.*\d{4}).*/i,"$1"); // remove all after 4 consecutive digits
alert (n);

var n = "Signs 2002 TLKDA9ALKADNCLA0";
n = n.replace(/(.*\d{4}).*/i,"$1");
alert (n);

var n = "Snabba.Cash.2010.SWEDiSH.VID.WpLA"
n = n.replace(/(.*\d{4}).*/i,"$1");
n = n.replace(/[\._]/g," "); // replace all periods and underscores in the truncated text by spaces
alert (n);

</script>

To write output to a file you need a server-side language such as PHP.

misery
07-12-2012, 08:06 PM
Thanks for the reply Philip,

I noticed your code actually includes the file names I'll be using. Does this mean I'll need to manually copy/paste all my file names into the code for the function to work?

I could easily be misunderstanding the code, but I was under the impression I'd need a generic code that would simply apply its function to my entire list, thus never needing to include the actual file names in the code itself. Perhaps I need to include a loop somewhere after the "var n = ".... making the variable a loop going through my entire list?

You just provided me with Javascript, but I need to use PHP to do the function I need? I'm a little confused... sorry.

Further suggestions welcome! I'm very appreciative, thank you.

Philip M
07-12-2012, 08:17 PM
Thanks for the reply Philip,

I noticed your code actually includes the file names I'll be using. Does this mean I'll need to manually copy/paste all my file names into the code for the function to work?

I could easily be misunderstanding the code, but I was under the impression I'd need a generic code that would simply apply its function to my entire list, thus never needing to include the actual file names in the code itself.

Thoughts/opinions.. I'm very appreciative, thank you.

Assuming that you are able to read your textfile using AJAX and the XMLHTTP object, I am assuming that your file would be read into a Javascript array - defined as say var myArray = [].

Each item (file name) in your list will be an element in the array.

Then you can loop through:-


for (var i = 0; i<myArray.length-1; i++) {
myArray[i] = myArray[i].replace(/(.*\d{4}).*/i,"$1");
alert (myArray[i]); // for testing
}

But you should (must) do all this using server-side coding as there is no way that Javascript running in a browser can write the altered file back to the server. Ask about how to this in the PHP forum (where you should really have posted this thread to start with).

misery
07-12-2012, 08:48 PM
do all this using server-side coding as there is no way that Javascript running in a browser can write
I reposted my question in the PHP forum.

I'm not sure why you're talking about the browser though. I was of the opinion that I'd open up some software on my home PC (PHP based software it seems).. and the code would be given the location of my text file (ex: C:/Users/Desktop/Testfile.txt) My text file is located on my desktop.

I then want this software/code to provide alterations to my text file and write out a new one to my desktop or wherever... I don't see where a browser, internet, or server-side has to come into play.

I'm clearly confused, please explain. Thanks for your patience.

Philip M
07-12-2012, 09:05 PM
There seems to be a fundamental misunderstanding. You posted this in the Javascript forum and both Old Pedant and I understood that your text file resides on a server, not the desktop of the local machine.

Javascript, other programming languages and browsers are are not relevant here. Javascript can neither read from nor write to a local file. You need a text editor which supports regular expressions.

Try TexpPad, http://www.textpad.com/

misery
07-12-2012, 09:13 PM
Ok thanks!

You say it supports regular expressions, is that a type of code/script? Which forum section is appropriate?

I'm not sure where to start once I have TextPad installed. Could somebody please provide me with a sample code, template, etc?

I'm assuming I'll have a brief code written, then I compile/build this and it will alter my locally designated text file, and output another (altered) version.

Thanks again, you're being a great help.

Philip M
07-12-2012, 10:18 PM
Regular expressions are a fairly advanced topic, and I fear probably beyond you at this stage.

You could also use free Notetab Light as an editor.

Let us say that your text file has just three lines:-

Signs 2002 TLKDA9ALKADNCLA0
Sleuth.1972.882.27fps.562kbs.v7vbr.wunseedee
Snabba.Cash.2010.SWEDiSH.VID.WpL

In Notetab light select Search > Replace. Check the box marked Regular Expression.

In the box now labeled Reg Expn type (exactly)

(.*\d{4}).*

and in the box below marked Replace With type

"$1"

Place your cursor at the start of the file, click on Replace All and the result is:-

"Signs 2002"
"Sleuth.1972"
"Snabba.Cash.2010"

You can do a simple replace if required to delete the quotation marks - find " and replace by blank (nothing).

Changing underscores and periods into spaces is another simple replace exercise.

When you are satisfied - save your text file (preferably under a different name).

This has nothing whatsoever to do with Javascript, so I would hope that marks the end of this thread. :)

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum