How to find the backslash in a string??
 

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

How to find the backslash in a string??

Started by Shurlock, 26 September, 2004, 20:54:50

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Shurlock

Hi all,

Need some help here.
I'm trying to find the last backslash in a string.

Example:
string is: C:\foldername\subfolder\filename.txt

I would like to find the position within the string where the last backslash is found.

Anyone that can help me out with this?

Thanks in advance.  :D
If it's raining and your roof leaks, fix the roof. DON'T try to stop the rain!!

nErBoS

#1
Hi,

Is a aproach to your question...

string = "C:\\foldername\\subfolder\\filename.txt"

	local len1,len2 = 1,strlen(string)
	while len1 ~= len2+1 do
		if (strsub(string,len1,len1) == "\\") then
			printf("FOUND: \\. POS: "..len1)
		end
	len1 = len1 + 1
	end

In a string \\ = \, because \ is use to other things.

Best regards, nErBoS
--## nErBoS Spot ##--

NotRabidWombat

local sTest = "C:\\foldername\\subfolder\\filename.txt"
local iPos = strfind(sTest, "\\[^\\]*$");
print("Position: " .. iPos);

-NotRabidWombat


I like childish behavior. Maybe this post will be deleted next.

Shurlock

Thanks guys, but.... I think my problem has just become even worse!

Here's the problem:
from within the client (DC++) I am using a 'User Command' stating the following instruction: !filewarn %[nick] %[file]

I would like to use the variable '%[file]' to detect that 'last backslash'.
But.... how do I get to the point of changing that variable result to contain a double  backslash?

The meaning of all this is:
- the present function will warn users of an undesired file
- I would like to warn users of the undesired folder in which that file was found (mostly, any folder containing a PORN file, will contain more of those files)

I believe this function to be very usefull for other hub-owners, as the result of a warning works a LOT better than just plain kicking the user.

Hope this is clear. If not, I will try to go into detail a bit further.

In any case: thanks for the help!
If it's raining and your roof leaks, fix the roof. DON'T try to stop the rain!!

Herodes

file = gsub(file, string.char(92), string.char(92)..string.char(92)) ...

possible?

Shurlock

#5
Thanks for the suggestion Herodes, but.....
I think not. The result in mainchat (which I capture of course) from the function mentioned is the following:

*** I have warned user [nickname]  about FILE: filmer\Bilar\Trafikpolisens Klipp\Are You Fucking Crazy.mpg

The problem is that, should I want to warn about the folder that contains the mentioned file, I would have to eliminate the 'filename' part.

Searching for DOUBLE 'char(92)' will not have a result, as the original presented string does not contain  two such characters.   X(

In other words: I wish the 'User commands' of the client would also provide the varaiable %[folder] and not just %[file]. But..... since the client does not provide it, I must search for other possibilities.   :rolleyes:
If it's raining and your roof leaks, fix the roof. DON'T try to stop the rain!!

bastya_elvtars

path=string.gsub(filename_with_path,".[^"..string.char(92).."]$","")

this possible?
Everything could have been anything else and it would have just as much meaning.

Shurlock

#7
I'd be eternally greatfull if that works!

But.... I also wish to know "what I'm doing here".  8)

If it's not too much to ask: What would this do?

Is the solution you wrote:
path=string.gsub(filename_with_path,".[^"..string.char(92).."]$","")
acceptable in LUA?

And - if so - would this tell me the FIRST or the LAST position of a backslash?
Whichever, that would be a less problem. A loop would do what I want it to.

(PS: sorry for the re-edits!!!)
If it's raining and your roof leaks, fix the roof. DON'T try to stop the rain!!

BottledHate

#8
here's a function i made awhile ago.. maybe it can help with what you are tryingto do.. i'm not sure...

--//-------------------------
--//VERIFY FILE (make)
--//-------------------------
--///verifyFile()\\\
--//  make file and path(s)
--//  can used to verify a save/load file at start to prevent read/write errors
--//  when the verify funtion is used no changes are made to the file if it exists. a blank file with no data is created otherwise.
--//  optional returns: 1 file exists.. 2 on success new file made returns.. 0 on fail 
--//        ...fails if path/filename is invalid to winblows. 
--//  strip the function, put in main or at top of script to verify/make your global file at start.
--//  paths may be absolute: "c:\\path\\to\\a dir\\file.txt"
--//  relative to current dir: "scripts/save data/savefile.txt"
--//  UNC(network) paths use "\\\\pcname\\share\\this folder\\file.txt" --only if proper rights exist.. ;)

--//LUA5 / BCDC / DCDM
         function verifyFile(file) --//(make file)
            local f,e = io.open(file, "a+" ) 
            if f then f:close() return 1
            else 
               local _,_,path = string.find(file, "(.+[/_\\]).+$") 
               if path ~= nil then os.execute("mkdir ".."\""..gsub(path, "/", "\\").."\"") end
               f,e = io.open( file, "a+" )
               if f then f:close() return 2
               else return 0 end
            end
         end
         
--//LUA4 / PTOKAX
         function verifyFile(file)--//(make file)
            local f,e = openfile( file, "a+" )
            if f then closefile(f) return 1
            else 
               local _,_,path = strfind(file, "(.+[/_\\]).+$") 
               if path ~= nil then execute("mkdir ".."\""..gsub(path, "/", "\\").."\"") end
               f,e = openfile( file, "a+" )
               if f then closefile(f) return 2
               else return 0 end
            end
         end
         
--//verifFile() EXAMPLE.
         MySaveFile = "scripts/data/thisbot/settings.txt"
         x = verfyFile(MySaveFile)
         if x = 0 then
            --fail..path/filename is invalid to winblows
         elseif x = 1
            --file exists.
         elseif x = 2
            --file or path did not exist.. now they do.
         end

-BH
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

NotRabidWombat

Shurlock,

The backslash is an escape character in Lua (and many other programming languages). It *escapes* other characters so they can be used in a string. A possible usage:
local sTest = "This is a \" test";

The backslash allows the developer to add a double quote into a string without the compiler considering that double quote the end of the string.

Since the backslash is used this way, you must actually escape a backslash if you want to represent it in a string, hence "\\".

Try a simple test like:
SendToAll("tester" , "This \\  is \" my \' escape \\ test");

Now that you've actually asked your final question, I would recommend:
function DataArrival(oUser, sData)
   -- do some basic user authentication (operator, admin, nick, etc)
   local flag, flag, sNickname, sFolder, sFile = 
      strfind(sData, "^%b<>%s+!filewarn%s+(%S+)%s+(.*\\)([^\\]+)%s*$");
      -- regex voodoo ;-)
   if( flag ~= nil ) then
      -- you have sNickname, sFolder, sFile in here
   end
end

By the way, 92 is the ascii value of \. That's why you were seeing people recommend strchar(92) instead of \\. More information on ascii values: http://www.asciitable.com/

-NotRabidWombat


I like childish behavior. Maybe this post will be deleted next.

Shurlock

GOT IT !!  

Thanks people, with the hints and help I finally came up with something that 'does the job'.

The function below will split the data within the variable  %[file] (which is being sent by the User command !folderwarn %[nick] %[file] )  into seperate foldername and the filename.

The faulty user is being sent a message to remove the 'FOLDER' as it contains files like 'FILE'

Hope it will be of use to someone else as well.

---------------------------------------
function FolderWarn(curUser, vUser, filename)
   local chrtr
   local folder, file = ""
   for i=1,strlen(filename) do
      chrtr = strsub(filename, i, i)
      if chrtr == "\\" then
         folder = strsub(filename, 1, i)
         file = strsub(filename, i+1, strlen(filename))
      end
   end
   local gmt = ("\n"..date("%d").."-"..date("%m").."-"..date("%y").." "..date("%H")..":"..date("%M").."")
   local Mess = "\r\n\r\n\t---<>--------------------------------------------------------------------<>---"
   Mess = Mess.."\r\n\tWarning: do NOT get kicked!"
   Mess = Mess.."\r\n\tPlease remove the following FOLDER from your share: "
   Mess = Mess.."\r\n\r\n\t\t"..folder
   Mess = Mess.."\r\n\r\n\tas it contains files that will get you kicked, such as:"
   Mess = Mess.."\r\n\t\t"..file
   Mess = Mess.."\r\n\t---<>--------------------------------------------------------------------<>---"
   Mess = Mess.."\r\n\tAfter removing, do not forget to enter the command in the mainchat:\r\n\t\t\t /refesh"
   Mess = Mess.."\r\n\r\n\r\n\tPlease respond to this message using the command:"
   Mess = Mess.."\r\n\t!ops [your response]      (Where [your response] is your reaction to this warning!)"
   SendPmToNick(vUser, curUser.sName, Mess)
   SendToOps(curUser.sName, "*** I have warned user "..vUser.."  about FOLDER: \r\n"..folder)
   appendto("Logs/foldwarn.txt")
   write(gmt,"  ", curUser.sName,"  ", vUser,"  ", folder)
   writeto()
   return 1
end

---------------------------------------
If it's raining and your roof leaks, fix the roof. DON'T try to stop the rain!!

SMF spam blocked by CleanTalk