I have no idea about batch file and Windows command line and I am trying to write a batch file which would run at a given folder location and validate filenames of all the files present there.
File names can be validated using regex:
^[a-zA-Z0-9]{1,20}\-[a-zA-Z0-9]{1,40}\-[0-9]{8}(\-[0-9]+)?\.[a-zA-Z]{2,4}$
Given regex is correct.
Or if regex is not useful, in human language, the file name should be:
alphaNumerricMax20char-alphaNumerricMax40char-8charDateofFormatyyyyMMdd.extension
I have previously written a C# code which is as follows:
rtbResult.Text = string.Empty;
List<string> fileNamesNotValid = new List<string>();
string[] allFilesInDirectory = new string[0];
try
{
if (Directory.Exists(txtFolderLocation.Text))
{
allFilesInDirectory = Directory.GetFiles(txtFolderLocation.Text);
foreach (string file in allFilesInDirectory)
{
var fileName = Path.GetFileName(file);
if (Regex.IsMatch(fileName, @"^[a-zA-Z0-9]{1,20}\-[a-zA-Z0-9]{1,40}\-[0-9]{8}(\-[0-9]+)?\.[a-zA-Z]{2,4}$"))
{
var dataComponent = fileName.Split('-')[2].Split('.')[0];
try
{
DateTime date = DateTime.ParseExact(dataComponent, "yyyyMMdd",
System.Globalization.CultureInfo.InvariantCulture);
if (date > DateTime.Now)
{
fileNamesNotValid.Add(fileName);
}
}
catch (Exception)
{
fileNamesNotValid.Add(fileName);
}
}
else
{
fileNamesNotValid.Add(fileName);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
foreach (var item in fileNamesNotValid)
{
rtbResult.Text += item + "\n";
}
MessageBox.Show("Process Completed!!\n Total Files proccessed = " + allFilesInDirectory.Count() + "\n Total Invalid Files = " + fileNamesNotValid.Count);