cancel
Showing results for 
Search instead for 
Did you mean: 

How to pick the multiples based on file size

Former Member
0 Kudos

Hi All,

my sender file adapter needs to pick up 5 files based on file size.

for example 1file size is 500kb,2nd file size 300kb,3rd file size 400kb, 4file size 100kb and 5file size 600kb.

here my requirement is, my file adapter needs to pick in the below order like 5th file,1st file,3rd file 2nd file and 4th file.

means based on file size, i need to pick up my file adapter.

could you please ang inputs on this requirement.

Thanks & Regards,

AVR

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

hi all,

any inputs plz.

Regards,

AVR

anupam_ghosh2
Active Contributor
0 Kudos

hi AVR,

Scripts can solve this problem. If PI server is on linux/unix platform u need shell scripts, in case of windows you need batch file. I think forum members might be able suggest better solutions if you would please kindly furnish the following information

1. What is the version of PI you are working on?

2. What is the operating system of the PI server?

3. when do you require the files to be picked up by PI server for processing?When do you require sorting process to begin?Is it that once 5 files are in source directory the sorting begins or the sorting happens once a day? This information is necessary because once you put a file in source directory PI server will start processing it. According to your requirement you need to sort the files on basis of size before PI starts processing it. Thus you are waiting for file count to reach say 5 or you are doing it once a day at a particular time etc.

regards

Anupam

Former Member
0 Kudos

Hi Anupam,

i am using pi7.0 and xp.

u mean to say that,i need to write batch file and need to apply the OS command after/before message processing.

Regards,

AVR

anupam_ghosh2
Active Contributor
0 Kudos

Hi AVR,

If XP is operating system of PI server, then you need batch files. You have still not answered question 3 in my last post.The answer to your query "u mean to say that,i need to write batch file and need to apply the OS command after/before message processing." depends on your answer to question 3 above. Still I feel this scripts needs to keep on running in server in infinite loop, this is because , you have to collect files before sorting. Also there is no inputs regarding the frequency of file reception in PI server. If you do not need to collect the files before sorting on basis of file size then you can use the feature "apply the OS command after/before message processing."

But if you are not collecting the files in one place before sorting then there remains no way you can sort them on basis of size.

Hence I feel the script will wait till all files are collected for a day or be of a particular number. Instead of scripts you might use codes in programming language such as C or java.

There might be some much better solutions than this,you can wait for some time for experts to respond. But writing scripts is definately one of the solutions.

regards

Anupam

Former Member
0 Kudos

hi anupam,

it may be 2 cases.

1. five files are placed in the source directory at a time.

2.have to wait till end of the day,and need to pick up the files.

Regards,

AVR

anupam_ghosh2
Active Contributor
0 Kudos

Hi AVR,

The Batch file/java/c code should run in infinite loop within the server. This should serve the following purposes

Case 1:

1. continuously count the number of files in directory say "c:\apps\acm" after some time gap say every 10 minutes

2. if the file count has reached 5 then sort the files on basis of their size in decending order.

3. place the files one by one after definite time interval (more than polling time of the sender communication channel).

in target directory say "c:\apps\acm1" from where PI server picks up the files for further processing. The file with smallest size is placed last.

Here are the java codes which does exactly the same

case1:

 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 


public class sortFilesOnSize { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
// TODO Auto-generated method stub 
try 
{ 
/* 
* In Unix/Linux OS, dir1="/usr/apps/test"  etc 
*/ 
int pollingInterval=10,sleepTime=10*60; 
String dir1="c:\\apps\\acm"; 
String dir2="c:\\apps\\acm1"; 
File fread=new File(dir1); 
File fwrite=new File(dir2); 
if(fread.canRead()==false) 
{ 
System.out.println("error: "+dir1+" does not have read permission. Program Terminates."); 
return; 
} 
if(fwrite.canWrite()==false) 
{ 
System.out.println("error: "+dir2+" does not have write permission. Program Terminates."); 
return; 
} 
String fileNames[],fileNamesOut[]; 
long fileSize[]; 
int maxCount=2,i,j; 
byte b[]; 
int t=10; 
while(t>0) 
{ 
//loop unless file count reaches maxCount value 
if(fread.list().length<maxCount) 
{ 
/* 
* in case you wanna to make this thread sleep for 
* say sleepTime(10) minutes before it checks the files once again 
* because looping continuously causes wastage of CPU cycles.    
*/ 
Thread.sleep(sleepTime*1000); 
continue; 
} 
//read list of files 
fileNames=fread.list(); 
fileSize=new long[fileNames.length]; 
fileNamesOut=new String[fileNames.length]; 
//read their sizes 
for(i=0;i<fileNames.length;++i) 
{ 
fileNamesOut<i>=fileNames<i>; 
fileNames<i>=dir1+System.getProperty("file.separator";)+fileNames<i>; 
fileSize<i>=new File(fileNames<i>).length(); 
System.out.println(fileNames<i>+" size="+fileSize<i>); 
} 
//sorting on basis of file size descending order 
long value; 
String temp; 
for(i=1;i<fileSize.length;++i) 
{ 
value=fileSize<i>; 
temp=fileNames<i>; 
for(j=i-1;j>=0 && fileSize[j]<value;--j) 
{ 
fileSize[j+1]=fileSize[j]; 
fileNames[j+1]=fileNames[j]; 
} 
fileSize[j+1]=value; 
fileNames[j+1]=temp; 
} 

//now copy files to dir2 
b=new byte[512]; 
for(i=0;i<fileNames.length;++i) 
{ 
System.out.println(fileNames<i>+" size="+fileSize<i>); 
} 
for(i=0;i<fileNames.length;++i) 
{ 
File f=new File(fileNames<i>); 
FileInputStream in=new FileInputStream(f); 
FileOutputStream out=new FileOutputStream(dir2+System.getProperty("file.separator";)+fileNamesOut<i>); 
int len=0; 
while(2>1) 
{ 
if((len=in.read(b))<0) 
{ 
break; 
} 
out.write(b,0,len); 
} 

in.close(); 
//delete files after copying from dir1 
f.delete(); 
out.close(); 
//put each file after polling interval is over 
Thread.sleep(pollingInterval*1000); 
} 

} 

} 
catch(Exception e) 
{ 
e.printStackTrace(); 
} 
} 

} 

The codes run in infinite loop. You need to run them in command line in DOS environment as you indicated that you OS is WIN XP.

Hope this solves your problem.

regards

Anupam

anupam_ghosh2
Active Contributor
0 Kudos

Hi AVR,

for case 2:

1. At specific time each day "23:58:00" hours ,count the number of files in directory say "c:\apps\acm".

2. sort the files on basis of their size.

3. place the files one by one after definite time interval (more than polling time of the sender communication channel).

in target directory say "c:\apps\acm1" from where PI server picks up the files for further processing. The file with smallest size is placed last.

4. In this case you need to ensure all files are present in the directory "c:\apps\acm" before "23:58:00" hours.

for case 2 java code

 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.util.Calendar; 
import java.util.GregorianCalendar; 


public class sortFilesOnSpecificTime { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
// TODO Auto-generated method stub 
try 
{ 
/* 
* In Unix/Linux OS, dir1="/usr/apps/test"  etc 
*/ 
int pollingInterval=10,sleepTime=1*60; 
String dir1="c:\\apps\\acm"; 
String dir2="c:\\apps\\acm1"; 
File fread=new File(dir1); 
File fwrite=new File(dir2); 
if(fread.canRead()==false) 
{ 
System.out.println("error: "+dir1+" does not have read permission. Program Terminates."); 
return; 
} 
if(fwrite.canWrite()==false) 
{ 
System.out.println("error: "+dir2+" does not have write permission. Program Terminates."); 
return; 
} 
String fileNames[],fileNamesOut[]; 
long fileSize[]; 
int i,j; 
byte b[]; 
int t=4; 
Calendar cal; 
int hour24,min,fileCopyHour=23,fileCopyMin=58; 
long waitSeconds=1,currentTime=0; 
while(t>0) 
{ 
cal = new GregorianCalendar(); 
hour24 = cal.get(Calendar.HOUR_OF_DAY);     // 0..23 
min = cal.get(Calendar.MINUTE);             //0..59 
System.out.println("current time="+hour24+":"+min); 

/*loop unless time reaches a specific predetermined value 
* predetermined values are provided by values 
* fileCopyHour=8,fileCopyMin=30 i.e say 08:30 hours 
*/ 
currentTime=(hour24*60+min)*60; 
waitSeconds=(fileCopyHour*60+fileCopyMin)*60 - currentTime; 
if(waitSeconds>0) 
{ 
/* 
* in case you wanna to make this thread sleep for 
* say sleepTime(10) minutes before it checks the files once again 
* because looping continuously causes wastage of CPU cycles.    
*/ 
Thread.sleep(waitSeconds*1000); 
} 
//read list of files 
fileNames=fread.list(); 
if(fileNames.length;=0) 
{ 
/* 
* time is up but there are no file 
* in dir1 to copy. Then this program 
* goes to sleep for some time and 
* checks only at 11:55 hours. That is 
* end of the day 
* */ 
continue; 
} 
fileSize=new long[fileNames.length]; 
fileNamesOut=new String[fileNames.length]; 
//read their sizes 
for(i=0;i<fileNames.length;++i) 
{ 
fileNamesOut<i>=fileNames<i>; 
fileNames<i>=dir1+System.getProperty("file.separator";)+fileNames<i>; 
fileSize<i>=new File(fileNames<i>).length(); 
System.out.println(fileNames<i>+" size="+fileSize<i>); 
} 
//sorting on basis of file size descending order 
long value; 
String temp; 
for(i=1;i<fileSize.length;++i) 
{ 
value=fileSize<i>; 
temp=fileNames<i>; 
for(j=i-1;j>=0 && fileSize[j]<value;--j) 
{ 
fileSize[j+1]=fileSize[j]; 
fileNames[j+1]=fileNames[j]; 
} 
fileSize[j+1]=value; 
fileNames[j+1]=temp; 
} 

//now copy files to dir2 
b=new byte[512]; 
for(i=0;i<fileNames.length;++i) 
{ 
System.out.println(fileNames<i>+" size="+fileSize<i>); 
} 
for(i=0;i<fileNames.length;++i) 
{ 
File f=new File(fileNames<i>); 
FileInputStream in=new FileInputStream(f); 
FileOutputStream out=new FileOutputStream(dir2+System.getProperty("file.separator";)+fileNamesOut<i>); 
int len=0; 
while(2>1) 
{ 
if((len=in.read(b))<0) 
{ 
break; 
} 
out.write(b,0,len); 
} 

in.close(); 
//delete files after copying from dir1 
f.delete(); 
out.close(); 
//put each file after polling interval is over 
Thread.sleep(pollingInterval*1000); 
} 

} 

} 
catch(Exception e) 
{ 
e.printStackTrace(); 
} 
} 

} 

The code runs in infinite loop. You need to run them in command line in DOS environment as you indicated that you OS is WIN XP. I have a few print statements which I kept for debugging, you can safely remove them and run the codes. This code is independent of the Operating System you are using. Only change is required in values of "dir1","dir2", timings and file count, which I think you can take care easily.

Hope this solves your problem.

regards

Anupam