Page 1 of 2
Copy file by mask
Posted: Sat Nov 02, 2024 8:09 am
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 ?
Re: Copy file by mask
Posted: Sat Nov 02, 2024 9:04 am
by Enrico Maria Giordano
Use a loop controlled by the result of a DIRECTORY() call.
Re: Copy file by mask
Posted: Sat Nov 02, 2024 10:20 am
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.
Re: Copy file by mask
Posted: Sat Nov 02, 2024 3:57 pm
by karinha
Re: Copy file by mask
Posted: Sat Nov 02, 2024 3:59 pm
by karinha
Re: Copy file by mask
Posted: Sat Nov 02, 2024 4:01 pm
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?
Re: Copy file by mask
Posted: Sat Nov 02, 2024 4:34 pm
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
Re: Copy file by mask
Posted: Sat Nov 02, 2024 6:19 pm
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
Re: Copy file by mask
Posted: Sun Nov 03, 2024 4:30 am
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\
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.
Note: Tested and working with Harbour.
Re: Copy file by mask
Posted: Sun Nov 03, 2024 7:56 am
by Natter
Enrico, Rao - thanks, it works. Only instead of WaitRun() function I used Shell object (so that CMD window didn't flicker)
Re: Copy file by mask
Posted: Sun Nov 03, 2024 8:11 am
by Enrico Maria Giordano
In my sample the CMD window does not flicker. Please note the second parameter 0 (zero = SW_HIDE).
Re: Copy file by mask
Posted: Thu Nov 07, 2024 4:37 am
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:
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.
Re: Copy file by mask
Posted: Thu Nov 07, 2024 4:46 am
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:
The result is OK. Can see the filenames in the directory correctly and also could copy to the other folder.
Re: Copy file by mask
Posted: Thu Nov 07, 2024 4:58 am
by nageswaragunupudi
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
Re: Copy file by mask
Posted: Thu Nov 07, 2024 8:13 am
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