Copy file by mask

Copy file by mask

Postby Natter » Sat Nov 02, 2024 8: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 ?
Natter
 
Posts: 1221
Joined: Mon May 14, 2007 9:49 am

Re: Copy file by mask

Postby Enrico Maria Giordano » Sat Nov 02, 2024 9:04 am

Use a loop controlled by the result of a DIRECTORY() call.
User avatar
Enrico Maria Giordano
 
Posts: 8715
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Copy file by mask

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


Re: Copy file by mask

Postby karinha » Sat Nov 02, 2024 3:59 pm

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7824
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: Copy file by mask

Postby Enrico Maria Giordano » Sat Nov 02, 2024 4: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?
User avatar
Enrico Maria Giordano
 
Posts: 8715
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Copy file by mask

Postby Natter » Sat Nov 02, 2024 4: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
Natter
 
Posts: 1221
Joined: Mon May 14, 2007 9:49 am

Re: Copy file by mask

Postby Enrico Maria Giordano » Sat Nov 02, 2024 6:19 pm

As a workaround, try this sample:

Code: Select all  Expand view
#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
Enrico Maria Giordano
 
Posts: 8715
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Copy file by mask

Postby nageswaragunupudi » Sun Nov 03, 2024 4: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\

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 view
function CopyX()
   WaitRun( "xcopy " + cSrcDir + "my*file.txt " + cDstDir + " /Y" )
? "Copied"
return nil


another:
Code: Select all  Expand view
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
User avatar
nageswaragunupudi
 
Posts: 10642
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Copy file by mask

Postby Natter » Sun Nov 03, 2024 7:56 am

Enrico, Rao - thanks, it works. Only instead of WaitRun() function I used Shell object (so that CMD window didn't flicker)
Natter
 
Posts: 1221
Joined: Mon May 14, 2007 9:49 am

Re: Copy file by mask

Postby Enrico Maria Giordano » Sun Nov 03, 2024 8:11 am

In my sample the CMD window does not flicker. Please note the second parameter 0 (zero = SW_HIDE).
User avatar
Enrico Maria Giordano
 
Posts: 8715
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Copy file by mask

Postby nageswaragunupudi » Thu Nov 07, 2024 4:37 am

Seeking guidance from Experts.
I created files, with names encoded with different codepages like this using Harbour. (not xHarbour)
Code: Select all  Expand view
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: 10642
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Copy file by mask

Postby nageswaragunupudi » Thu Nov 07, 2024 4:46 am

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

Code: Select all  Expand view
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: 10642
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Copy file by mask

Postby nageswaragunupudi » Thu Nov 07, 2024 4:58 am

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

Re: Copy file by mask

Postby Natter » Thu Nov 07, 2024 8: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
Natter
 
Posts: 1221
Joined: Mon May 14, 2007 9:49 am

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 73 guests