Copy file by mask

Natter
Posts: 1226
Joined: Mon May 14, 2007 9:49 am

Copy file by mask

Post by Natter »

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 ?
User avatar
Enrico Maria Giordano
Posts: 8728
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Copy file by mask

Post by Enrico Maria Giordano »

Use a loop controlled by the result of a DIRECTORY() call.
Natter
Posts: 1226
Joined: Mon May 14, 2007 9:49 am

Re: Copy file by mask

Post by Natter »

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.
User avatar
karinha
Posts: 7885
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil
Contact:

Re: Copy file by mask

Post by karinha »

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
Enrico Maria Giordano
Posts: 8728
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Copy file by mask

Post by Enrico Maria Giordano »

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?
Natter
Posts: 1226
Joined: Mon May 14, 2007 9:49 am

Re: Copy file by mask

Post by Natter »

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
User avatar
Enrico Maria Giordano
Posts: 8728
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Copy file by mask

Post by Enrico Maria Giordano »

As a workaround, try this sample:

Code: Select all | Expand

#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
User avatar
nageswaragunupudi
Posts: 10691
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Copy file by mask

Post by nageswaragunupudi »

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\

Image

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: Select all | Expand

function CopyX()
   WaitRun( "xcopy " + cSrcDir + "my*file.txt " + cDstDir + " /Y" )
? "Copied"
return nil
another:

Code: Select all | Expand

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.

Image

Note: Tested and working with Harbour.
Regards

G. N. Rao.
Hyderabad, India
Natter
Posts: 1226
Joined: Mon May 14, 2007 9:49 am

Re: Copy file by mask

Post by Natter »

Enrico, Rao - thanks, it works. Only instead of WaitRun() function I used Shell object (so that CMD window didn't flicker)
User avatar
Enrico Maria Giordano
Posts: 8728
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Copy file by mask

Post by Enrico Maria Giordano »

In my sample the CMD window does not flicker. Please note the second parameter 0 (zero = SW_HIDE).
User avatar
nageswaragunupudi
Posts: 10691
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Copy file by mask

Post by nageswaragunupudi »

Seeking guidance from Experts.
I created files, with names encoded with different codepages like this using Harbour. (not xHarbour)

Code: Select all | Expand

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:
Image
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
User avatar
nageswaragunupudi
Posts: 10691
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Copy file by mask

Post by nageswaragunupudi »

Now, with this function I read the directory, display and then copy to another folder.

Code: Select all | Expand

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:

Image

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
User avatar
nageswaragunupudi
Posts: 10691
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Copy file by mask

Post by nageswaragunupudi »

Now, I tried to build the same code with xHarbour and this is the result.

Image

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
Natter
Posts: 1226
Joined: Mon May 14, 2007 9:49 am

Re: Copy file by mask

Post by Natter »

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
Post Reply