Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Locating A File, Replacing Text Within File

Status
Not open for further replies.

dnayana

Programmer
Nov 14, 2002
53
US
Is it possible to find a file on a server and replacing specific text within that file via CF code?

I would like the user to make a selection from a drop down box and based on his/her selection, I would like to write code to locate the file, find data (within the file) that contains a certain phrase, and then replace that text based on the user's selection.

Code:
<cfif isdefined("form.change")>
		<cfswitch expression="#form.color#">
			<cfcase value="Green">
				<cfif FileExists("c:\cfusionmx\[URL unfurl="true"]wwwroot\dtralink\index.cfm")>[/URL]
					<cffile action="append" 
				file="c:\cfusionmx\[URL unfurl="true"]wwwroot\dtralink\index.cfm"[/URL] 
				output="#Replace("/operating_status/os_button_includes/green.cfm", "/operating_status/os_button_includes/yellow.cfm","one")#">
					<cflocation url="index.cfm">
				</cfif>
			</cfcase>

			<cfcase value="Red">
				<cfif FileExists("c:\cfusionmx\[URL unfurl="true"]wwwroot\dtralink\index.cfm")>[/URL]
					<cfif 
					<cfoutput>#Replace("/operating_status/os_button_includes/red.cfm", "/operating_status/os_button_includes/green.cfm","one")#"></cfoutput>
					<cflocation url="index.cfm">
				</cfif>
			</cfcase>
		</cfswitch>
</cfif>


<form action="#cgi.SCRIPT_NAME#" enctype="multipart/form-data" method="post">
<table border="0">
	<tr>
		<td>Select Status</td>
		<td><select name="color">		
		<option value=""></option>
		<option value="Green">Open</option>
		<option value="Orange">Open - Early Dismissal</option>
		<option value="Red">Closed</option>
		<option value="Yellow1">Open - Unscheduled Leave</option>
		<option value="Yellow2">Open - Delayed Arrival</option>
		<option value="Yellow3">Delayed Arrival/Unscheduled Leave</option>
		</select>						
		</td>
	</tr>
<tr>
<td width="" valign="top" colspan="2" align="center"><br><input type="submit" value="Change Color" name="Change"></td>
					</tr>	
</table>
</form>

Thank you in advance to anyone who can assist/point me in the right direction.
 
I've not tried this out, but one thing you might try is to read the file in using CFFILE, then replace the text that you want to replace, then create a new file (presumably with the same name, thereby overwriting or deleting the original) with the data that contains the replaced string.

Hope that helps,

MG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top