Parsing File details for Windows FTP Server - ftp-client

I am trying to connect to FTP server through the c# code and I am getting the list of Files and directories. And that I am saving in a ArrayList(with all attributes). I can find the FTP Server type through the SYS ftp command. I have a regular expression for UNIX based files to parse the file\directories attributes. But I have no expression for Windows FTP server files parsing. I need help in making that..
04-30-09 10:40AM <DIR> Acrobat
12-08-09 10:36PM 9058 AuthCheck.zip
12-06-09 12:49PM 174 desktop.ini
11-09-09 03:33PM <DIR> FailedPDF
I need to parse these. Date, Time, Dir\File, Name of the file
Please help. Thanks.

I don't know much about C#, but if you just need a RegEx try this one:
^(\d\d-\d\d-\d\d)\s+(\d\d:\d\d(AM|PM))\s+([\w<>]*)\s+(\d*)\s+([\w\._\-]+)\s*$
$1 = date, $2=time, $3=am or pm, $4=type (could be null), $5=size(null if dir), $6=name
or iff $4 is only or empty
^(\d\d-\d\d-\d\d)\s+(\d\d:\d\d(AM|PM))\s+(<DIR>)?\s+(\d*)\s+([\w\._\-]+)\s*$
I suppose that "<" and ">" are no special chars at c#

^(\d{2}-\d{2}-\d{2}\s*\d{2}:\d{2}(A|P)M)\s*(<DIR>){0,1}\s*(\d*)\s*(\w)\s*$
Capture groups:
0: datetime
1: Is dir
2: FileSize (or null for directory)
3: Name
I dont have a compiler handy so I cant test that, but it should be close enough to get started.

It looks like the lines have fixed structure. So you could simply do this:
Date fileDate;
bool isDir;
int fileSize;
string fileName;
fileDate=line.Substring(0,18).ParseExact("MM-dd-yy hh-sstt");
isDir=line.Substring(24,5)=="DIR";
if (!isDir)
{
fileSize=int.Parse(line.Substring(29,10).Trim());
}
fileName=line.Substring(39);

Related

How to avoid the 40 character maximum, when reading a filename from a Weblogic server?

I am trying to read the name of some files from a weblogic server.
dir.eachFileRecurse(FileType.FILES) { file ->
println file.getName()
}
However the base filename must be too long, since it's cutted of when i print the file.getName(). Looking at the deployed jar, I have the file
OnlineOfflineSomethingknowledgement-2.DDD
The result of the print however is
OnlineOfflineSomethingknowledgement-2.D
It's like 40 characters is the maximum length of the filename.
Looking at the SB-console, and look at the list of files. The 40 character maximum is also present in the web view. Hovering the mouse over the filename though, will show the full name of the file.
Is there a way to get the full file name from the code?
not clear of the environment of your script execution.
normally, there is no such limitation.
try to print the class of your dir and file variables and probably this will give you an answer.

Searching in multiple files using findstr, only proceeding with the resulting files? (cmd)

I'm currently working on a project where I search hundreds of files using findstr in the command line. If I find the string which I searched for, I want to proceed with this exact file (and the other ones that include my string).
So in my case:
I searched for the string WRI2016 by using:
H:\KOBINI>findstr "WRI2016" *.ini > %temp%\xx.txt && %temp%\xx.txt
To see what the PC does, I save it in a .txt file as you can see.
So if my file includes WRI2016 I want to extract some facts out of the file. In my case it is NR, Kunde, WebHDAktiv, DigIDAktiv.
But I just can't find a proper way to link both of these functions.
At first I simply printed all of the parameters:
H:\KOBINI>findstr "\<NR Kunde WRI2016 WebHDAktiv DigIDAktiv" *.ini > %temp%\xx.csv && %temp%\xx.csv
I also played around using the if command but that didn't really work out. I'm pretty new to this stuff as you'll see in my following tries to solve this problem:
H:\KOBINI>findstr "\<NR DigIDAktiv WebHDAktiv" set a =*.ini findstr "WRI2016" set b =*.ini if a EQU b > %temp%\xx.txt && %temp%\xx.txt
So all I wanted to achieve with that weird code was: if there is a WRI2016 in the file, give me the remaining parameters. But that didn't work out at all.
I also tried it with using new lines for every command which didn't change a thing.
As I want this to be a .csv in the end I want to add a semicolon between my parameters, any chance how I could do that? I've seen versions using -s";" which didn't do anything for me.
Sorry, I'm quite new and thought I'd give it a shot.
an example of my .ini files Looks like this:
> Kunde=Markt
> Nr=101381
> [...]
> DigIDAktiv=Ja
> WebHDAktiv=Nein
> Version=WRI2016_U2_P1
some files have a different Version though.
So I only want to know "NR, DigIDAktiv ..." if it's the 2016 Version.
As a result it should be sorted in a CSV, in different columns.
My Folder Looks like this
So I search These files in order to find Version 2016 and then try to extract my Information and put it into a .csv

Check latest file updates in directory on linux using bash shell scripting

I have basic knowledge of linux bash shell scripting, right now I am facing a problem that is like following:
Suppose I am working in an empty directory mydir
Then there is a process which is created by a C program to generate a file with one word. (Exp: file.txt would have one word, "hello")
Routinely, after a specific period of time, the file is updated by the C program with the same one word "hello".
I want check the file every time when it is updated.
But the issue is that I also want my script doing some other operation while checking the file updates and when it detects file updates that it returns something for which I can use to trigger something else.
So, can anyone help me.
Also, some proof of concept :
while true;
do
func1();
func2();
check file is updated or not
if updated ; then
break;
else
continue;
You probably want the stat command. Do man stat to see how yours works. You want to look for "modtime" or "time of last data modification" option. For mine that would be stat -c%Y file. Something like basemodtime=$(stat -c%Y file) before the loop, modtime=$(stat -c%Y file) after func2(), and then if [ $modtime != $basemodtime ]; then to detect "updated".

file command generating 'invalid argument'

I have a perl script that traverses a set of directories and when it hits one of them it blows up with an Invalid Argument and I want to be able to programmatically skip it. I thought I could start by finding out the file type with the file command but it too blows up like this:
$ file /sys/devices/virtual/net/br-ex/speed
/sys/devices/virtual/net/br-ex/speed: ERROR: cannot read `/sys/devices/virtual/net/br-ex/speed' (Invalid argument)
If I print out the mode of the file with the perl or python stat function it tells me 33060 but I'm not sure what all the bits mean and I'm hoping a particular one would tell me not to try to look inside. Any suggestions?
To understand the stats number you got, you need to convert the number to octal (in python oct(...)).
Then you'll see that 33060 interprets to 100444. You're interested only in the last three digits (444). The first digit is file owner permissions, the second is group and the third is everyone else.
You can look at each of the numbers (in your case all are 4) as 3 binary bits in this order:
read-write-execute.
Since in your case owner, group & other has 4, it is translated (for all of them) to 100 (in binary) which means that only the read bit is on for all three - meaning that all three can only read the file.
As far as file permissions go, you should have been successful reading /sys/devices/virtual/net/br-ex/speed.
There are two reasons for the read to fail:
- Either speed is a directory, (directories require execute permissions to read inside).
- Or it's a special file - which can be tested using the -f flag in perl or bash, or using os.path.isfile(...) in python.
Anyhow, you can use the following links to filter files & directories according to their permissions in the 3 languages you mentioned:
ways to test permissions in perl.
ways to test permissions in python.
ways to test permissions in bash.
Not related to this particular case, but I hit the same error when I ran it on a malicious ELF (Linux executable) file. In that case it was because the program headers of the ELF was intentionally corrupted. Looking at the source code for file command, this is clear as it checks the ELF headers and bails out with the same error in case the headers are corrupted:
/*
* Loop through all the program headers.
*/
for ( ; num; num--) {
if (pread(fd, xph_addr, xph_sizeof, off) <
CAST(ssize_t, xph_sizeof)) {
file_badread(ms);
return -1;
}
TLDR; The file command checks not only the magic bytes, but it also performs other checks to validate a file type.

How to capture file names and check for date part in the filename in Unix code?

I am a newbie to linux and I have a requirement where I need to capture the file names, check the date in the file names and proceed and load the data from all the files only if all the file names have the same date in them. Lets say, I have few files
X_US_20130420.CSV
X_CA_20130420.CSV
X_PH_20130420.CSV
X_NS_20130420.CSV
I need to check if all the files have the same date (20130420 here) and then use that date as a parameter in my next job. Please help.
There are lots of ways to go about this, but one way would be to loop through all the files, parse out the dates, and see if any date doesn't match the others. I'm not going to deprive you of the privilege of figuring out the bulk of the work, but the date parsing can be done like so:
If you have bash:
file=X_US_20130420.CSV
myDate=${file##*([A-Z_])}
myDate=${myDate%.CSV}
# myDate is now 20130420
If you don't have bash:
file=X_US_20130420.CSV
myDate="$(echo $file | sed 's:^[A-Z_]\{1,\}\([0-9]\{8\}\).CSV/\1/')"
# myDate is now 20130420

Resources