Hi Guys I have the following code which search a folder and return any string containing the value down:
filter MultiSelect-String( [string[]]$Patterns ) {
# Check the current item against all patterns.
foreach( $Pattern in $Patterns ) {
# If one of the patterns does not match, skip the item.
$matched = #($_ | Select-String -Pattern $Pattern)
if( -not $matched ) {
return
}
}
# If all patterns matched, pass the item through.
$_
}
Get-ChildItem -recurse | MultiSelect-String 'report','Product1'
So if the code gets the file it displays it like that:
Directory: C:\Users\sarvesh.nundram\Desktop\PMI\RPD_Extract_XML\SQL_tobemigrated2\GROUP1
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 11/21/2013 1:07 PM 133279 Acapulco
What if I don't want these info:
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 11/21/2013 1:07 PM
this?
Get-ChildItem -recurse |
MultiSelect-String 'report','Product1' |
select fullname # or select -expa fullname
Related
In PowerShell, I can get a nice list of files in descending sorted order using a filter:
$tt = gci -Path \\Munis2\musys_read\export_test\* -Include "ARLMA_*.csv" | sort LastWriteTime -Descending
PS H:\WindowsPowerShell\Scripts\ProductionScripts\Munis> $tt
Directory: \\Munis2\musys_read\export_test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 03/04/2022 3:09 AM 25545520 ARLMA_20220304030027.csv
.
.
.
Then, I can get just the name of the file for the purposes of transferring that file to an FTP site.
PS H:\WindowsPowerShell\Scripts\ProductionScripts\Munis> $tt[0].Name
ARLMA_20220304030027.csv
How can I parse $tt[0].LastWriteTime
PS H:\WindowsPowerShell\Scripts\ProductionScripts\Munis> $tt[0].LastWriteTime
Friday, March 4, 2022 3:09:14 AM
into something that looks like yymmddhhmmss, or is there a way to get the binary time of the file the last time it was accessed?
The ToString() method can be used to format the date into a string. Are you sure that a two digit year is appropriate?
$DateResult = (Get-ChildItem -Path \\Munis2\musys_read\export_test\* -Include "ARLMA_*.csv" |
Sort-Object -Property LastWriteTime -Descending |
Select-Object -First 1).LastWriteTime.ToString('yyMMddHHmmss')
I have below files which i am reading using a foreach loop.
$GetGeneratedFiles = Get-ChildItem -Path "C:\Script\LF*.csv" -recurse | % { $_.FullName }
C:\Script\LF_Batch_1.csv
C:\Script\LF_Batch_10.csv
C:\Script\LF_Batch_11.csv
C:\Script\LF_Batch_12.csv
C:\Script\LF_Batch_13.csv
C:\Script\LF_Batch_14.csv
C:\Script\LF_Batch_15.csv
C:\Script\LF_Batch_16.csv
C:\Script\LF_Batch_17.csv
C:\Script\LF_Batch_18.csv
C:\Script\LF_Batch_19.csv
C:\Script\LF_Batch_2.csv
C:\Script\LF_Batch_20.csv
C:\Script\LF_Batch_21.csv etc...upto LF_Batch_.96.csv
Problem is it is reading the files like above not 1,2,3...and so on.
Please need idea how to read in ordered way
Solved using below approach
$GetGeneratedFiles = Get-ChildItem -Path "C:\Script\LF*.csv" -recurse | % { $_.FullName }
$ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) }
$GetGeneratedFiles = $GetGeneratedFiles | Sort-Object $ToNatural
Thanks #vonPryz for the reference.
Another way. It's funny how I just did another answer similar to this. A numeric sort on the names.
echo hi | set-content (1,2,10,20 | % tostring LF_Batch_0\.csv)
dir | sort {[void]($_ -match '\d+'); [int]$matches.0}
Directory: C:\Users\js\foo
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 9/17/2020 9:14 AM 4 LF_Batch_1.csv
-a--- 9/17/2020 9:14 AM 4 LF_Batch_2.csv
-a--- 9/17/2020 9:14 AM 4 LF_Batch_10.csv
-a--- 9/17/2020 9:14 AM 4 LF_Batch_20.csv
I will start off by saying I am new to powershell...
I can get all the directories in a specified directory but I need to only get the directories that their names are numeric. Example:
1 - include
2 - include
3 - include
10 - include
LastFailed - exclude
I also will need to order them in sequential order for later processing.
Here is what I have so far:
$Dirs = Get-ChildItem -Path ..\..\..\builds -Attributes D
foreach($D in $Dirs)
{
Write-Host $D.Name
}
To get a list of just the folders that have numeric names, you can do this:
$Path = '..\..\..\builds'
$Dirs = Get-ChildItem -Path $Path -Attributes D |
Where-Object { $_.Name -match '^\d+$' } |
Sort-Object
$Dirs
This code above uses the Where-Object clause to filter out only the folders that have a numeric-only name.
It does this by using the -match operator and the regular expression ^\d+$ where:
`^` --> start at the beginning of the line
`\d` --> look for numeric values (0-9)
`+` --> there must be 1 or more numeric values present
`$` --> the end of the string
Instead of \d it could also have been written using [0-9]
The code ends with Sort-Object because the OP wants the list of folders sorted.
because we do not tell what property to sort on here, Powershell defaults to the Name.
The above will produce a list like:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 18-8-2018 16:13 1
d----- 18-8-2018 16:13 10
d----- 18-8-2018 16:13 2
d----- 18-8-2018 16:13 5
As you can see, the list is sorted by property Name, but.. the numbers were treated as strings, so '10' follows '1'.
What we most likely want to do here is to have the Sort-Object treat the names as real Numbers, not strings.
To fix that we add the Select-Object cmdlet in order to create our own objects with properties we need.
We can get standard properties of the DirInfo object that is coming through the pipeline, AND it is possible to create a new calculated property.
Here I add the calculated property called SortIndex that is simply the numeric value converted from the Name, which we will then use for the numeric sort:
$Dirs = Get-ChildItem -Path $Path -Attributes D |
Where-Object { $_.Name -match '^\d+$' } |
Select-Object -Property FullName, Name, LastWriteTime, #{Name = 'SortIndex'; Expression = {[int]$_.Name} } |
Sort-Object -Property SortIndex
$Dirs
Now it produces an array of objects that when printed to console looks like this:
FullName Name LastWriteTime SortIndex
-------- ---- ------------- ---------
D:\Temp\1 1 18-8-2018 16:13:22 1
D:\Temp\2 2 18-8-2018 16:13:25 2
D:\Temp\5 5 18-8-2018 16:13:28 5
D:\Temp\10 10 18-8-2018 16:13:31 10
Hope this helps.
You can utilize Where-Object to filter your collection:
$dirs = Get-ChildItem -Path ..\..\..\builds -Directory |
Where-Object -Property Name -Match '\d'
This operation uses regex to match the digit class.
If you meant you only want the directory names that start with a digit, you can use an anchor:
'^\d'
and lastly, you can sort them with Sort-Object:
$dirs = Get-ChildItem -Path ..\..\..\builds -Directory |
Where-Object -Property Name -Match '^\d' |
Sort-Object -Property Name
You can further enhance this sort by being explicit about the starting digit:
-Property {[int]($_.Name -split '[^\d]')[0]}
In this context, we're splitting on the first non-digit character and capturing the first fragment that was split off (which should be the number represented as a string, so we cast to an integer).
I want to bulk rename the files in my folder, and all of them have the format of FilenameYeara\b.pdf, for example, TestData2001a.pdf, File2015b.pdf. I want to rename all of them to something like [Yeara\b]Filename, such as [2001a]TestData. The problem is that I don't know how can I split my filename into two parts (actually three if we count the extension, .pdf part), such that I put that second part as the first part of the file name.
Get-ChildItem | Rename-Item {$_.name -replace ‘current’, ’old’ }
How can I achieve this?
This does the regex match "anything, four digits, one character, .pdf" and replaces it with those items in the new ordering.
PS D:\t> gci | ren -newname { $_ -replace '(.*)(\d{4})(.)\.pdf', '[$2$3]$1.pdf' }
Directory: D:\t
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 13/05/2016 02:54 0 File2015b.pdf
-a--- 13/05/2016 02:53 0 TestData2001a.pdf
becomes
Directory: D:\t
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 13/05/2016 02:53 0 [2001a]TestData.pdf
-a--- 13/05/2016 02:54 0 [2015b]File.pdf
(Maybe try it with -Whatif before running for real)
This should get you started
$Matches.Clear()
Get-Item | % {
$_.BaseName -match "(\D+)([0-9]{4}[ab])"
Rename-Item -Path $_.FullName -NewName "$($Matches[2])$($Matches[1])$($_.Extension)"
}
If I have this:
Get-ChildItem -Path $BACKUP_REG_PATH >> $TOT_LOG_FILE
I will get a fine list in my log file like this:
Directory: C:\WS\BACKUP\xxxx-Reg
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2016-05-17 11:04 494018 xxxxxx_REGISTRY_EVENTLOG__2016-05-17__11_04_38.reg
-a--- 2016-05-17 11:08 494018 xxxxxx_REGISTRY_EVENTLOG__2016-05-17__11_08_59.reg
-a--- 2016-05-17 11:10 494018 xxxxx_REGISTRY_EVENTLOG__2016-05-17__11_10_31.reg
I want to do this for NLog instead but I don't know how to get a nice list as above.
If I do this:
$regtxt=Get-ChildItem -Path $BACKUP_REG_PATH
$LOGGER.Trace("$regtxt");
I only get a long list on the same row with the Name column.
Any ideas how to solve this?
I don't know NLog but the Trace method probably output the trace in a single line. You could iterate over each item using the Foreach-Object cmdlet and write a trace:
Get-ChildItem -Path $BACKUP_REG_PATH | Foreach-Object {
$LOGGER.Trace($_);
}
Note: This will not output the name column, you may have to trace this yourself.
To solve this, you could pipe the output to the Out-String cmdlet which will give you a single string. You then have to split the string by [System.Environment]::NewLine to get an array to iterate over it:
((Get-ChildItem | select -first 4 | Out-String) -split [System.Environment]::NewLine) |
ForEach-Object {
$LOGGER.Trace($_);
}