|
-
Dec 3rd, 2009, 10:25 PM
#1
Thread Starter
Frenzied Member
[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.
-
Dec 3rd, 2009, 10:58 PM
#2
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.
-
Dec 3rd, 2009, 11:13 PM
#3
Thread Starter
Frenzied Member
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...
-
Dec 3rd, 2009, 11:15 PM
#4
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
-
Dec 3rd, 2009, 11:20 PM
#5
Hyperactive Member
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
-
Dec 3rd, 2009, 11:32 PM
#6
Thread Starter
Frenzied Member
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:
Imports System.IO
Public Class Form1
Dim srcFilename As String = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub replaceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles replaceButton.Click
Dim path As String = "MyFilePath"
Dim dDir As New DirectoryInfo(path)
Dim fFileSystemInfo As FileSystemInfo
For Each fFileSystemInfo In dDir.GetFileSystemInfos()
Dim fs As New FileStream(fFileSystemInfo.FullName, FileMode.Open, FileAccess.ReadWrite)
Dim ioFile As New StreamReader(fs)
Dim myText As String = ioFile.ReadLine()
If myText = "stringImsearchingfor"
myText.Replace(myText, "replacementtext")
filesListbox.Items.Add(fFileSystemInfo.Name)
Else
Me.notEditedTextbox.Text &= Environment.NewLine & fFileSystemInfo.Name
End If
ioFile.Close() 'Close file
Next
End Sub
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?
-
Dec 3rd, 2009, 11:36 PM
#7
Hyperactive Member
Re: Programming Challenge (not homework) Editing many files quickly
 Originally Posted by stateofidleness
here's what I came up with while waiting to see if someone else could knock something out:
vb.net Code:
Imports System.IO
Public Class Form1
Dim srcFilename As String = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub replaceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles replaceButton.Click
Dim path As String = "MyFilePath"
Dim dDir As New DirectoryInfo(path)
Dim fFileSystemInfo As FileSystemInfo
For Each fFileSystemInfo In dDir.GetFileSystemInfos()
Dim fs As New FileStream(fFileSystemInfo.FullName, FileMode.Open, FileAccess.ReadWrite)
Dim ioFile As New StreamReader(fs)
Dim myText As String = ioFile.ReadLine()
If myText = "stringImsearchingfor"
myText.Replace(myText, "replacementtext")
filesListbox.Items.Add(fFileSystemInfo.Name)
Else
Me.notEditedTextbox.Text &= Environment.NewLine & fFileSystemInfo.Name
End If
ioFile.Close() 'Close file
Next
End Sub
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 ^^^^
-
Dec 3rd, 2009, 11:36 PM
#8
Thread Starter
Frenzied Member
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.
-
Dec 4th, 2009, 05:20 AM
#9
Junior Member
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.
-
Dec 4th, 2009, 05:33 AM
#10
Hyperactive Member
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
-
Dec 4th, 2009, 01:00 PM
#11
Thread Starter
Frenzied Member
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
-
Dec 4th, 2009, 01:23 PM
#12
Re: Programming Challenge (not homework) Editing many files quickly
 Originally Posted by stateofidleness
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?
-
Dec 4th, 2009, 01:28 PM
#13
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|