Search fileName and get full path based on a pattern in C# - c#-4.0

How do I get the full path for a file based on first 6 letters in the filename in C#? Is there any Regex for this?
I have a folder with 500K+ images.
All the images are named as per the database column value.
I am querying the table and based on the column value I have to search the folder for the specific file.
If the column value is 209050 there will be one file in the folder like 209050.pdf or 209050_12345.pdf. There will always be one image for 209050.
Please help

var files = Directory.EnumerateFiles(#"C:\MyRootDir", "209050*", SearchOption.AllDirectories);
This will return an enumerable of strings. Each string will the full path of the file whose name matches the specified pattern, in this case, any file whose name begins with '209050', irrespective of the extension. This will even search within sub directories of the folder MyRootDir.
If you wish to only filter for jpg files, change the 2nd argument accordingly.
var files = Directory.EnumerateFiles(#"C:\MyRootDir", "209050*.jpg", SearchOption.AllDirectories);
In case you are NOT using .Net Framework 4 or 4.5, you could use the GetFiles method instead.
var files = Directory.GetFiles(#"C:\MyRootDir", "209050*.jpg", SearchOption.AllDirectories);

The following will show you all .jpg files starting with 209050 in C:\MyDirectory:
using System;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] myFiles = Directory.GetFiles(#"C:\MyDirectory");
foreach (var x in myFiles)
{
string[] splitName = x.Split('\\');
string fileName = splitName[splitName.Length - 1];
if (fileName.StartsWith("209050") && fileName.EndsWith(".jpg"))
{
Console.WriteLine(x);
}
}
Console.ReadLine();
}
}
}
Here is my directory:
Here is the output:
Does this help?

Related

How can I turn a txt file into a string just using its path?

So I have a program that turns a .txt file into a string to then send it via bluetooth to a printer, the problem is that right now I'm doing it using the file name but I wanted to do it only using the path of the file, this has to do with the fact that I need to search on the folder for any existing txt files and if there are any I need to print the first one and then delete it, so I can't be doing it by using the file's name. This is my code so far:
private fun readFile() String {
val file = File(storage/emulated/0/IS4-PDF-RDP/00233116695912019091310005913BLUETOOTH.txt)
var ins InputStream = file.inputStream()
read contents of IntputStream to String
var content = ins.readBytes().toString(Charset.defaultCharset())
return content
}
You can find the first file in the folder read it and then delete it as per your requirements
File("/storage/emulated/0/IS4-PDF-RDP/").walk().find {
it.extension == "txt"
}?.apply {
inputStream().readBytes().toString(Charset.defaultCharset())
delete()
}

Rename PDF file

I would like to create a custom module that renames the generated PDF files by taking a generated filename string containg some field values from index fields, batch fields etc.
So when it comes to batch processing i could go for this (setupTransformator contains the parsed values from the custom storage strings)
public void ProcessBatch(IBatch batch)
{
IACDataElement batchElement = GetBatchElementFromBatch(batch);
IACDataElementCollection currentDocuments = GetDocumentsFromBatchElement(batchElement);
IACDataElement customStorageStrings = GetCustomStorageStringsFromBatch(batch);
IACDataElementCollection batchFields = GetElementsByName(batchElement, ResourcesKofax.BATCH_FIELDS, ResourcesKofax.BATCH_FIELD);
setupTransformator = new SetupTransformator(customStorageStrings);
for (int i = 0; i < currentDocuments.Count; i++)
{
int currentDocumentIndex = i + 1;
IACDataElement currentDocument = currentDocuments[currentDocumentIndex];
IACDataElementCollection indexFields = GetElementsByName(currentDocument, ResourcesKofax.INDEX_FIELDS, ResourcesKofax.INDEX_FIELD);
string targetFilename = setupTransformator.GetFilename(batchElement, currentDocument, batchFields, indexFields);
string documentFilePath = currentDocument[ResourcesKofax.PDF_GENERATION_FILE_NAME];
// rename the PDF file
}
batch.BatchClose(KfxDbState.KfxDbBatchReady, KfxDbQueue.KfxDbQueueNext, 0, string.Empty);
}
private IACDataElement GetBatchElementFromBatch(IBatch batch)
{
IACDataElement rootElement = batch.ExtractRuntimeACDataElement(0);
return rootElement.FindChildElementByName(ResourcesKofax.BATCH);
}
private IACDataElementCollection GetDocumentsFromBatchElement(IACDataElement batchElement)
{
return GetElementsByName(batchElement, ResourcesKofax.DOCUMENTS, ResourcesKofax.DOCUMENT);
}
private IACDataElement GetCustomStorageStringsFromBatch(IBatch batch)
{
IACDataElement setupElement = batch.ExtractSetupACDataElement(0);
IACDataElementCollection batchClasses = GetElementsByName(setupElement, ResourcesKofax.BATCH_CLASSES, ResourcesKofax.BATCH_CLASS);
IACDataElement batchClass = batchClasses[1];
return batchClass.FindChildElementByName(ResourcesKofax.BATCH_CLASS_CUSTOM_STORAGE_STRINGS);
}
private IACDataElementCollection GetElementsByName(IACDataElement dataElement, string rootName, string targetName)
{
return dataElement.FindChildElementByName(rootName).FindChildElementsByName(targetName);
}
Do I have to use the File.Move method or is there a method from the Kofax library I can use?
File names should be handled by Export Connectors only. As long as the batch is in the system you shouldn't have their names changed as this could lead to data loss and corruption.
This especially applies when using field values for a PDF's name - since values are subject to change as long as the batch is in the system, how would you accommodate this? Nothing is stopping your users from processing a batch in your custom module and set the batch back to validation and change one or more fields.
Speaking of the Export Connector and its API:
By default, Kofax offers two methods to export a PDF - both on the ReleaseData object (this is taken from the API docs):
CopyKofaxPDFFile: Copy the PDF file that belongs to a document into the export PDF path that is defined during export setup.
CopyKofaxPDFFileToPath: Copy the PDF file that belongs to a document into a specified path (the path is a string input argument for this method).
Both methods make use of something that you could define during Setup - for example, CopyKofaxPDFFile makes use of the KofaxPDFPath property. I am not sure if there's a property reserved for the file name.
I usually stick with the KofaxPDFProperty exposed during runtime and perform a File.Copy operation. I wouldn't recommend moving the file or deleting it as this is something that KC automatically handles once the batch was exported successfully (in theory, there could be another export, or the export might just fail).
Use the ReleaseData object to access field values, and string interpolation to define the PDF's final name.

Google Apps Script creates sheets version of excel file. Issue with multiple creation of versions.

I found a solution for my original question in another post Google Apps Script creates sheets version of excel file.
Testing with the code provided in the answer I ran into another issue. Every time I run the script it creates the Spreadsheets version of the .xlsx files again even if they already exist. I have tried modifying the code withing the last If with no results. Then went back to run your code as posted in case I have missed something but it keeps creating versions of the same files.
Any idea of what could I do to fix this will be really appreciated.
The code provided int he answer is the following.
// Convert the user's stored excel files to google spreadsheets based on the specified directories.
// There are quota limits on the maximum conversions per day: consumer #gmail = 250.
function convertExcelToGoogleSheets()
{
var user = Session.getActiveUser(); // Used for ownership testing.
var origin = DriveApp.getFolderById("origin folder id");
var dest = DriveApp.getFolderById("destination folder id");
// Index the filenames of owned Google Sheets files as object keys (which are hashed).
// This avoids needing to search and do multiple string comparisons.
// It takes around 100-200 ms per iteration to advance the iterator, check if the file
// should be cached, and insert the key-value pair. Depending on the magnitude of
// the task, this may need to be done separately, and loaded from a storage device instead.
// Note that there are quota limits on queries per second - 1000 per 100 sec:
// If the sequence is too large and the loop too fast, Utilities.sleep() usage will be needed.
var gsi = dest.getFilesByType(MimeType.GOOGLE_SHEETS), gsNames = {};
while (gsi.hasNext())
{
var file = gsi.next();
if(file.getOwner().getEmail() == user.getEmail())
gsNames[file.getName()] = true;
}
// Find and convert any unconverted .xls, .xlsx files in the given directories.
var exceltypes = [MimeType.MICROSOFT_EXCEL, MimeType.MICROSOFT_EXCEL_LEGACY];
for(var mt = 0; mt < exceltypes.length; ++mt)
{
var efi = origin.getFilesByType(exceltypes[mt]);
while (efi.hasNext())
{
var file = efi.next();
// Perform conversions only for owned files that don't have owned gs equivalents.
// If an excel file does not have gs file with the same name, gsNames[ ... ] will be undefined, and !undefined -> true
// If an excel file does have a gs file with the same name, gsNames[ ... ] will be true, and !true -> false
if(file.getOwner().getEmail() == user.getEmail() && !gsNames[file.getName()])
{
Drive.Files.insert(
{title: file.getName(), parents: [{"id": dest.getId()}]},
file.getBlob(),
{convert: true}
);
// Do not convert any more spreadsheets with this same name.
gsNames[file.getName()] = true;
}
}
}
}
You want to convert Excel files in origin folder to Google Spreadsheet and put the converted Spreadsheet to dest folder.
When the filename of converted file is existing in dest folder, you don't want to convert it.
If my understanding is correct, how about this modification?
From:
if(file.getOwner().getEmail() == user.getEmail() && !gsNames[file.getName()])
To:
if(file.getOwner().getEmail() == user.getEmail() && !gsNames[file.getName().split(".")[0]])
Note:
In this modification, when the filename of converted file is found in the dest folder, the file is not converted.
When the filename has the extension like ###.xlsx and it is converted to Google Spreadsheet, it seems that the extension is automatically removed. I think that this is the reason that the duplicated files are created. So I used split(".")[0] for this situation.
Reference:
split()

Replace part of a filename in C#

I have a folder with .pdf files. In the names of most files I want to replace specific string with another string.
Here's what I've written.
private void btnGetFiles_Click(object sender, EventArgs e)
{
string dir = tbGetFIles.Text;
List<string> FileNames = new List<string>();
DirectoryInfo DirInfo = new DirectoryInfo(dir);
foreach (FileInfo File in DirInfo.GetFiles())
{
FileNames.Add(File.Name);
}
lbFileNames.DataSource = FileNames;
}
Here I extract all file names in List Box.
private void btnReplace_Click(object sender, EventArgs e)
{
string strReplace = tbReplace.Text; // The existing string
string strWith = tbWith.Text; // The new string
string dir = tbGetFIles.Text;
DirectoryInfo DirInfo = new DirectoryInfo(dir);
FileInfo[] names = DirInfo.GetFiles();
foreach (FileInfo f in names)
{
if(f.Name.Contains(strReplace))
{
f.Name.Replace(strReplace, strWith);
}
}
And here I want to do the replacing, but something is going wrong. What?
It sounds like you want to change the name of the file on disk. If so then you need to use the File.Move API vs. changing the actual string which is the file name.
One other mistake you are making is the Replace call itself. A string in .Net is immutable and hence all of the mutating APIs like Replace return a new string vs. changing the old one in place. To see the change you need to assign the new value back to a variable
string newName = f.Name.Replace(strReplace, strWith);
File.Move(f.Name, newName);
f.Name is a read-only property. f.Name.Replace(..) simply returns a new string with the filename you want, but never actually changes the file.
I suggest something along the following, though I haven't tested it:
File.Move(f.Name, f.Name.Replace(strReplace, strWith));
Replace return another string, it doesn't change the original string.
So you need to write
string newName = f.Name.Replace(strReplace, strWith);
of course this doesn't change the name of the file on disk.
If that was your intention then you should look at
File.Move(f.Name, newName);
also keep in mind that File.Move will fail with an exception if the destination file exists.
See here for an example
At a first glance, seems like you're not reassigning the replaced string to your f.Name variable. Try this:
string NewFileName = f.Name.Replace(strReplace, strWith);
File.Copy(f.Name, NewFileName);
File.Delete(f.Name);
When you call string.Replace this doesn't alter your existing string. Instead it is returning a new string.
You need to change your code to something like this:
if(f.Name.Contains(strReplace))
{
string newFileName = f.Name.Replace(strReplace, strWith);
//and work here with your new string
}

Is there a way to get all embedded objects in .xlsx file using xssf event mdel api

Is there a way to get all embedded objects in .xlsx file using xssf event model api?
Usermodel has the method workbook.getallembedds...similarly is there anything in eventmodel?
This is an example in usermodel.I want to implement the same functionality using eventusermodel.Kindly help.
for (PackagePart pPart : workbook.getAllEmbedds()) {
String contentType = pPart.getContentType();
if (contentType.equals(------)
Instead of xssfworkbook(in usermodel), in the eventmodel code i have a containerObject of type OPCPackage.
#Gagravarr : Thanks for your reply. I tried using the method suggested by you...but im unable to get the contents of the embedded excel.Could you please help me find out where I am going wrong.Here is a part of the code:
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(container);
XSSFReader xssfReader = new XSSFReader(container);
XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator)xssfReader.getSheetsData();
for(PackageRelationship rel : iter.getSheetPart().getRelationshipsByType(XSSFRelation.OLEEMBEDDINGS.getRelation()))
embedds.add(getTargetPart(rel));
for (PackagePart pPart :getAllEmbedds()) {
String contentType = pPart.getContentType();
// Excel Workbook - OpenXML file format
if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml")) {
OPCPackage excelObject = OPCPackage.open(pPart.getInputStream());
`
Your best bet is probably just to enumerate all the package parts, and find the ones that interest you from that
Alternately, the logic to identify embedded parts attached to a given sheet is pretty simple:
List<PackagePart> embedds = new LinkedList<PackagePart>();
// Get the embeddings for the workbook
for(PackageRelationship rel : sheet.getSheetPart().getRelationshipsByType(XSSFRelation.OLEEMBEDDINGS.getRelation()))
embedds.add(getTargetPart(rel));
for(PackageRelationship rel : sheet.getSheetPart().getRelationshipsByType(XSSFRelation.PACKEMBEDDINGS.getRelation()))
embedds.add(getTargetPart(rel));
return embedds;
Finally all I used was this!
ArrayList<PackagePart> parts = container.getParts();
for (PackagePart pPart :parts) {
String contentType = pPart.getContentType();
if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {

Resources