
Java tutorial for beginners
1000 FOLLOWERS
A blog on core java, android, data structures,web services and various frameworks like struts 2,spring,spring mvc,hibernate and various java design patterns
Java tutorial for beginners
4d ago
Using Add-Content Cmdlet
Use the Add-Content cmdlet to write binary files in PowerShell.
$binaryData = [Byte[]] (0x01, 0x02, 0x03, 0x04)
Add-Content -Path ".\test.bin" -Value $binaryData -Encoding Byte
This script created a new file named test.bin in the current working directory and wrote the binary data to the file. The script will overwrite the existing file with the new data if the file already exists. First, we stored the array of bytes in $binaryData containing different values.
Then, we used the Add-Content cmdlet to write provided binary data to the specified .bin file; in our case ..read more
Java tutorial for beginners
4d ago
Using Percent (%) in Different Contexts
Typically the % character is for mod functionality. But in PowerShell, it’s an alias for ForEach-Objectand can be also used as Modulus Operator and an Assignment Operator (%=). Let’s learn each of them below.
Use Percent(%) as an alias of the ForEach-Object cmdlet in PowerShell
We can use % sign instead of ForEach-Object cmdlet as % is alias for ForEach-Object cmdlet.
Get-Alias -Definition ForEach-Object
CommandType Name
Alias % -> ForEach-Object
Alias foreach -> ForEach-Object
The above command will list all of the alia ..read more
Java tutorial for beginners
4d ago
Using Get-Content Cmdlet
The Get-Content cmdlet can be used in the following ways to get the number of lines in the specified .csv file:
Get-Content cmdlet with .Length property
Get-Content cmdlet with .Count property
Get-Content cmdlet with .ReadCount property
Get-Content cmdlet with Measure-Object cmdlet and its -Line parameter
Before moving towards the ways mentioned above, let’s look at the content of our countries.csv; you can use your .csv file. Although content is optional here, it helps to know the number of lines we have.
Content of countries.csv File:
Country
Capital
India ..read more
Java tutorial for beginners
4d ago
Using the Import-Csv Cmdlet
To read a CSV file in PowerShell, use the Import-Csv cmdlet.
$csvFile = "d:\data.csv"
Import-Csv -Path $csvFile
Country Age Salary Purchased
------- --- ------ ---------
France 44 72000 No
Spain 27 48000 Yes
Germany 30 54000 No
Spain 38 61000 No
Germany 40 Yes
France 35 58000 Yes
Spain 52000 No
France 48 79000 Yes
Germany 50 83000 No
France 37 67000 Yes
PowerShell provides several cmdlets that allow us to work with CSV files:
Import-Csv
ConvertFrom-Csv
Export-Csv
ConvertTo-Csv
These cmdlets allow us to easily read, write ..read more
Java tutorial for beginners
4d ago
Using the Get-Content Cmdlet
We can use the Get-Content cmdlet to read the contents of a file into a string.
Use the Get-Content Cmdlet to Read a Single File
To read a single file into a string in PowerShell:
Use the Get-Content cmdlet with the -Raw parameter to read the file’s contents into a string.
Use the .GetType() method to check the data type of the result.
$file = "D:\abc.txt"
$content = Get-Content -Path $file -Raw
($content.GetType()).Name
String
The Get-Content cmdlet in PowerShell reads the contents of a file or other input and output objects, such as the output of another cm ..read more
Java tutorial for beginners
4d ago
TL;DR
Use the substring() method to get String between two characters in JavaScript.
function getSubstring(str, start, end) {
char1 = str.indexOf(start) + 1;
char2 = str.lastIndexOf(end);
return str.substring(char1, char2);
}
let originalString = "Hello,World!";
let substring = getSubstring(originalString, ',', '!');
console.log(substring);
World
Here, we got String between , and ! in above example.
Using substring() Method
Use the substring() method to extract a substring that is between two specific characters from the given string in JavaScript.
let str = "This is a sample st ..read more
Java tutorial for beginners
4d ago
Using PSObject.Properties.Remove() Method
To delete a property from an object in PowerShell,
Use the New-Object cmdlet to create an object.
Use the PSObject.Properties.Remove() method to remove a specific property from an object by providing the property’s name as a string parameter.
$person_obj = New-Object -Typename PSCustomObject -Property @{
Name = 'Bob'
Age = '25'
Citizenship = 'U.S'
}
Write-Host "Before Removal: $person_obj"
$person_obj.PSObject.Properties.Remove('Age')
Write-Host "After Removal: $person_obj"
Before Removal: @{Citizenship=U.S; Name=Bob; Age=25}
After Rem ..read more
Java tutorial for beginners
4d ago
Using the if-else Statement
We can use the if-else statement to create a file as follows:
Use the New-Item Cmdlet
To create a file if it does not exist in PowerShell:
Use the Test-Path cmdlet with the if condition to check if the file exists.
Use the New-Item cmdlet to create a new file if it does not exist.
Print an error message in the else condition.
$file = "D:\test.txt"
if(!(Test-Path -Path $file)){
New-Item -ItemType File -Path $file
Write-Output "File '$file' created successfully"
} else {
Write-Output "File '$file' already exists"
}
Directory: D:\
Mode ..read more
Java tutorial for beginners
4d ago
Using Built-in Cmdlets and Methods
We can use built-in cmdlets and methods to check if a directory exists.
Use the Test-Path Cmdlet
To check if a folder exists, use the Test-Path cmdlet.
$path = "C:\Windows"
if(test-Path -Path $path){
Write-Host "Folder exists."
}else{
Write-Host "Folder does not exist."
}
Folder exists.
PowerShell’s Test-Path cmdlet verifies that a path exists, regardless of whether it is a file or folder. This cmdlet helps write scripts that need verification that specific files and folders exist before continuing with the rest of the script. If the path exists ..read more
Java tutorial for beginners
1M ago
Count Files in Folders in PowerShell
We can use the Measure-Object cmdlet in different ways to meet our project requirements. However, before moving forward, it is essential to know how many files and folders we have on the different paths that we will use in this article.
E:\Test contains three folders (FolderA, FolderB and FolderC) and four text files (File1.txt, File2.txt, File3.txt, and File4.txt).
FolderA contains four text files (File1.txt, File2.txt, File3.txt, and File4.txt)
FolderB contains two text files (File1.txt, and File2.txt)
FolderC has one text file (File1.txt)
Let’s practic ..read more