//--- mostramos la ventana de parámetros de entrada al iniciar el script
#property script_show_inputs
//--- parámetros de entrada
input string InpSrc="source.txt"; // fuente
input string InpDst="destination.txt"; // copia
input int InpEncodingType=FILE_ANSI; // ANSI=32 o UNICODE=64
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- mostramos el contenido de la fuente (tiene que existir)
if(!FileDisplay(InpSrc))
return;
//--- comprobamos si existe ya el archivo de la copia (no es obligatorio que haya sido creado)
if(!FileDisplay(InpDst))
{
//--- el archivo de la copia no existe, el copiado sin bandera FILE_REWRITE (copiado correcto)
if(FileCopy(InpSrc,0,InpDst,0))
Print("File is copied!");
else
Print("File is not copied!");
}
else
{
//--- el archivo de la copia ya existe, intentemos copiar sin bandera FILE_REWRITE (copiado incorrecto)
if(FileCopy(InpSrc,0,InpDst,0))
Print("File is copied!");
else
Print("File is not copied!");
//--- el contenido del archivo InpDst se queda el mismo
FileDisplay(InpDst);
//--- volvemos a copiar con la bandera FILE_REWRITE (copiado correcto cuando el archivo existe)
if(FileCopy(InpSrc,0,InpDst,FILE_REWRITE))
Print("File is copied!");
else
Print("File is not copied!");
}
//--- recibida la copia del archivo InpSrc
FileDisplay(InpDst);
}
//+------------------------------------------------------------------+
//| Lectura del contenido de archivo |
//+------------------------------------------------------------------+
bool FileDisplay(const string file_name)
{
//--- reseteamos el valor del error
ResetLastError();
//--- abrimos el archivo
int file_handle=FileOpen(file_name,FILE_READ|FILE_TXT|InpEncodingType);
if(file_handle!=INVALID_HANDLE)
{
//--- mostramos en el ciclo el contenido del archivo
Print("+---------------------+");
PrintFormat("File name = %s",file_name);
while(!FileIsEnding(file_handle))
Print(FileReadString(file_handle));
Print("+---------------------+");
//--- cerramos archivo
FileClose(file_handle);
return(true);
}
//--- no se puede abrir el archivo
PrintFormat("%s is not opened, error = %d",file_name,GetLastError());
return(false);
}
|