This is a follow up question of: PowerShell concatenate output of Get-ChildItem
This code works fine:
Get-ChildItem -Path "D:\Wim\TM1\TI processes" -Filter "*.vue" -Recurse -File |
Where-Object { $_.BaseName -match '^[0-9]+$' } |
ForEach-Object { ($_.FullName -split '\\')[-2,-1] -join '\' } |
Out-File D:\wim.txt
But I would need to restrict the search folder to only certain folders, basically, this filter: D:\Wim\TM1\TI processes\\*}vues (so all subfolders ending in }vues).
If I add that wildcard condition I get no result. Without the restriction, I get the correct result. Is this possible please?
The idea is to get rid of the 3rd line in the first output (which was a copy/paste by me) and also to minimize the number of folders to look at.
You can nest two Get-ChildItem calls:
An outer Get-ChildItem -Directory -Recurse call to filter directories of interest first,
an inner Get-ChildItem -File call that, for each directory found, examines and processes the files of interest.
Get-ChildItem -Path "D:\Wim\TM1\TI processes" -Filter "*}vues" -Recurse -Directory |
ForEach-Object {
Get-ChildItem -LiteralPath $_.FullName -Filter "*.vue" -File |
Where-Object { $_.BaseName -match '^[0-9]+$' } |
ForEach-Object { ($_.FullName -split '\\')[-2,-1] -join '\' }
} | Out-File D:\wim.txt
Note: The assumption is that all *.vue files of interest are located directly in each *}vues folder.
As for what you tried:
Given that you're limiting items being enumerated to files (-File), your directory-name wildcard pattern *}vues never gets to match any directory names and, in the absence of files matching that pattern, returns nothing.
Generally, with -Recurse it is conceptually cleaner not to append the wildcard pattern directly to the -Path argument, so as to better signal that the pattern will be matched in every directory in the subtree.
In your case you would have noticed your attempt to filter doubly, given that you're also using the -Filter parameter.
Related
Hello I would like to know is there a way to replace multiple filenames from parent folder?
I was able to rename multiple files using command below, but I have to access each folder first to rename multiple files.
dir .\* -include ('*.mp4','*.srt') | Rename-Item -NewName { $_.Name -replace '\[','' -replace '\]','' }
I was trying to replace dir .\* to dir .\**\* to select from parent folder but didn't work.
What am I missing?
You can do that by adding the -Recurse switch to Get-ChildItem (dir is alias to Get-ChildItem).
When searching for files that contain square brackets, you need to use the -LiteralPath parameter.
Also, be aware that there is a snag if you pipe the results from that directly to Rename-Item..
When doing so, Get-ChildItem may pick up the already processed files again, wasting time of course, so to prevent that you can either do:
$rootFolder = 'X:\WhereTheFilesAre'
(Get-ChildItem -LiteralPath $rootFolder -Include '*.mp4','*.srt' -File -Recurse) |
Rename-Item -NewName { $_.Name -replace '\[|]' }
or
$rootFolder = 'X:\WhereTheFilesAre'
$files = Get-ChildItem -LiteralPath $rootFolder -Include '*.mp4','*.srt' -File -Recurse
foreach ($file in $files) {
$file | Rename-Item -NewName { $file.Name -replace '\[|]' }
}
In additional to marsze's concise answer and realizing this isn't really a performance question:
-Include is much slower than -Filter because it filters after retrieving from the file system. Whereas, -Filter has the file system do the heavy lifting. In essence -Filter is a nuanced version of moving filtering operations left in the command/pipeline to improve performance. However, -Filter doesn't take multiple values! That said, you may still be able to exploit this characteristic using a loop, something like:
'*.mp4','*.srt' |
ForEach-Object{
Get-ChildItem .\ -Filter $_ -File
} |
Rename-Item -NewName { $_.Name -replace '\[|\]' }
-File works even though you are using -Recurse and may carry a modest performance improvement. I also shortened the -replace to 1 operation which can only help further.
What you probably want is the -Recurse switch:
dir "the folder" -Recurse -Include ('*.mp4','*.srt')
Note that this will recurse all levels of subdirectories.
I just found out I could use dir . to select all files (with specific file types), including files in subdirectories.
dir . -Recurse -Include ('*.srt', '*.mp4') | Rename-Item -NewName { $_.Name -replace '\[|\]','' }
I have multiple files stored in folders and subfolders. Almost all of them contain _v1 at the end of the BaseName. I tried the following but I'm getting an error.
Get-ChildItem -Recurse * -Filter "/(_v1)/" |
Rename-Item -NewName { $_.Name -replace '/(_v1)/','' } -WhatIf
Error:
Get-ChildItem : Second path fragment must not be a drive or UNC name.
Try this :
Get-ChildItem "C:\test\ " -recurse -Filter "*_V1*" | % { Rename-Item $_ -NewName $($_.Name -replace "_V1","" ) }
PowerShell isn't Perl. Forward slashes don't indicate a regular expression, they're just literal forward slashes, so neither your filter expression nor your search string will match the intended files. Also, the parentheses (i.e. the capturing group) serve no purpose, so you should remove them.
Use a wildcard pattern as the filter string for Get-ChildItem and apply the regular expression replacement to the basename to avoid unintentional replacements of _v1 elsewhere in the file names.
Get-ChildItem -Filter '*_v1.*' -Recurse |
Rename-Item -NewName { ($_.BaseName -replace '_v1$') + $_.Extension }
If you're running at least PowerShell v3 you can add the parameter -File to Get-ChildItem so that it won't return directories.
I have a PowerShell script that recursively deletes all files and folders, but excludes certain folders as they should not be deleted.
It works 100%, but my problem is performance. I need this to run a lot faster.
Any ideas on how to make this faster?
Write-Host "Purging $InstallationDirectorySite - Deleting files..."
$FolderExlusions = (
"App_Data",
"Logs",
"TEMP",
"ExamineIndexes",
"DistCache",
"GitPathProviderRepository"
)
[regex] $files_regex = "Logs|ExamineIndexes|DistCache*|GitPathProviderRepository*"
if(Test-Path $InstallationDirectorySite) {
Get-ChildItem -Path $InstallationDirectorySite -Recurse -Exclude $FolderExlusions |
Where-Object {$_.FullName -notmatch $files_regex} |
Remove-Item -Recurse
}
else {
Write-Output "$InstallationDirectorySite doesn't exist"
}
You are in fact filtering the excluded folders twice.
The first time using the -Exclude parameter and the second time using a Regex -match.
However, the Exclude parameter takes a string array, not a single string with keywords separated by a comma and a newline as you get from the 'here-string'.
See Get-ChildItem
Also, the regex you use is wrong, because the asteriks * in regex is a quantifier, not a wildcard character.
I suggest you filter once using either the -Exclude parameter like this (here the asteriks is a Wildcard):
$FolderExlusions = "App_Data","Logs","TEMP","ExamineIndexes","DistCache*","GitPathProviderRepository*"
Get-ChildItem -Path $InstallationDirectorySite -Recurse -Exclude $FolderExlusions | Remove-Item -Recurse -WhatIf
Or use only the regex method in a Where-Object clause like this:
$FolderExlusions = "^(App_Data|Logs|TEMP|ExamineIndexes|DistCache.*|GitPathProviderRepository.*)"
Get-ChildItem -Path $InstallationDirectorySite -Recurse | Where-Object { $_.Name -notmatch $FolderExlusions } | Remove-Item -Recurse -WhatIf
Remove the -WhatIf if you are satisfied with the results.
Hope that helps
I have seen duplicate questions, but nothing worked for me.
I want to exclude looking into certain paths, while performing an operation. This doesn't work:
$archive = ("C:\Windows\Temp*","C:\Windows\winsxs*","C:\Windows\system*")
$final = Get-ChildItem C:\ -Include *.dll -Exclude $archive -Recurse | ? {
$_.PSIsContainer -and $_.FullName -notlike "\\obj\\?"
} | Where-Object {
$_.VersionInfo.LegalCopyright -notmatch 'Microsoft'
}
Please correct me where am I wrong? Do I have to use a foreach loop to iterate through items in $archive and exclude them individually? Pipeline or any other short command is there in this case?
First and foremost, a statement Get-ChildItem -Include *.dll will return only file objects (unless you have a folder named <something>.dll, which would be rather uncommon), so if you filter the output for directory objects ($_.PSIsContainer) you will obviously come up with an empty result. Since the rest of your code suggests that you want files anyway just remove the $_.PSIsContainer clause from your filter.
Also, the -Exclude parameter applies to the name of the items. You can't use it to exclude (partial) paths. If you want files from the given directories omitted from the result you should exclude them in your Where-Object filter with a regular expression match like this:
$_.FullName -notmatch '^C:\\windows\\(system|temp|winsxs)\\'
And finally, wildcard matches (-like, -notlike) require a * wildcard at beginning and/or end of the expression if you want to match a partial string:
PS> 'abcde' -like 'a*e'
True
PS> 'abcde' -like 'c*e'
False
PS> 'abcde' -like '*c*e'
True
Without the leading/trailing * the expression is automatically anchored at beginning/end of the string.
However, your pattern doesn't look like a wildcard expression in the first place. It looks more like a regular expression to me (to match paths containing \obj\ or ending with \obj). For that you'd also use the -notmatch operator:
$_.FullName -notmatch '\\obj\\'
From a perfomance perspective wildcard matches are more efficient, though, so it'd be better to use an expression like this:
$_.FullName -notlike '*\obj\*'
Making the trailing backslash optional is pointless, because Get-ChildItem returns a list of *.dll files, so none of the full paths will end with \obj.
Something like this should do what you want, assuming that I interpreted your code correctly:
$final = Get-ChildItem 'C:\' -Include '*.dll' -Recurse | Where-Object {
$_.FullName -notmatch '^C:\\windows\\(system|temp|winsxs)\\' -and
$_.FullName -notlike '*\obj\*' -and
$_.VersionInfo.LegalCopyright.Contains('Microsoft')
}
I have no problem adding sequential prefixes to filenames. The following works great on the top directory in question.
$path="E:\path\newtest1"
$count=4000
Get-ChildItem $path -recurse | Where-Object {!$_.PSIsContainer -and $_.Name -NotMatch '^\d{4}\s+'} | ForEach -Process {Rename-Item $_ -NewName ("$count " + $_.name -f $count++) -whatif}
BUT if there are files in subfolders within the top directory, these are all completely missed. Whatif reports that for any deeper file it "does not exist".
I have tried the following, based on looking at some pages on other recursion problems, but as you can probably guess I have no clue what it is doing. Whatif shows that it does at least pickup and rename all the files. But the following does it too much and makes multiple copies of each file with each number:
$path="E:\path\newtest1"
$count=4000
Get-ChildItem -recurse | ForEach-Object { Get-ChildItem $path | Rename-item -NewName ("$count " + $_.Basename -f $count++) -whatif}
Really keen to get some guidance on how to get the first of these two snippets to work to find all files in all subdirectories and rename them with sequential number prepended.
Try it like so:
Get-ChildItem $path -recurse -file | Where Name -NotMatch '^\d{4}\s+' |
Rename-Item -NewName {"{0} $($_.name)" -f $count++} -whatif
When you supply $_ as an argument (not a pipeline object), that gets assigned to the Path parameter which is of type string. PowerShell tries to convert that FileInfo object to a string but unfortunately the "ToString()" representation of files in nested folders is just the filename and not the full path. You can see this by executing:
Get-ChildItem $path -recurse -file | Where Name -NotMatch '^\d{4}\s+' | ForEach {"$_"}
The solution is either to A) pipe the object into Rename-Item or B) use the FullName property e.g. Rename-Item -LiteralPath $_.FullName ....