The List() method of the File class returns a String array containing the names of all the files and directories in the path represented by the current (File) object.In order to search for a file you need to compare the name of each file in the directory to the name of the required file using the equals() method.ExampleLive Demoimport java.io.File; import java.util.Arrays; import java.util.Scanner; public class Example { public static void main(String[] argv) throws Exception { System.out.println("Enter the directory path: "); Scanner sc = new Scanner(System.in); String pathStr = sc.next(); ... Read More
The String[] list(FilenameFilter filter) method of a File class returns a String array containing the names of all the files and directories in the path represented by the current (File) object. But the retuned array contains the filenames which are filtered based on the specified filter. The FilenameFilter is an interface in Java with a single method.accept(File dir, String name)To get the file names based on extensions implement this interface as such and pass its object to the above specified list() method of the file class.Assume we have a folder named ExampleDirectory in the directory D with 7 files and 2 directories ... Read More
To multiply only one column with a number, we can simply use the multiplication operator * but need to replace the original column with the new values. For example, if we have a data frame called df that contains three columns x1, x2, and x3, and we want to multiply the second column x2 with 2 then it can be done as df$x2
Since Java 7 the File.02s class was introduced this contains (static) methods that operate on files, directories, or other types of files.The createDirectory() method of the Files class accepts the path of the required directory and creates a new directory.ExampleFollowing Java example reads the path and name of the directory to be created, from the user, and creates it.Live Demoimport java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Scanner; public class Test { public static void main(String args[]) throws IOException { System.out.println("Enter the path to create a directory: "); Scanner sc = new Scanner(System.in); ... Read More
To check if the PowerShell string contains a specific word, we can use the string method Contains(). For example, ExamplePS C:\> $str = 'TestNZ01LT' PS C:\> $str.Contains('NZ') TrueNow the funny thing is, even if the PowerShell is case-Insensitive, the above command is not. We need to provide the exact substring. For example, the below output will be false.ExamplePS C:\> $str.Contains('Nz') FalseTo overcome this problem, we can either provide the same search name in the method or we need to use the lowercase or uppercase method if you don’t want the search case-sensitive.PS C:\> $str = 'TestNZ01LT' PS C:\> ($str.ToLower()).Contains(('Nz').ToLower()) True ... Read More
To convert any command output to the hashtable in PowerShell, we can first convert the output to the JSON format using the ConvertTo-JSON command, and applying the ConvertFrom-JSON command from the first output will produce an output to the Hashtable.ExampleGet-ServiceThe above command will give the output in the array format.OutputStatus Name DisplayName ------ ---- ----------- Stopped WwanSvc WWAN AutoConfig Stopped XblAuthManager Xbox Live Auth Manager Stopped XblGameSave Xbox Live Game Save Stopped XboxGipSvc Xbox Accessory Management Service Stopped XboxNetApiSvc Xbox Live ... Read More
When we write Invoke-Command in PowerShell, sometimes we get the RunSpaceID property in PowerShell. This is because we use Select-Object (alias: Select) command inside the scriptblock. For example,ExampleInvoke-Command -ComputerName LabMachine2k12 -ScriptBlock{Get-Process PowerShell | Select Name, CPU, WorkingSet}OutputName : powershell CPU : 9.1572587 WorkingSet : 127700992 PSComputerName : LabMachine2k12 RunspaceId : c690f205-06d4-4cc4-be29-5302725eadf1To avoid getting the RunSpaceID property in the output, use the Select command output the scriptblock. For example,ExampleInvoke-Command -ComputerName LabMachine2k12 -ScriptBlock{Get-Process PowerShell} | Select Name, CPU, WorkingSetOutputName CPU WorkingSet ---- --- ---------- powershell 9.1572587 127700992
When we are working with Invoke-Command, we get the PSComputerName extra field in the output table. You can remove it by using -HideComputerName parameter. For example, the Below command gives the output with the PSComputerName property.ExampleInvoke-Command -ComputerName LabMachine2k12 -ScriptBlock{Get-Service Winrm} | ft -AutoSizeOutputStatus Name DisplayName PSComputerName ------ ---- ----------- -------------- Running Winrm Windows Remote Management (WS-Management) LabMachine2k12To hide the PSComputerName property,ExampleInvoke-Command -ComputerName LabMachine2k12 -ScriptBlock{Get-Service Winrm} -HideComputerNameOutputStatus Name DisplayName ------ ---- ----------- Running Winrm Windows Remote Management (WS-Management)
To find the device driver version using PowerShell, we need to use the class win32_PnpSignedDriver of the WMI object. For example, ExampleGet-WmiObject win32_PnpSignedDriverOr if you are using PowerShell core (PowerShell 6.0 or later), you can use the CIM Instance command. For example, Get-CimInstance win32_PnpSignedDriverTo filter out the Drivers against their versions, use the below command to filter.Examplegwmi win32_PnpSignedDriver | Select Description, DriverVersionOutputACPI x64-based PC 6.2.9200.16384 UMBus Root Bus Enumerator 6.2.9200.16384 WAN Miniport (IPv6) 6.2.9200.16384 Composite Bus Enumerator ... Read More
To get the particular windows certificate expiry date from the particular store, we first need the full path of that certificate along with a thumbprint. If the thumbprint is not known to you, we can use the friendly name.With the thumbprint, Get-ChildItem Cert:\LocalMachine\root\0563B8630D62D75 | fl *When you run the above command, it will get all the details of the certificate having thumbprint 0563B8630D62D75.There you can see there are two fields listed, NotAfter and NotBefore which shows the expiry and start dates respectively. To filter them out, ExampleGet-ChildItem Cert:\LocalMachine\root\0563B8630D62D75 | Select FriendlyName, NotAfter, NotBeforeOutputFriendlyName NotAfter NotBefore ------------ -------- --------- ... Read More