Saving file exists... [message]
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Saving file exists... [message]

Started by TTB, 04 February, 2005, 11:45:58

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

TTB

Hi,

I have a question... I've made a script, with a function from chill's "save string".

It save's with a command text in a text file. It works fine, but I want an extra feature in it.

The extra feature would be, IF the textfile already exists, it won't write, just notify in the main chat to the current user it can't be saved because the file already exists... Here is a part of the script, so you know what I'm talking about...

function DataArrival(curUser,data)
	if (strsub(data,1,1) == "<") then
		local _,_,cmd,fileordir = strfind(data,"^%b<>%s+(%S+)%s+([^|^ ]+)")
		if cmd == (prefix.."faqsave") and curUser.bOperator then
			local _,_,sdata = strfind(data,"^%b<>%s+%S+%s+%S+%s+(.*)|$")
			if sdata then
			      	SaveFile(fileordir,sdata)
				SendToAll(BotName, curUser.sName.." saved a new FAQ:  '"..fileordir.."'          Type:    "..prefix.."faq "..fileordir.."    in the MAIN CHAT to view this new FAQ!")
				dir = (FAQFolder.."/")
				execute("dir \""..dir.."\" /o:n /b /l /d > "..indexfile..".txt")
			else
			  	curUser:SendData(BotName, "Syntax error! Use: "..prefix.."faqsave   to save your FAQ!|")
			end
			return 1


with it's function:

function SaveFile(filename,sdata)
	local handle = openfile(FAQFolder.."/"..filename,"w")
	write(handle,sdata)
	closefile(handle)
end

That function should be different, like:

function SaveFile(filename,sdata,curUser)
	   local blaat = openfile(FAQFolder.."/"..filename,"r")
           if blaat then
	    closefile(blaat)
	    curUser:SendData(BotName, "Faq already exists! Delete the FAQ before you save it!")
	   else	    
	    local handle = openfile(FAQFolder.."/"..filename,"w")
	    write(handle,sdata)
            closefile(handle)
	    SendToAll(BotName, curUser.sName.." saved a new FAQ:  '"..filename.."'          Type:    "..prefix.."faq "..filename.."    in the MAIN CHAT to view this new FAQ!")
	  end
end

I'm just started, and already came this far.  I don't get it work. I know it is wrong, but how to get this work??

TTB

(? ?.??.-> Admin @ Surfnet hubs <-.??.???)

TTB

#1
Hi,

I think I already figured out... :)  I have tried it so many times, and *bling* ...

it should be this:

function SaveFile(filename,sdata,curUser)
	   local f,sdata = openfile(FAQFolder.."/"..filename,"r")
           if f then
	    curUser:SendData(BotName, "Faq already exists! Delete the FAQ before you save it!")
            closefile(f)
	    return nil
	   else
	    local handle = openfile(FAQFolder.."/"..filename,"w")
	    write(handle,sdata)
            closefile(handle)
	    SendToAll(BotName, curUser.sName.." saved a new FAQ:  '"..filename.."'          Type:    "..prefix.."faq "..filename.."    in the MAIN CHAT to view this new FAQ!")
           end
    
end

If I do get probs with it, I will keep posting... :))
TTB

(? ?.??.-> Admin @ Surfnet hubs <-.??.???)

TTB

Hi,

Well. I do have another problem now...

I want to have this command also active in PM to the bot:

function DataArrival(curUser,data)
	if (strsub(data,1,1) == "<") then
		local _,_,cmd,fileordir = strfind(data,"^%b<>%s+(%S+)%s+([^|^ ]+)")

		--## COMMANDS WITH EXTRA TEXT ##--
		if cmd == (prefix.."faqsave") and curUser.bOperator then
			local _,_,sdata = strfind(data,"^%b<>%s+%S+%s+%S+%s+(.*)|$")
			if sdata then
			      	SaveFile(fileordir,sdata,curUser)
				dir = (FAQFolder.."/")
				execute("dir \""..dir.."\" /o:n /b /l /d > "..indexfile..".txt")
			else
			  	curUser:SendData(BotName, "Syntax error! Use: "..prefix.."faqsave   to save your FAQ!|")
			end
			return 1


The string thing could be very frustrating when it doesn't work the way I like... I have tried it with this:


elseif (strsub(data,1,5+strlen(BotName))=="$To: "..BotName) then
		data = strsub(data,1,strlen(data)-1)
		local s,e,cmd,fileordir = strfind(data, "^%b<>%s+%S+%s+%S+%s+(.*)")
		if cmd == (prefix.."faqsave") and curUser.bOperator then
			local s,e,sdata = strfind(data, "%b<>%s+%S+%s+(%S+)%s+(.*)")
			if sdata then
		      		SaveFile(fileordir,data,curUser)
				dir = (FAQFolder.."/")
				execute("dir \""..dir.."\" /o:n /b /l /d > "..indexfile..".txt")
			else
		  		curUser:SendPM(BotName, "Syntax error! Use: "..prefix.."faqsave   to save your FAQ!|")
			end
			return 1
		end
	end

I also tried some more off course... but for ppl who program a lot here... please give me the good patterns and help me with this... and what did I forget?

Thanx!
TTB

(? ?.??.-> Admin @ Surfnet hubs <-.??.???)

plop

a simple way 2 check if a file excists.
if readfrom(filename) then
   file excists
else
   file doesn't excists
end
and a example of how 2 make main chat and pm use the same functions/commands handling.
function savesomething(user, data)
   do something with the file
end

function DataArrival(user, data)
   data = strsub(data, 1, (strlen(data) - 1))
   if strsub(data, 1,1) == "<" then
	savesomething(user, data)
   elseif strsub(data, 1, 4) == "$To:" then
	local s,e,sData = strfind(data, "%$(%b<>.*)")-- this changes the data so mainchat and pm are the same.
	savesomething(user, sData)
   end
end
for you 2 find out how 2 do the rest of it.

plop
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

TTB

TTB

(? ?.??.-> Admin @ Surfnet hubs <-.??.???)

SMF spam blocked by CleanTalk