FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Copy file by mask
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Copy file by mask
Posted: Sat Nov 02, 2024 08:09 AM
Hi,

There is a file My_ABC_file.prg The file() function sees it by the mask - file("My*File.prg") -> .T.
Is it possible to copy a file using the My*file.prg mask ?
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Copy file by mask
Posted: Sat Nov 02, 2024 09:04 AM

Use a loop controlled by the result of a DIRECTORY() call.

Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Re: Copy file by mask
Posted: Sat Nov 02, 2024 10:20 AM

The file in question has a header in UTF 8. It contains characters from another code table.

In this case, DIRECTORY() will show the file name with the characters "?" in places of characters from another code table.

Therefore, I cannot copy or change the name of this file using FW tools.

Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Copy file by mask
Posted: Sat Nov 02, 2024 04:01 PM
Natter wrote:The file in question has a header in UTF 8. It contains characters from another code table.
In this case, DIRECTORY() will show the file name with the characters "?" in places of characters from another code table.
Therefore, I cannot copy or change the name of this file using FW tools.
Can you send that file to my private mailbox so I can make some test?
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Re: Copy file by mask
Posted: Sat Nov 02, 2024 04:34 PM
https://cloud.mail.ru/public/p3ty/xhkxgTcXa

2 years ago I already solved this problem. The general solution was to change the encoding from UTF8 to ANSI.
However, Windows somehow solves this problem. If you change the title in Explorer, everything works fine.

The header of the attached file contains 2 characters with UTF encoding C

xHarbour, 2402
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Copy file by mask
Posted: Sat Nov 02, 2024 06:19 PM
As a workaround, try this sample:
Code (fw): Select all Collapse
#include "Fivewin.ch"
#include "Directry.ch"


FUNCTION MAIN()

    LOCAL aDir := DIRECTORY( "25_10_2024_*.docx" )

    LOCAL i

    FOR i = 1 TO LEN( aDir )
        ? WAITRUN( "CMD /C COPY /b " + aDir[ i, F_NAME ] + " Test" + LTRIM( STR( i ) ) + ".docx", 0 )
    NEXT

    RETURN NIL
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Copy file by mask
Posted: Sun Nov 03, 2024 04:30 AM
It does not matter whatever codepage the file names are encoded or the contents of the file are encoded.

Here is a sample to copy files matching the mast "My*File.txt" contained in the source folder "c:\natfiles\src\" into the destination folder "c:\natfiles\dst\" folder.

This is directory of c:\natfiles\src\



This folder contains 6 files. Two of them do not match the mask.
4 files match the mask. Each of them is encoded in different codepage.
1) MyOwnFile.txt (simple English)
2) MyÄÅÆFile.txt (WU ANSI codepage 1252)
3) MyДЕЖFile.txt (Russian ANSI codepage 1251)
4) MyమనవFile.txt (UTF8 codepage of any Unicode language, in this case, Telugu)

I propose 2 approaches to copy these 4 matching files into the destination folder.
Code (fw): Select all Collapse
function CopyX()
   WaitRun( "xcopy " + cSrcDir + "my*file.txt " + cDstDir + " /Y" )
? "Copied"
return nil
another:
Code (fw): Select all Collapse
function CopyHB()
   HB_CDPSELECT( "UTF8" )
   XBROWSER( aFiles  := DIRECTORY( cSrcDir + "My*File.txt" ) )
   AEval( aFiles, { |a| FileCopy( cSrcDir + a[1], cDstDir + a[1] ) } )
? "copied"
return nil
In both the cases the matching files are copied into the destination folder and also retain their original codepage encodings.



Note: Tested and working with Harbour.
Regards



G. N. Rao.

Hyderabad, India
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Re: Copy file by mask
Posted: Sun Nov 03, 2024 07:56 AM

Enrico, Rao - thanks, it works. Only instead of WaitRun() function I used Shell object (so that CMD window didn't flicker)

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Copy file by mask
Posted: Sun Nov 03, 2024 08:11 AM

In my sample the CMD window does not flicker. Please note the second parameter 0 (zero = SW_HIDE).

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Copy file by mask
Posted: Thu Nov 07, 2024 04:37 AM
Seeking guidance from Experts.
I created files, with names encoded with different codepages like this using Harbour. (not xHarbour)
Code (fw): Select all Collapse
function NatCreate()

   local aFiles := { "Own", Chr(196)+Chr(197)+Chr(198), Chr(196)+Chr(197)+Chr(198), ;
                     CHR(208)+CHR(221)+CHR(222), HEXTOSTR( "E0B0AEE0B0A8E0B0B5" ) }
   local cdp := { "DEWIN", "DEWIN", "RU1251", "TRWIN", "UTF8" }

   FW_setunicode( .t. )
   WinExec( "del c:\natfiles\src\*.*" )
   AEval( aFiles, { |c,i| aFiles[ i ] := "My" + c + "File.txt" } )
   xbrowser afiles
   AEVAL( aFiles, <|c,i|
      HB_CDPSELECT( cdp[ i ] )
      HB_MEMOWRIT( "c:\natfiles\src\" + c, c )
      return nil
      > )

return nil
Now I try to see the directory in dos-prompt:

We can see from the image is that the command prompt is using my default OEM codepage 437 (OEM English codepage)
Still all filenames created with different codepages are displayed correctly though different from the codepage used by the command window.

How?

I understand, though I am not sure, that Windows must be storing all directory information using UTF16LE.

Request our experts to guide and enlighten.
Regards



G. N. Rao.

Hyderabad, India
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Copy file by mask
Posted: Thu Nov 07, 2024 04:46 AM
Now, with this function I read the directory, display and then copy to another folder.
Code (fw): Select all Collapse
function NatRead()

   local cSrcDir  := "c:\natfiles\src\"
   local cDstDir  := "c:\natfiles\dst\"
   local aFiles
   local cmd

   FW_SetUnicode( .t. )

   WinExec( "CMD /K DIR c:\natfiles\src\*.*" )

   FW_SetUnicode( .t. )
   HB_CDPSELECT( "UTF8" )
   XBROWSER ( aFiles  := DIRECTORY( cSrcDir + "My???File.txt" ) ) ;
      COLUMNS 1, ;
              { |x,o| If( IsUtf8( o:oBrw:aRow[ 1 ] ), "UTF8", "...." ) }, ;
              { |x,o| Len( o:oBrw:aRow[ 1 ] ) }, ;
              {|x,o| StrToHex( o:oBrw:aRow[ 1 ] ) } ;
      TITLE BeforAtNum( " ", Version(), 1 )

#ifndef __XHARBOUR__
   if MsgNoYes( "Copy Files (Y/N)?" )
      AEval( aFiles, { |a| FileCopy( cSrcDir + a[1], cDstDir + a[1] ) } )
      ? "Copied"
   endif
#endif

return nil
First I built this sample with Harbour:



The result is OK. Can see the filenames in the directory correctly and also could copy to the other folder.
Regards



G. N. Rao.

Hyderabad, India
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Copy file by mask
Posted: Thu Nov 07, 2024 04:58 AM
Now, I tried to build the same code with xHarbour and this is the result.



I request guidance as to how should I write this function with xHarbour to display correct filenames and copy them
Regards



G. N. Rao.

Hyderabad, India
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Re: Copy file by mask
Posted: Thu Nov 07, 2024 08:13 AM
Such a feature has emerged. Functions like filecopy() work fine if there is no '*' character in the file name.
CMD copy and xcopy allow you to copy files with the '*' symbol in the name, but only if all folder names in the path to the file are in Latin