WEBVTT 00:07.120 --> 00:13.390 In this video you're going to continue working with the accessing method of the child process module 00:14.110 --> 00:16.470 and then use it in your product. 00:17.110 --> 00:26.840 So since it's a sync version of the method we will still need to make sure we handle errors effectively. 00:27.340 --> 00:31.660 So let's use a try catch statements 00:41.330 --> 00:52.950 like this and let's says the outputs to a variable for example result equals this and now it's console. 00:52.970 --> 00:54.520 The log results 00:57.330 --> 00:58.810 and let's see what we get 01:01.680 --> 01:02.330 here we go. 01:02.330 --> 01:10.770 You can see our current location right that now you want to calculate the size of a folder. 01:10.850 --> 01:20.240 So if you remember in the command line crash course we used the DB command and to get the human readable 01:20.240 --> 01:28.740 formats you are the option S and H followed by the path of the folder thus you are interested in. 01:28.940 --> 01:30.740 So let's try this one for instance 01:35.780 --> 01:41.970 so it says no such file or directory and the reason why is because we have to escape the spaces. 01:42.270 --> 01:44.760 Okay so if you try 01:49.320 --> 01:54.770 the same command while escaping the spaces like this 02:02.980 --> 02:04.810 so it still does not work. 02:07.210 --> 02:15.630 There is a space that I skipped and now I get the size of this folder. 02:15.700 --> 02:24.970 Now if you do this in your node code you will need to place the path of the folder following your command 02:27.880 --> 02:34.020 do you minus s h inside bulk goods like this. 02:34.020 --> 02:39.660 So I strongly recommend using that workers to make sure it works in all systems. 02:39.660 --> 02:41.610 So here we go. 02:43.260 --> 02:46.010 So let's save and try our code 02:49.070 --> 02:51.790 and here we are getting the correct results. 02:51.860 --> 03:01.350 Now you still need to manipulate this string to get your data but for now you can see does it does work. 03:01.490 --> 03:09.290 Now if you aren't working with only one method of this node module then there is no need to define the 03:09.290 --> 03:18.650 whole module that you can just define the method execs sink like that and then you go to the module 03:18.680 --> 03:20.720 and then you write dots. 03:23.900 --> 03:32.730 Execs sing and then you use the function straightaway so if you try that's it should be fine. 03:34.290 --> 03:35.320 All right. 03:35.500 --> 03:39.470 Now another way of writing this using S6. 03:39.510 --> 03:44.090 You can just place this inside curly brackets. 03:48.610 --> 03:52.490 And remove those and it still works. 03:52.510 --> 04:01.950 So this is similar to writing that you are defining an object and you're setting the parameter execs 04:01.960 --> 04:03.340 think of the objects 04:07.570 --> 04:11.890 to a variable X X ink which you are sitting to this object. 04:11.890 --> 04:15.670 So this object has of course also a perimeter executing. 04:15.730 --> 04:23.770 So was was gonna happening in a store inside this variable the value or the property X axing of these 04:23.800 --> 04:24.730 objects. 04:24.760 --> 04:35.910 All right so this is a really good use of E6 restructuring OK so now let's go back to our projects and 04:36.390 --> 04:41.070 use this function so I'm gonna copy Lance 04:43.940 --> 04:45.530 and use his other top 04:52.770 --> 04:53.520 like nuts 04:56.780 --> 05:06.570 and also let's use something similar to this. 05:06.610 --> 05:12.930 Now you want to apply that code to your variable item for static path. 05:13.030 --> 05:18.360 Now as we said earlier you really need to escape any spaces that you've crossed inside. 05:18.580 --> 05:23.460 So you will need to convert that variable to a new variable. 05:23.620 --> 05:33.720 So let's call it so first of all let's say that we can escape spaces tabs etc.. 05:33.760 --> 05:39.650 Ok so in our case it's only spaces but we're gonna use a method which will remove anything like that. 05:39.660 --> 05:47.640 So spaces new line tabs etc. Even though we are interested mainly in spaces in this you know in other 05:47.640 --> 05:50.640 scenarios you may want to remove everything. 05:51.510 --> 06:00.310 So let's define a new variable and call it item for static path cleaned. 06:00.660 --> 06:11.340 So we're going to take that one and then we can replace old spaces so let's see what regular expression 06:11.500 --> 06:17.490 we're gonna use and we can replace all those with an escaped space. 06:17.490 --> 06:23.780 So the regular expression you want to use here is backslash s. 06:24.010 --> 06:28.860 So this will correspond to any space tab on your line. 06:28.870 --> 06:29.550 All right. 06:29.560 --> 06:34.180 I'm going to add the global option to replace all of them. 06:34.180 --> 06:36.950 Not only the first one. 06:37.430 --> 06:38.910 So this is the clean path. 06:38.920 --> 06:48.470 And now if you apply exec sink using that command then inside here you can just place your variable 06:50.310 --> 06:51.900 item or stack cleaned 06:55.000 --> 06:59.620 and let's call this variable command 07:02.890 --> 07:03.340 outputs 07:06.190 --> 07:06.420 right. 07:06.420 --> 07:26.950 So now let's just console not a command outputs and see what we get. 07:26.950 --> 07:37.240 All right so for that for all the nasty results we're getting that's another one and so on. 07:37.260 --> 07:37.520 All right. 07:37.520 --> 07:38.900 Perfect. 07:38.900 --> 07:44.980 So now we need to manipulate each of these results and gets only the information that we need. 07:45.110 --> 07:48.830 We only want that piece of information. 07:48.830 --> 07:57.710 So I suggest you pause the video here then extract that's bits from the results which will be your human 07:57.770 --> 08:07.730 readable format and then convert those results to bytes which will go in your second but I mean it or 08:07.730 --> 08:10.360 use second elements in the array. 08:10.520 --> 08:16.460 So once you finish then you can come back to compare your findings with whatever we're going to be doing 08:16.460 --> 08:17.390 together. 08:17.410 --> 08:17.750 All right. 08:17.780 --> 08:19.700 I hope you manage to find the solution. 08:20.030 --> 08:30.860 So first of all what we can do is remove all spaces from the results. 08:30.890 --> 08:32.270 Okay. 08:32.300 --> 08:43.360 And then we will split the outputs using the separator for slash and then after that your first item 08:43.480 --> 08:45.310 is going to be this one. 08:45.490 --> 08:46.630 Right. 08:46.720 --> 08:48.720 So let's go ahead and do this. 08:49.270 --> 08:50.950 So let's define 08:57.430 --> 08:58.280 a new variable. 08:58.300 --> 08:59.830 This call is file size 09:03.440 --> 09:05.200 and that's gonna be our command outputs 09:08.240 --> 09:08.980 which we're gonna change. 09:08.990 --> 09:11.810 So we're gonna replace all spaces tabs etc. 09:15.090 --> 09:16.080 with empty strings 09:22.640 --> 09:33.430 instead of thus we're going to have an empty string and then we are going to split the outputs using 09:33.490 --> 09:35.530 this separator. 09:35.530 --> 09:40.840 So just to make our code readable I'm going to explain here what we're doing. 09:40.990 --> 09:41.820 So remote. 09:41.860 --> 09:51.670 Remove all spaces tabs etc. and then we're gonna change the variable again so any let's instead 09:54.540 --> 09:55.440 so split 09:59.040 --> 10:03.900 file size using the forward slash separator 10:10.030 --> 10:11.850 so we're gonna use the split method. 10:14.090 --> 10:18.600 And we're gonna pass the separator that right. 10:18.660 --> 10:25.510 So once you get this let's see what's our variable file size looks like 10:37.690 --> 10:37.970 okay. 10:37.980 --> 10:42.700 So this is an example from the array we are getting so that's the size we are interested in. 10:42.690 --> 10:55.140 So all you need to do is get the first elements so the human size is the first item of the array. 10:55.380 --> 10:56.190 So let's give that 11:02.720 --> 11:12.220 and let's print it just to check. 11:12.370 --> 11:13.840 Here we go. 11:13.840 --> 11:18.020 So for this one we're guessing that's 1. 11:18.050 --> 11:19.900 That's another example. 11:19.920 --> 11:20.600 Okay. 11:20.870 --> 11:26.250 Perfect so it's printed twice because we had this twice. 11:26.270 --> 11:32.190 So if you try again I guess it's only once all right. 11:32.250 --> 11:37.450 So file size is going to be the first item inside your array. 11:37.530 --> 11:44.080 So now if you try this code you will get the correct size there for all for this. 11:44.130 --> 11:51.120 So let's try and you can see that we're getting the right size for fall is perfect. 11:51.120 --> 11:59.200 So now the next step is to convert that size to what bytes basically. 11:59.580 --> 12:09.900 So you will need to extract both the number and the units separately and depending on what you need 12:10.020 --> 12:10.640 you're gonna get. 12:10.650 --> 12:14.070 You can easily convert that's two bytes. 12:14.520 --> 12:25.180 So for example in this scenario here we're going to get one hundred eight and then the unit is K for 12:25.180 --> 12:26.560 kilobytes. 12:26.560 --> 12:34.590 So we will just multiply one hundred eight with one thousand to get the number of bytes. 12:35.110 --> 12:46.440 So it is go ahead and do this so we're going to define a new variable and call it the units 12:51.090 --> 12:54.980 or file size units. 12:55.330 --> 13:03.040 So to get the unit out of this string you can just go to the string replace everything 13:05.760 --> 13:12.090 thus is a digits or a dots and replace that with an empty string. 13:12.270 --> 13:19.060 So you will keep only the units that say it is go ahead undo this. 13:20.640 --> 13:22.350 So we're gonna go to our file size 13:25.650 --> 13:34.150 which is nice and then we will replace all those elements thus we don't need anymore. 13:34.160 --> 13:37.130 We can replace them with an empty string. 13:37.280 --> 13:41.960 So let's look now for the correct regular expression. 13:43.340 --> 13:47.090 So first of all you want to replace digits. 13:47.090 --> 13:47.320 Okay. 13:47.360 --> 13:52.380 So I did it is going to be backslash D also. 13:52.400 --> 13:54.320 You want to replace any dots. 13:55.790 --> 14:04.230 So to add something you just add a vertical bar like that and then you add the other elements you want 14:04.230 --> 14:08.120 to look for in our case we're looking for a dot. 14:08.130 --> 14:14.530 So we're gonna have a backslash and then eight dots backslash to escape. 14:14.760 --> 14:17.310 OK and then dots. 14:17.430 --> 14:19.040 So that's all we need to replace. 14:19.040 --> 14:21.720 So let's see what we get. 14:22.080 --> 14:25.110 If we can so not log the units 14:31.050 --> 14:31.740 so here we go. 14:31.740 --> 14:33.730 We've got K for that one. 14:34.050 --> 14:40.250 I've got cable one as well saying that same the all right. 14:40.260 --> 14:42.830 Perfect. 14:42.840 --> 14:45.150 So let's try with this one 14:48.220 --> 14:53.100 of course and therefore megabytes K and so on. 14:53.140 --> 15:03.070 So it is working so we're getting the right units now as we of course the units we need the number which 15:03.070 --> 15:05.660 will convert afterwords to bytes. 15:05.770 --> 15:06.410 So it is defined. 15:06.430 --> 15:10.810 Another variable I call its file size number 15:14.780 --> 15:18.610 so we're going to follow the same process that we did before. 15:18.680 --> 15:27.830 But instead of replacing dots and digits we will replace any characters between a and Z regardless of 15:29.840 --> 15:32.120 whether they are capital or small characters. 15:32.120 --> 15:34.910 So our search is going to be insensitive. 15:35.090 --> 15:39.960 So let's copy lots and change it. 15:40.330 --> 15:49.480 So we are replacing all characters between a and set and we're doing a case insensitive search. 15:49.480 --> 15:57.260 So that's going gonna be I and let's around a Z with brackets. 15:57.390 --> 16:03.070 And finally let's console log the results to make sure it works. 16:04.500 --> 16:09.080 And you can see here Matthew courts the correct number without the units. 16:09.090 --> 16:17.360 So that's the units and that's the number now you want to convert this number to bytes and that's what 16:17.360 --> 16:19.090 you're going to do in the next video.