Long-filenames to short-filenames from within a string

Share your advanced PureBasic knowledge/code with the community.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Long-filenames to short-filenames from within a string

Post by Mistrel »

I'm having some fun playing with some older compilers like Borland C++. The problem I've encountered when compiling from the command-line is that some older compilers only accept short filenames. I wrote this tool to automatically convert the paths for me before I pass the parameters to the compiler.

str2sfn.pb

Code: Select all

;/***********************************************************************
;/* Program: str2sfn (Convert all instances of long-filenames within a
;/*                    string to short-filnames)
;/* Summary: 
;/*    This tool will filter long-filenames parameters encapsulated 
;/*    within double-quotes to short-filenames. It also checks all string
;/*    fields using the double-quotes character as a delimiter.
;/*    
;/*    The advantage of the additional check is to be able to convert
;/*    paths that are preceded by a command-line switch which does not
;/*    permit a preceding space.
;/* Compiler options:
;/*    Compile as a console program. 
;/************************************************************************/

OpenConsole()

HelpText.s=#CRLF$+"Convert all instances of long-filenames within a string to"
HelpText.s+#CRLF$+"short-filnames."
HelpText.s+#CRLF$+""
HelpText.s+#CRLF$+"STR2SFN [String]"
HelpText.s+#CRLF$+""
HelpText.s+#CRLF$+"  String     Null-terminated character string"
HelpText.s+#CRLF$+""

If Not CountProgramParameters()
  ;/ Print help text if no parameters exist
  Print(HelpText.s)
Else
  ;/ Replace all path and path/filename parameters with their short equivalents
  For i=1 To CountProgramParameters()
    Param.s=ProgramParameter()
    
    ;/ Parameter is a file or a path
    If FileSize(Param.s)=-2 Or FileSize(Param.s)>=0
      GetShortPathName_(@Param.s,@ShortPath.s{#MAX_PATH},#MAX_PATH)
      Param.s=ShortPath.s
    EndIf
    
    ;/ Concatenate all parameters into one string  
    If ParamString.s
      ParamString.s+" "
    EndIf
    ParamString.s+Param.s
  Next i
  
  For i=1 To CountString(ParamString.s,#DOUBLEQUOTE$)
    ;/ Check all string fields using the double-quotes ASCII character as a delimiter
    ParamField.s=StringField(ParamString.s,i,#DOUBLEQUOTE$)
    
    ;/ Parameter is a file or a path
    If (FileSize(ParamField.s)=-2 Or FileSize(ParamField.s)>=0)
      GetShortPathName_(@ParamField.s,@ShortPath.s{#MAX_PATH},#MAX_PATH)
      ;/ Replace long-filename with the short-filename in parameter string and strip superfluous quotes
      ParamString.s=ReplaceString(ParamString.s,#DOUBLEQUOTE$+ParamField.s+#DOUBLEQUOTE$,ShortPath.s)
    EndIf
  Next i
  
  Print(ParamString.s)
EndIf

End