How to: Read a directory on a batch job using .NET system classes instead of WINAPI

Today I’m going to show you how to read a directory and loop through its files but this will also work on a batch job.

So first we have the findfiles method (line 69) , this will ask for the directory and the extention of the files we are going to search. On the findfiles method we are going to use .NET libraries so this will run on a batch job (lines 19 to 24). As always when working with directories we have to first assert permissions so we don’t run on any CLR error.(lines 37 to 42)

 X++ |  copy code |? 
01
02
public void scanImportDirectory()
03
{
04
    CustParameters          custParameters;
05
    #File
06
 
07
    Filename fullFileName(Filename _path, FileName _fileName)
08
    {
09
        Filename pathName;
10
        Filename fileName;
11
        Filename fileExtension;
12
        ;
13
        [pathName,fileName,fileExtension]= fileNameSplit(_fileName);
14
        return _path+'\\'+fileName+fileExtension;
15
    }
16
 
17
    void findFiles(Filename _path,Filename _fileName,boolean _inclSubDir = true,Filename _prefix = fullFileName(_path,_fileName))
18
    {
19
        //.NET Libraries start
20
        System.IO.DirectoryInfo         di;
21
        System.Type                     arrayType;
22
        System.Array                    array;
23
        System.IO.FileInfo              fi;
24
        //.NET Libraries end
25
        FilePath                        filePath, moveFilePath, shortFile;
26
        FileNameOpen                    fileNameOpen;
27
        FileIOPermission                dirPermission, filePermission;
28
        InteropPermission               interopPermission;
29
        Set                             permissionSet;
30
        int i;
31
        int l;
32
 
33
        ;
34
 
35
        interopPermission = new InteropPermission(InteropKind::ClrInterop);
36
        filePath = _path;
37
        //Read directory and assert permissions
38
        dirPermission = new FileIOPermission(filePath, 'R');
39
        permissionSet = new Set(Types::Class);
40
        permissionSet.add(interopPermission);
41
        permissionSet.add(dirPermission);
42
        CodeAccessPermission::assertMultiple(permissionSet);
43
 
44
        di = new System.IO.DirectoryInfo(filePath);
45
        //Construct the array
46
        arrayType = System.Type::GetType("System.IO.FileInfo");
47
        array = System.Array::CreateInstance(arrayType, 1);
48
        array = di.GetFiles();
49
        l = array.get_Length();
50
        CodeAccessPermission::revertAssert();
51
        if (l > 0)
52
        {
53
            for (i = 0; i < l; i++)
54
            {
55
                //Loop through the files and assert permissions
56
                interopPermission = new InteropPermission(InteropKind::ClrInterop);
57
                interopPermission.assert();
58
                fi = array.GetValue(i);
59
                fileNameOpen = fi.get_FullName();
60
                CodeAccessPermission::revertAssert();
61
                filepermission = new FileIOPermission(fileNameOpen, 'RW');
62
                interopPermission = new InteropPermission(InteropKind::ClrInterop);
63
                permissionSet = new Set(Types::Class);
64
                permissionSet.add(interopPermission);
65
                permissionSet.add(dirPermission);
66
                CodeAccessPermission::assertMultiple(permissionSet);
67
                shortFile = fi.get_FullName();
68
                //PROCESS THE FILE HERE
69
                CodeAccessPermission::revertAssert();
70
            }
71
        }
72
 
73
    }
74
    //Process start
75
    findFiles("YOURDIRECTORY",'*.txt');
76
}
77

Next we set an array (lines 45 to 50) that will save all the filenames, we will loop through this array and again assert permissions (lines 55 to 66). Finally we can process our file, meaning you can for example read the file and use the content to generate a sales order, a journal or anything you may need. Please note that the extension is optional, you can always send a “*” if you want to get all the files in the directory.

And that’s it! I have show you how to create a method that will read a directory and that will also work on a batch job. Usin .net libraries instead of WINAPI.

  • Itsuki Minami

    by LP