@@ -48,7 +48,7 @@ dtutils_file.libdoc.functions["check_if_bin_exists"] = {
4848 quoted and returned. If no preference is specified and the operating system is
4949 linux then the which command is used to check for a binary in the path. If found
5050 that path is returned. If no binary is found, false is returned.]] ,
51- Return_Value = [[ result - string - the path of the binary, false if not found]] ,
51+ Return_Value = [[ result - string - the sanitized path of the binary, false if not found]] ,
5252 Limitations = [[ ]] ,
5353 Example = [[ ]] ,
5454 See_Also = [[ ]] ,
@@ -70,17 +70,17 @@ function dtutils_file.check_if_bin_exists(bin)
7070 if string.len (path ) > 0 then
7171 if dtutils_file .check_if_file_exists (path ) then
7272 if (string.match (path , " .exe$" ) or string.match (path , " .EXE$" )) and dt .configuration .running_os ~= " windows" then
73- result = " wine " .. " \" " .. path .. " \" "
73+ result = dtutils_file . sanitize_filename ( " wine " .. path )
7474 else
75- result = " \" " .. path .. " \" "
75+ result = dtutils_file . sanitize_filename ( path )
7676 end
7777 end
7878 elseif dt .configuration .running_os == " linux" then
7979 local p = io.popen (" which " .. bin )
8080 local output = p :read (" *a" )
8181 p :close ()
8282 if string.len (output ) > 0 then
83- result = output :sub (1 ,- 2 )
83+ result = dtutils_file . sanitize_filename ( output :sub (1 ,- 2 ) )
8484 end
8585 end
8686 return result
@@ -107,9 +107,14 @@ dtutils_file.libdoc.functions["split_filepath"] = {
107107function dtutils_file .split_filepath (str )
108108 -- strip out single quotes from quoted pathnames
109109 str = string.gsub (str , " '" , " " )
110+ str = string.gsub (str , ' "' , ' ' )
110111 local result = {}
111112 -- Thank you Tobias Jakobs for the awesome regular expression, which I tweaked a little
112113 result [" path" ], result [" filename" ], result [" basename" ], result [" filetype" ] = string.match (str , " (.-)(([^\\ /]-)%.?([^%.\\ /]*))$" )
114+ if result [" basename" ] == " " and result [" filetype" ]:len () > 1 then
115+ result [" basename" ] = result [" filetype" ]
116+ result [" filetype" ] = " "
117+ end
113118 return result
114119end
115120
@@ -222,15 +227,21 @@ dtutils_file.libdoc.functions["check_if_file_exists"] = {
222227}
223228
224229function dtutils_file .check_if_file_exists (filepath )
225- local result
230+ local result = false
226231 if (dt .configuration .running_os == ' windows' ) then
227232 filepath = string.gsub (filepath , ' [\\ /]+' , ' \\ ' )
228- result = os.execute (' if exist "' .. filepath .. ' " (cmd /c exit 0) else (cmd /c exit 1)' )
229- if not result then
230- result = false
233+ local p = io.popen (" if exist " .. filepath .. " (echo 'yes') else (echo 'no')" )
234+ local ans = p :read (" *all" )
235+ p :close ()
236+ if string.match (ans , " yes" ) then
237+ result = true
231238 end
239+ -- result = os.execute('if exist "'..filepath..'" (cmd /c exit 0) else (cmd /c exit 1)')
240+ -- if not result then
241+ -- result = false
242+ -- end
232243 elseif (dt .configuration .running_os == " linux" ) then
233- result = os.execute (' test -e ' .. " \" " .. filepath .. " \" " )
244+ result = os.execute (' test -e ' .. dtutils_file . sanitize_filename ( filepath ) )
234245 if not result then
235246 result = false
236247 end
@@ -266,7 +277,11 @@ dtutils_file.libdoc.functions["chop_filetype"] = {
266277
267278function dtutils_file .chop_filetype (path )
268279 local length = dtutils_file .get_filetype (path ):len () + 2
269- return string.sub (path , 1 , - length )
280+ if length > 2 then
281+ return string.sub (path , 1 , - length )
282+ else
283+ return path
284+ end
270285end
271286
272287dtutils_file .libdoc .functions [" file_copy" ] = {
@@ -373,7 +388,7 @@ dtutils_file.libdoc.functions["filename_increment"] = {
373388 "01" is added to the basename. If the filename already has an increment, then
374389 1 is added to it and the filename returned.]] ,
375390 Return_Value = [[ result - string - the incremented filename]] ,
376- Limitations = [[ ]] ,
391+ Limitations = [[ The filename will be incremented to 99 ]] ,
377392 Example = [[ ]] ,
378393 See_Also = [[ ]] ,
379394 Reference = [[ ]] ,
@@ -396,6 +411,9 @@ function dtutils_file.filename_increment(filepath)
396411 if string.len (increment ) > 2 then
397412 -- we got the filename so set the increment to 01
398413 increment = " 01"
414+ elseif increment == " 99" then
415+ dt .print_error (" not incrementing, filename has already been incremented 99 times." )
416+ return filepath
399417 else
400418 increment = string.format (" %02d" , tonumber (increment ) + 1 )
401419 basename = string.gsub (basename , " _(%d-)$" , " " )
@@ -433,8 +451,9 @@ function dtutils_file.create_unique_filename(filepath)
433451 while dtutils_file .check_if_file_exists (filepath ) do
434452 filepath = dtutils_file .filename_increment (filepath )
435453 -- limit to 99 more exports of the original export
436- if string.match (dtutils_file .get_basename (filepath ), " _(d-)$" ) == " 99" then
437- break
454+ local increment = string.match (dtutils_file .get_basename (filepath ), " _(%d-)$" )
455+ if increment == " 99" then
456+ break
438457 end
439458 end
440459 return filepath
@@ -576,6 +595,7 @@ dtutils_file.libdoc.functions["mkdir"] = {
576595 License = [[ ]] ,
577596 Copyright = [[ ]] ,
578597}
598+
579599function dtutils_file .mkdir (path )
580600 if not dtutils_file .check_if_file_exists (path ) then
581601 local mkdir_cmd = dt .configuration .running_os == " windows" and " mkdir" or " mkdir -p"
0 commit comments