Re: Trying to concatenating strings to a file path
- Posted by ghaberek (admin) in May
- 514 views
I'm trying to concatenate strings to a file path, need a little help.
I would lean a bit more into the standard library routines.
--Prompt the user to enter a directory path puts(1, "Please enter the directory path: " & "\n") -- Read the entered path sequence input_path = gets(0) input_path = head(input_path, length(input_path)-1)
You can use prompt_string() here to request input from the user. This automatically strips the newline added by gets().
include std/console.e --Prompt the user to enter a directory path sequence input_path = prompt_string("Please enter the directory path: ")
-- ???? ?? ?? ??????? ??????? ???? ?????? ???? regex backslash_re = re:new (`\\`) sequence words = re:split(backslash_re,input_path) sequence directory_name = words[length(words)]
The split() function in std/regex.e is a bit heavy-handed and should only be used when you need to split using a pattern. You can use the split() function in std/sequence.e when your delimiter is static (i.e. not a pattern).
Also note that I am using the SLASH constant from std/filesys.e instead of hard-coding the backslash character. This is helpful for creating portable code and to avoid confusion about escape characters.
include std/filesys.e include std/sequence.e sequence words = stdseq:split(input_path, SLASH) sequence directory_name = words[$]
Better yet, if all you want is the "name" from a path, use the filename() function in std/filesys.e. It will return the name of the last item in the path as long as the path does not end in a slash.
include std/filesys.e include std/text.e input_path = text:trim_tail(input_path, SLASH) sequence directory_name = filesys:filename(input_path)
-- ????? ?? ????? ???? - ???? sequence asm_=".asm" sequence output_filename = input_path&"\\"&directory_name & asm_ puts(1 ,output_filename )
I would recommend either concatenating the strings with the SLASH constant or using join_path() from std/filesys.e. Although I think join_path() still erroneously adds a leading slash to the returned path on Windows. You may need to trim that off for the time being.
include std/filesys.e sequence output_filename = input_path & SLASH & directory_name & SLASH & asm_ -- or -- include std/filesys.e include std/text.e sequence output_filename = filesys:join_path({input_path,directory_name,asm_}) output_filename = text:trim_head(output_filename, SLASH)
-- ???? ????? ????? ??????? file_list = dir(input_path & "\\*.vm")
Same as above, it's much easier to concatenate with the SLASH constant.
include std/filesys.e file_list = dir(input_path & SLASH & "*.vm")
function remove_extension(sequence file_name) regex dot_re = re:new (".") sequence remove_vm = re:split(dot_re, file_name) return remove_vm[1] end function
You can use the filebase() function in std/filesys.e to get just the "base" name of the file without the extension.
include std/filesys.e function remove_extension(sequence file_name) return filesys:filebase(file_name) end function
If you want the full path and file name without extension, you could concatenate them together like this.
include std/filesys.e function remove_extension(sequence file_name) return filesys:pathname(file_name) & SLASH & filesys:filebase(file_name) end function
Hope that helps.
-Greg