Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Text file -- Need help please!

    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
    Last edited by misery; 07-12-2012 at 02:46 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,636 Times in 4,598 Posts
    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).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!
    Last edited by misery; 07-12-2012 at 04:23 AM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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:-

    Code:
    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.
    Last edited by Philip M; 07-12-2012 at 08:54 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,451
    Thanks
    17
    Thanked 275 Times in 275 Posts
    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.

  • #6
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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
    Last edited by misery; 07-12-2012 at 07:06 PM.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by misery View Post
    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
    Code:
    <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.
    Last edited by Philip M; 07-12-2012 at 08:06 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.
    Last edited by misery; 07-12-2012 at 08:11 PM.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by misery View Post
    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:-

    Code:
    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).
    Last edited by Philip M; 07-12-2012 at 08:25 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #10
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    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.
    Last edited by misery; 07-12-2012 at 08:51 PM.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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/

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #12
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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.
    Last edited by Philip M; 07-12-2012 at 10:22 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    misery (07-13-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •