Tag: write file to new folder

Reading text file data from folder

HI, Below sample code used to read multiple files

  1. Read data from file,Write to new file in different folder
  2. Delete files

Read and write file:

public void writeToHistoryFile(FilePath _fromFilePath)//Source filepath
{
#File
System.IO.StreamWriter streamWriterHistory;
Counter countFiles;
FileName fileNameHistory;
str inputLineHistory;
Set permissionSetHistory;
str directoryHistory, filePathHistory;
container fileListConHistory, record;
CommaTextIo commaTextIoHistory;

directoryHistory = _fromFilePath;
if(directoryHistory == “”)
{
return;
}
fileListConHistory = this.findMatchingFiles(directoryHistory);
//permissionSetHistory.add(new InteropPermission(InteropKind::ClrInterop));
for (countFiles=1; countFiles <= conlen(fileListConHistory); countFiles++)
{
ttsBegin;
inputLineHistory = “”;
fileNameHistory = “Newfolderpath”;

fileNameHistory += “\\newfilename”+”_”+Winapi::createGUID()+”_”;
fileNameHistory += date2Str(today(), 213, 2, DateSeparator::None, 2, DateSeparator::None, 4);
fileNameHistory +=” “;
fileNameHistory += time2str(timeNow(),TimeSeparator::Dot, TimeFormat::AMPM);
fileNameHistory += “.txt”;
streamWriterHistory= new System.IO.StreamWriter(fileNameHistory,
false,
System.Text.Encoding::GetEncoding(28591));

permissionSetHistory = new Set(Types::Class);

permissionSetHistory.add(new InteropPermission(InteropKind::ClrInterop));
filePathHistory = conPeek(fileListConHistory,countFiles);

permissionSetHistory.add(new FileIOPermission(filePathHistory, ‘r’));
permissionSetHistory.add(new FileIOPermission(filePathHistory, ‘rw’));
CodeAccessPermission::assertMultiple(permissionSetHistory);

commaTextIoHistory = new CommaTextIo(filePathHistory,#io_read);
if (! commaTextIoHistory || commaTextIoHistory.status() != IO_Status::Ok)
{
throw error(“@SYS19358”);
}
while (commaTextIoHistory.status() == IO_Status::Ok)
{
record = commaTextIoHistory.read();

if(!record)
{
CodeAccessPermission::revertAssert();
break;
}

//record = conIns(record, conLen(record) +1);
if(!inputLineHistory)
inputLineHistory = con2Str(record);
else
inputLineHistory = “\n” + con2Str(record);

streamWriterHistory.WriteLine(inputLineHistory,true);
}
CodeAccessPermission::revertAssert();

// }
streamWriterHistory.Close();
commaTextIoHistory.finalize();
ttsCommit;
}
}

Delete file:

static boolean importFromWorkstation(FilePath _deleteFile = “”)
{
//MPSWorkstation _tWorkstation
int iNumberOfFiles;
FileName sFileName,fileNamePerm, inFile;
InteropPermission interopPerm;
Set permissionSet, permSetFiles;
System.Array files;
int i, j;
container fList;
#File
;

permissionSet = new Set(Types::Class);
permissionSet.add(new InterOpPermission(InteropKind::ClrInterop));

CodeAccessPermission::assertMultiple(permissionSet);

files = System.IO.Directory::GetFiles(_deleteFile);
for( i=0; i<ClrInterop::getAnyTypeForObject(files.get_Length()); i++ )
{
fList = conins(fList, conlen(fList)+1, ClrInterop::getAnyTypeForObject(files.GetValue(i)));
}

for(j=1;j<=conlen(fList); j++)

{
sFileName = conpeek(fList, j);
inFile = sFileName;
ttsbegin;

System.IO.File::Delete(inFile);

ttscommit;
}
CodeAccessPermission::revertAssert();

return true;
}