Página 1 de 1

cShortName() do Clipper

Enviado: 08 Mar 2013 00:10
por Paulo Pereira
Entao, pessoal depois de ler muito.. e baixar a lib..
nao sei como compilar no harbour..
Uso o hbmk2 com arquivos HBP, so preciso converter o caminho de uma pasta de nome longo para nome curto...

Código: Selecionar todos

 
-osg.exe
-inc
-prgflag=/l
-L\clipper5\lib
-lmylib
-lxhb
-lhbct
-lhbwin
-compr=def
-rebuild
sg.prg
sg1.prg
sg2.prg
sg3.prg
funcoes.prg

cShortName() do Clipper

Enviado: 08 Mar 2013 08:11
por Pablo César
Olá Paulo, tive que desdobrar aquele tópico com a sua mensagem. Pois bibliotecas em Clipper não podem ser compiladas com Harbour. Ao menos que obtenha-se o fonte em C ou outra linguagem de baixo nível que possibilite adaptação para o Harbour. Se você está compilando com Harbour, então a sua solução está no próprio Harbour.

Aqui tem uma função que serve para mim em Harbour 3.2.0dev (Rev. 18443):

Código: Selecionar todos

Function Main()
SetMode(25,80)
Alert(cShortName("Novo Documento de texto.txt"))
Return Nil

FUNCTION cShortName ( cPath )
LOCAL cShortPathName

RETURN iif( WAPI_GetShortPathName( cPath, @cShortPathName ) > 0, cShortPathName, cPath )
Espero ter ajudado.

cShortName() do Clipper

Enviado: 08 Mar 2013 11:30
por Paulo Pereira
Show de bola..
Agradeco ao amigo.. seria estranho o Harbour nao tem uma solucao..

O objetivo final é esse...
Eu precisava gerar os arquivos do Dosprint na pasta meus documentos..
só que o Dosprint nao aceita nome longo.
eu acabava gerando na Raiz C:\ , e nem sempre podia escrever nesse lugar..
outra saida seria salvar na pasta onde esta rodando o sistema..
mas percebi que quando outro usuario vai tirar um preview em PDF, ele ve o ultimo arquivo PDF dos outros usuarios..

No inicio eu obtenho o nome da pasta que uso para varias saidas..

Código: Selecionar todos

wwmeusdoc:=alltrim(getenv("USERPROFILE")) // aqui o nome é longo
if !empty(wwmeusdoc)
   if IsDirectory(wwmeusdoc+'\Meus documentos')
      wwmeusdoc+='\Meus Documentos\'
   elseif IsDirectory(wwmeusdoc+'\MEUSDO~1')
      wwmeusdoc+='\MEUSDO~1\'
   elseif IsDirectory(wwmeusdoc+'\My Documents')
      wwmeusdoc+='\My Documents\'
   elseif IsDirectory(wwmeusdoc+'\Desktop')
      wwmeusdoc+='\Desktop\'
   else
      wwmeusdoc:='C:\'
   endif
endif
wwmeusdoc:=cshortname(wwmeusdoc)
trecho final para mandar imprimir

Código: Selecionar todos

varqsaida:=wwmeusdoc+"rel.txt"  // pode ser 'REL'+horaMinSeg se quiser gerar arquivos diferentes, essa arquivo é o mesmo para onde direcionei a saida da impressora no inicio da geracao do relatorio
SET PRINTER TO &(varqsaida)

vauxc:="@ DOSPrint.exe /LngBR /del /sel2 /sel "+varqsaida
hb_run(vcomando)  

cShortName() do Clipper

Enviado: 18 Ago 2014 16:34
por Mário Isa
Olha a função que fiz recentemente para o nomecurto

Como invocar a função:

Código: Selecionar todos

q_caminha_f := namecurto("c:\Meus Documentos\pasta azul\pasta marron")
A função:

Código: Selecionar todos

Function NameCurto(qual)
Local ritorna,ovetor:={},ctastring:=1,nomatual:=''
ritorna:=subs(qual,1,3)
qual:=subs(qual,4)

while ctastring <= len(qual)
 if subs(qual,ctastring,1) $ '/\'
  aadd(ovetor,nomatual)
  nomatual := ''
  ctastring++
  loop
 end
 nomatual += subs(qual,ctastring,1)
 ctastring++
end
aadd(ovetor,nomatual)

ctastring := 1
while ctastring <= len(ovetor)
 if len(ovetor[ctastring]) > 8 .or. ' ' $ ovetor[ctastring] // maior q 8 ou tem espaþo
  havia_espac := ' ' $ ovetor[ctastring]
  ovetor[ctastring] := menospaco(ovetor[ctastring])

  if len(ovetor[ctastring]) <= 8 .and. !havia_espac
   *
  else
   ovetor[ctastring] := trim(subs(ovetor[ctastring] ,1 ,6))+'~1'
  end
 end

 ritorna += ovetor[ctastring]+'\'


 ctastring++
end

ritorna := subs(ritorna,1,len(ritorna)-1)
O resultado retornado será:
c:\meusdo~1\pastaa~1\pastam~1

Porém há um problema:

Se numa pasta tiver duas com nomes bem parecidos tipo
em c:\meus documentos
tiver a pasta azul
e também a pasta amarela

no caso, se meu objetivo for a pasta amarela
o nome DOS dela é
pastaa~2 (por causa da azul)

mas a função vai retornar pastaa~1 assim mesmo.
:(

cShortName() do Clipper

Enviado: 18 Ago 2014 17:03
por Kapiaba
Olá, veja se te ajuda:

Código: Selecionar todos

FUNCTION Test()

    Local cFile := cGetFile32( "*.*", "Elija...",, "C:\", .F.,, )

    ? cFile, cShortName( cFile ), cShortName( cFilePath( cFile ) )

RETURN NIL

/*
 *	cShortName()
 */
FUNCTION cShortName( cLong )

   Local   cShort  := Space(164) + Chr(0)
   Local   nBuffer := 164
   Local   nLen

   nLen    := GetSPName( cLong, @cShort, nBuffer )

   cShort  := Left( cShort, nLen )

RETURN cShort

// Windows Api
DLL32 STATIC FUNCTION GetSPName( lpszLongPath AS STRING, lpszShortPath AS STRING, lBuffer AS LONG ) ;
      AS LONG PASCAL FROM "GetShortPathNameA" LIB "kernel32"
Veja tambem:

Código: Selecionar todos


/*
 *	cShortName()
 */
STATIC FUNCTION cShortName( cLong )

    local   cShort  := Space(164) + Chr(0)
    local   nBuffer := 164
    local   nLen

    nLen    := GetSPName( cLong, @cShort, nBuffer )

    cShort  := Left( cShort, nLen )

RETURN cShort

/*
 *  LfnDirectory()
*/

STATIC FUNCTION LFNDirectory( cDir, cAttr )

    Local aDir := {}, cPath := "", nSlash

    If  ( nSlash := RAt( "\", cDir ) ) > 0 .or. ;
        ( nSlash := RAt( "/", cDir ) ) > 0

        cPath := cShortName( SubStr( cDir, 1, nSlash ) )
        cDir := SubStr( cDir, nSlash + 1 )

        If Empty( cPath )    // invalid short dir from long path name
            Return aDir      // return empty array, just like Directory()
        Endif

    Endif

    aDir := Directory( cPath + cDir, cAttr ) // strings MUST be in OEM format
    AEval( aDir, {|aFile| aFile[1] := SFN2LFN( AddPath(aFile[1],cPath) ) } )

RETURN aDir

/*
 *  LFnRename()
*/

STATIC FUNCTION LFnRename( cOldName, cNewName, lDeleteIfExist )

    Local nRet

    DEFAULT lDeleteIfExist := .T.

    if !File(cOldName)
        RETURN -1
    endif

    if lDeleteIfExist .and. File(cShortName(cNewName))
        fErase(cShortName(cNewName))
    endif

    nRet := apiMoveFile(cOldName,cNewName)

    if File(cOldName)
        fErase(cOldName)
    endif

RETURN nRet

// Windows Api

DLL32 STATIC FUNCTION GetSPName( lpszLongPath AS STRING,     ;
      lpszShortPath AS STRING, lBuffer AS LONG )             ;
      AS LONG PASCAL FROM "GetShortPathNameA" LIB "kernel32"

DLL32 STATIC FUNCTION apiMoveFile(cName1 AS STRING, cName2 AS STRING) ;
      AS LONG PASCAL FROM "MoveFileA" LIB "kernel32.dll"

STATIC FUNCTION Sfn2Lfn(cShort)

    Local cLong := ""
    Local nFor, nAt

    nAt    := 1

    cShort := Alltrim( StrTran( cShort, "/", "\" ) )

    If !File( cShort ) .and. !lIsDir( cShort )
        RETURN ""
    Endif

    If SubStr( cShort, 2, 1 ) == ":"
        If Len( cShort ) < 4
            RETURN cShort
        Endif
        cLong := SubStr( cShort, 1, 3)
        nAt   := 4
    Endif

    For nFor := nAt TO Len( cShort )
        If SubStr( cShort, nFor, 1 ) == "\"
            cLong += _Sfn2Lfn( SubStr( cShort, 1, nFor - 1 ) ) + "\"
        Endif
    Next

RETURN cLong+_Sfn2Lfn( cShort )

//----------------------------------------------------------------------------//

STATIC FUNCTION _SFN2LFN( cSpec )

    Local oWin32, cBuffer, nHandle

    STRUCT oWin32
        MEMBER nFileAttributes  AS DWORD
        MEMBER nCreation        AS STRING LEN 8
        MEMBER nLastRead        AS STRING LEN 8
        MEMBER nLastWrite       AS STRING LEN 8
        MEMBER nSizeHight       AS DWORD
        MEMBER nSizeLow         AS DWORD
        MEMBER nReserved0       AS DWORD
        MEMBER nReserved1       AS DWORD
        MEMBER cFileName        AS STRING LEN 260
        MEMBER cAltName         AS STRING LEN  14
    ENDSTRUCT

    cBuffer := oWin32:cBuffer

    nHandle := apiFindFst( cSpec, @cBuffer )

    oWin32:cBuffer := cBuffer

    apiFindCls( nHandle )

RETURN Psz( oWin32:cFileName )

//----------------------------------------------------------------------------//

FUNCTION Lfn2Sfn(cDir)

    Local cBuffer := Space(261)
    Local nLen

    nLen := GetShortPathName( cDir, @cBuffer, Len(cBuffer) )

RETURN Left(cBuffer, nLen)

//----------------------------------------------------------------------------//

STATIC FUNCTION psz( c ) ; RETURN substr( c, 1, At( Chr(0), c ) - 1 )

//----------------------------------------------------------------------------//

DLL32 Function apiFindFst( lpFilename AS LPSTR,;
                           @cWin32DataInfo AS LPSTR ) AS LONG ;
                           PASCAL FROM "FindFirstFileA" LIB "Kernel32.dll"

DLL32 Function apiFindNxt( nHandle AS LONG,;
                           @cWin32DataInfo AS LPSTR ) AS BOOL ;
                           PASCAL FROM "FindNextFileA" LIB "Kernel32.dll"

DLL32 Function apiFindCls( nHandle AS LONG) AS BOOL ;
                           PASCAL FROM "FindClose" LIB "Kernel32.dll"

DLL32 FUNCTION GetShortPathName( lpLongPath AS LPSTR,;
                                 lpShortPath AS LPSTR,;
                                 wBufferLen AS DWORD ) AS DWORD ;
                                 PASCAL FROM "GetShortPathNameA" LIB "kernel32.dll"

DLL32 Function GetFullPathName( lpszFile AS LPSTR,;
                                cchPath AS DWORD,;
                                lpszPath AS LPSTR,;
                                @nFilePos AS PTR ) AS DWORD ;
                                PASCAL FROM "GetFullPathNameA" LIB "kernel32.dll"


DLL32 FUNCTION GetShortPathName( lpLongPath AS LPSTR,;
                                 lpShortPath AS LPSTR,;
                                 wBufferLen AS DWORD ) AS DWORD ;
                                 PASCAL FROM "GetShortPathNameA" LIB "kernel32.dll"

DLL32 Function GetFullPathName( lpszFile AS LPSTR,;
                                cchPath AS DWORD,;
                                lpszPath AS LPSTR,;
                                @nFilePos AS PTR ) AS DWORD ;
                                PASCAL FROM "GetFullPathNameA" LIB "kernel32.dll"
abs,


cShortName() do Clipper

Enviado: 21 Ago 2014 14:51
por Kapiaba