I started with a powershell script that extracts filenames from a folder on a specific date and extract them to a new csv file. All I want is to trim the characters after the 5th character in each line. I started with this:
Get-ChildItem U:\data\*.* |Where{$_.LastWriteTime -gt (Get-
Date).AddDays(-1)} | Foreach-Object {$_.BaseName} > U:\result.csv
My result was:
I want to trim the characters from _ and after for each line. I want the result to be:
Assign that in a variable and use split method to split with underscore. Once split is done, you will have the values stored in array. So, index of zero will give your result.
This should do your work:
$values= Get-ChildItem U:\data\*.* |Where{$_.LastWriteTime -gt (Get-date).AddDays(-1)}
Foreach ($value in $values)
{
$value.BaseName.Split('_')[0] >> U:\result.csv
}
just do it
Get-ChildItem "U:\data\" -file |Where {$_.LastWriteTime -gt (Get-Date).AddDays(-1) -and $_.BaseName -like '*_*' } |
Foreach-Object {$_.BaseName.Split('_')[0]} > U:\result.csv
#short version
gci "U:\data\" -file |? {$_.LastWriteTime -gt (Get-Date).AddDays(-1) -and $_.BaseName -like '*_*' } |
% {$_.BaseName.Split('_')[0]} > U:\result.csv
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.