Home > Software design >  How to set a text content of a specific column as a variable using Batch or PowerShell?
How to set a text content of a specific column as a variable using Batch or PowerShell?

Time:10-23

For example, I have a file/file output with the following content:

2022-10-16 14:33 1,860,477 Mausi-~1.JPG Mausi-wife.JPG

There are spaces between these five blocks in between, i.e. between date and time and number and file name and another file name.

I would now like to set only the fourth column of this as a variable. Is this possible?

CodePudding user response:

Split the string on spaces, update the desired value in the resulting array, and then stitch back together with -join:

$row = '2022-10-16 14:33 1,860,477 Mausi-~1.JPG Mausi-wife.JPG'

# split into individual field values
$fields = $row.Split(' ')

# update/overwrite the 4th item in the resulting string array
$fields[3] = "New value"

# stitch row back together with `-join`
$row = $fields -join ' '

CodePudding user response:

OK, thank you. Thats helpful.

Another queation: Is it possible to use PowerShell to search a variable value to see if a specific character occurs anywhere in that word?

For example: Abc123~xyz

Here I would like to scan to see if the tilde character ~ occurs anywhere. Is the?

  • Related