Results 1 to 13 of 13

Thread: [RESOLVED] Programming Challenge (not homework) Editing many files quickly

  1. #1

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Resolved [RESOLVED] Programming Challenge (not homework) Editing many files quickly

    Have a "challenge" for you guys...
    *This is not homework, an assignment, etc.. this is to help me complete a task that would otherwise take forever and curious if someone could whip out a quick solution*

    I have 202 .html files and at the beginning of each file, I need to add a chunk of php code. I could make this myself, but to be honest, it would take me forever and I might as well just manually edit them lol...

    All the .html files are in one folder.
    Majority of the files begin with this line:

    Code:
    <?php require_once('Connections/connCJ.php'); ?>
    I would like to replace that line with:

    Code:
    <?php include('mobile_device_detect.php'); mobile_device_detect('http://www.campjellystone.mobi/','http://www.campjellystone.mobi/','http://www.campjellystone.mobi/','http://www.campjellystone.mobi/','http://www.campjellystone.mobi/','http://www.campjellystone.mobi/','http://www.campjellystone.mobi/',false); ?><?php require_once('Connections/connCJ.php'); ?>
    If it does not find that line, then nothing is changed on the file. This is important! I can not just simple add that line of text to the beginning of every file, it HAS to find that line before it adds anything.

    Can someone knock something out that will do this?

    Note: Please don't explain to me how it is done. I KNOW how it can be done, I just think that someone on here could knock it MUCh faster than I could and it would save me a LOT of time.

    Thanks!

    EDIT: one other thing that would be VERY cool, is if it could add the files that are not edited to a listbox or an outputted text file.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Programming Challenge (not homework) Editing many files quickly

    So, you already know how it could be done but you're not going to tell us how that is so that we don't waste our time providing the exact same code that you already know would work. You just want someone to do your work for you so that you don't have to waste your time doing it. Is that correct? Maybe that's not your intention so, if it's not, you should re-word your post because that's the way it comes across. At least it is to me anyway.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Programming Challenge (not homework) Editing many files quickly

    that's EXACTLY what i'm saying.
    let me be more direct:

    "I want someone to do my work for me. I know this can be coded much quicker by an experienced programmer who works with File IO regularly, much quicker than I could code it myself. Please do my work for me"

    now as to how I would do it if i had all the time in the world:

    iterate through the directory's files.
    read in each one, one at a time
    search for the text
    if found, replace it with appropriate text
    if not found, add filename to listbox/textbox
    rinse repeat..

    if you don't want to do it, then go on to the next thread...

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Programming Challenge (not homework) Editing many files quickly

    If you want someone to do your work for you, there are sites for that:

    http://freelancer.internet.com/
    http://rentacoder.com

  5. #5
    Hyperactive Member Philly0494's Avatar
    Join Date
    Apr 2008
    Posts
    485

    Re: Programming Challenge (not homework) Editing many files quickly

    Code:
            For Each file As String In IO.Directory.GetFiles("C:\folder")
                Dim temp As String() = IO.File.ReadAllLines(file)
                If temp(0) = "<?php require_once('Connections/connCJ.php'); ?>" Then
                    temp(0) = "<?php include('mobile_device_detect.php'); mobile_device_detect('http://www.campjellystone.mobi/','http://www.campjellystone.mobi/','http://www.campjellystone.mobi/','http://www.campjellystone.mobi/','http://www.campjellystone.mobi/','http://www.campjellystone.mobi/','http://www.campjellystone.mobi/',false); ?><?php require_once('Connections/connCJ.php'); ?>"
                    IO.File.WriteAllLines(file, temp)
                End If
            Next

  6. #6

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Programming Challenge (not homework) Editing many files quickly

    here's what I came up with while waiting to see if someone else could knock something out:

    vb.net Code:
    1. Imports System.IO
    2.  
    3. Public Class Form1
    4.  
    5.     Dim srcFilename As String = ""
    6.  
    7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.  
    9.     End Sub
    10.  
    11.     Private Sub replaceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles replaceButton.Click
    12.         Dim path As String = "MyFilePath"
    13.         Dim dDir As New DirectoryInfo(path)
    14.         Dim fFileSystemInfo As FileSystemInfo
    15.         For Each fFileSystemInfo In dDir.GetFileSystemInfos()
    16.             Dim fs As New FileStream(fFileSystemInfo.FullName, FileMode.Open, FileAccess.ReadWrite)
    17.             Dim ioFile As New StreamReader(fs)
    18.             Dim myText As String = ioFile.ReadLine()
    19.             If myText = "stringImsearchingfor"
    20.                 myText.Replace(myText, "replacementtext")
    21.                 filesListbox.Items.Add(fFileSystemInfo.Name)
    22.             Else
    23.                 Me.notEditedTextbox.Text &= Environment.NewLine & fFileSystemInfo.Name
    24.             End If
    25.             ioFile.Close() 'Close file
    26.         Next
    27.     End Sub
    28. End Class

    the condition is being met correctly and the files (edited and nonedited) get added to the correct control (listbox if edited, textbox if not edited) but the files themselves are not being updated. am i missing something?

  7. #7
    Hyperactive Member Philly0494's Avatar
    Join Date
    Apr 2008
    Posts
    485

    Re: Programming Challenge (not homework) Editing many files quickly

    Quote Originally Posted by stateofidleness View Post
    here's what I came up with while waiting to see if someone else could knock something out:

    vb.net Code:
    1. Imports System.IO
    2.  
    3. Public Class Form1
    4.  
    5.     Dim srcFilename As String = ""
    6.  
    7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.  
    9.     End Sub
    10.  
    11.     Private Sub replaceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles replaceButton.Click
    12.         Dim path As String = "MyFilePath"
    13.         Dim dDir As New DirectoryInfo(path)
    14.         Dim fFileSystemInfo As FileSystemInfo
    15.         For Each fFileSystemInfo In dDir.GetFileSystemInfos()
    16.             Dim fs As New FileStream(fFileSystemInfo.FullName, FileMode.Open, FileAccess.ReadWrite)
    17.             Dim ioFile As New StreamReader(fs)
    18.             Dim myText As String = ioFile.ReadLine()
    19.             If myText = "stringImsearchingfor"
    20.                 myText.Replace(myText, "replacementtext")
    21.                 filesListbox.Items.Add(fFileSystemInfo.Name)
    22.             Else
    23.                 Me.notEditedTextbox.Text &= Environment.NewLine & fFileSystemInfo.Name
    24.             End If
    25.             ioFile.Close() 'Close file
    26.         Next
    27.     End Sub
    28. End Class

    the condition is being met correctly and the files (edited and nonedited) get added to the correct control (listbox if edited, textbox if not edited) but the files themselves are not being updated. am i missing something?
    you never write to the file, you are just using StreamREADER which isnt going to be much help in writing to the file.

    see what i did before your post ^^^^

  8. #8

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Programming Challenge (not homework) Editing many files quickly

    ahhh Philly that works perfectly!! and in 7 lines no less. That is the EXACT reason I posted the thread because I knew someone had a quick way of doing it.
    wonder why mine didn't work though...

    Appreciate the help very much!

    EDIT: AH I SEE! makes sense.

  9. #9
    Junior Member
    Join Date
    Nov 2009
    Location
    UK
    Posts
    16

    Re: [RESOLVED] Programming Challenge (not homework) Editing many files quickly

    Personally for this case I would not write any code at all, I'd use a text editor that can edit multiple files.

    Get Notepad++ from http://notepad-plus.sourceforge.net/ (it's free, and no I don't have any connection with it except as a user).
    One of the things Notepad++ can do is 'Replace in file' for every file in a directory matching a pattern (*.html in this case). One (careful) click and you're done!

    Notepad++ is also great for editing SQL, HTML, PHP, Perl and many other languages as it knows the language syntax and colours keywords, matches brackets, etc., making it easier to see what you are doing.

    That said, Philly's code looks a great example of how to easily Do Stuff to lots of files, if what you need to do is a little more complex than replacing one string.

  10. #10
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: [RESOLVED] Programming Challenge (not homework) Editing many files quickly

    I have another easier idea:
    If you have 200+ html files, maybe you have also Dreamweawer or similar app installed to edit/create them (you state that you "work with php" also so i guess that you are familiar with creating web sites)?!
    Do a Find/Replace inside Dreamweaver on All files.
    ... I know from my experience that this works on much more then 200+ files and it's very fast
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


  11. #11

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: [RESOLVED] Programming Challenge (not homework) Editing many files quickly

    ah gentlemen,
    I actually tried BOTH of those suggestions as my first steps. I absolutely LOVE Notepad++ and it did exactly what I needed, but the problem came when I went to "Save" the files. It would freeze my computer. Even trying to save individually one at a time woudl freeze it. Same thing with Dreamweaver. I even tried doing it like 15 files at a time and same result.

    The vb app did it almost instantaneously and even output the filenames of the files that were not edited. this was awesome!

    great suggestions though.

    thanks all

  12. #12
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Programming Challenge (not homework) Editing many files quickly

    Quote Originally Posted by stateofidleness View Post
    that's EXACTLY what i'm saying.
    let me be more direct:

    "I want someone to do my work for me. I know this can be coded much quicker by an experienced programmer who works with File IO regularly, much quicker than I could code it myself. Please do my work for me"

    now as to how I would do it if i had all the time in the world:

    iterate through the directory's files.
    read in each one, one at a time
    search for the text
    if found, replace it with appropriate text
    if not found, add filename to listbox/textbox
    rinse repeat..

    if you don't want to do it, then go on to the next thread...
    So, let me see if I've got this right. You know it can be done, you know how to do it so instead of taking an hour to do it yourself you would rather have someone who can do it faster for you by posting on a forum where it takes 6+ hours to get a response for something like that?

    Since when is an hour of your time slower than waiting a day for a fully coded response?
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  13. #13

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: [RESOLVED] Programming Challenge (not homework) Editing many files quickly

    seriously?
    if you don't like the thread, then go to the next one...

    and it only took 55 minutes, not 6+ hours...
    ...and his worked.

    hmm spend an hour reading through posts about people expressing their discontent with my thread or reading 1 post that accurately fixes the problem at hand?

    Thanks again Philly. Repped where appropriate.

    and yes, it was faster.

    thread is resolved, my challenge was met, the issue is done.
    you're showing that dead horse who's boss

Posting Permissions

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



Click Here to Expand Forum to Full Width