Página 1 de 1

abertura de arquivo em pasta com nome extenso

Enviado: 02 Jan 2021 00:57
por cjp
Pessoal, por favor, esclareçam uma dúvida: como faço para abrir uma base de dados .dbf que está em uma pasta com nome longo?

Estou tentando assim: c:\"nome extenso"\agenda\base.dbf

Está errado? Está dando arquivo inexistente, mas o arquivo existe.

Agradeço qualquer ajuda.

Inacio

abertura de arquivo em pasta com nome extenso

Enviado: 02 Jan 2021 01:48
por JoséQuintas
Costumo usar assim mesmo.
É pasta protegida?

Outra opção, talvez usar a rotina do Windows de converter pra nome abreviado.

abertura de arquivo em pasta com nome extenso

Enviado: 02 Jan 2021 14:00
por rochinha
Amiguinhos,

Use as segunites funções

Código: Selecionar todos

/////////////////////////////////////////////////////////////////////////////
//
// Thanks to Jim Gale
//
/////////////////////////////////////////////////////////////////////////////
#include "Struct.ch"

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"
 
Exemplo de salvamento de um caminho longo em formato curto:

Código: Selecionar todos

REPLACE FILIAL->CAMINHO   WITH Lfn2Sfn( cPath )
Recuperando o nome curto, salvo anteriormente e alongando-o:

Código: Selecionar todos

cCAMINHO := Sfn2Lfn( FILIAL->CAMINHO )

abertura de arquivo em pasta com nome extenso

Enviado: 02 Jan 2021 14:06
por rochinha
Amiguinhos,

Tentei editar mas por duas vezes o forum me pediu login e não alterou o post, lancei mais um trecho.

Amiguinhos,

Exemplo:

Código: Selecionar todos

cLongPath := "C:\Nossa que nome de pasta grande\nao vai caber\coisa de louco\arquivo.exe"

? file( cLongPath  )
.F.

? file( Lfn2Sfn( cLongPath  ) )
.T.
Veja se funciona.

Vou limpar os cookies.

abertura de arquivo em pasta com nome extenso

Enviado: 02 Jan 2021 14:56
por alxsts
Olá!

@Rochinha

Grato pela contribuição. Poderia anexar o arquivo struct.ch?

abertura de arquivo em pasta com nome extenso

Enviado: 02 Jan 2021 16:11
por rochinha

Código: Selecionar todos

// FiveWin C language structures support commmands

#ifndef _C_STRUCT
#define _C_STRUCT

#xcommand STRUCT <oStruct> => <oStruct> := ThisStruct( TStruct():New() )

#xcommand MEMBER <cName> AS <type> ;
             [ LEN <nLen> ] ;
             [ INIT <uValue> ] ;
       => ;
          ThisStruct():AddMember( <(cName)>, <type>, <nLen> ) ;;
        [ ThisStruct():SetMember( Len( ThisStruct():aMembers ), <uValue> ) ]

#xcommand ENDSTRUCT => ThisStruct( nil )

#endif
Tá dificil manter minha conexão no site. Apaguei meu cookies, limpei temporários, mas quando tento responder me pede para logar novamente.

Notei que websiteseguro aparece na url, acho que por isto estou penando.

abertura de arquivo em pasta com nome extenso

Enviado: 02 Jan 2021 23:30
por JoséQuintas
Talvez dê pra usar direto, em todo caso, tenho anotado aqui deste jeito:

Código: Selecionar todos

FUNCTION win_GetShortPathName( cPath )

   LOCAL cShort := Space(5000)

   wapi_GetShortPathName( cPath, @cShort, Len( cShort) )

   RETURN cShort

abertura de arquivo em pasta com nome extenso

Enviado: 03 Jan 2021 01:25
por alxsts
Olá!

@Rochinha: obrigado por postar o arquivo mas, mesmo com ele, não é possível compilar o teu exemplo. O arquivo .CH referencia uma função e uma classe que não temos:

Código: Selecionar todos

ThisStruct( TStruct():New() )
Quanto aos teus problemas de acesso ao site do fórum: utilizo o Chrome como navegador e ele sempre classifica o site do fórum como "Não Seguro". Já tive o problema que você relata algumas vezes. Mas é muito raro acontecer...

abertura de arquivo em pasta com nome extenso

Enviado: 03 Jan 2021 17:13
por rochinha
Amiguinhos,

Notei que a função Lnf2Sfn() faz uso direto da API do Windows. Vendo que está declarada a getFullPathName() você pode criar a Sfn2Lfn() baseada na Lnf2Sfn()

Código: Selecionar todos

DLL32 FUNCTION GetFullPathNameA;
      ( lpFileName AS STRING, ;
        nBufferLength AS LONG, ;
        lpBuffer AS STRING, ;
        lpFilePart AS STRING ) ;
   AS LONG PASCAL; 
   FROM "GetFullPathNameA" LIB "KERNEL32"

Código: Selecionar todos

FUNCTION Sfn2LfnA(cDir)
    Local cBuffer := Space(261)
    Local nLen
    nLen := GetFullPathNameA( cDir, Len(cBuffer), @cBuffer )
    RETURN Left(cBuffer, nLen)
Mas para o seu caso use apenas:

Código: Selecionar todos

FUNCTION Lfn2Sfn(cDir)
   Local cBuffer := Space(261)
   Local nLen
   nLen := GetShortPathName( cDir, @cBuffer, Len(cBuffer) )
   RETURN Left(cBuffer, nLen)

//----------------------------------------------------------------------------//
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"
Voce transforma longos nomes em curtos.