I need to check that all files in folders with the naming ".policy" is ether 'azureDeploy.parameters.json' or 'azureDeploy.json'
if i only want it to check one folder i just give the full path as here:
$azureDeployFiles = #('azureDeploy.parameters.json', 'azureDeploy.json')
(Get-ChildItem -path ..\..\fes\.policy -file -Recurse).name | Should -BeIn $azureDeployFiles
and that works. But i have multiple folders called something like '.\..\..\.policy' and i would like to check if all the folders with that naming only contains files with the naming 'azureDeploy.parameters.json' or 'azureDeploy.json'
You could specify 'only files in folders named X' with Where-Object on the Directory property:
$azureDeployFiles = #('azureDeploy.parameters.json', 'azureDeploy.json')
Get-ChildItem -path ..\..\ -Recurse -File |
Where Directory -Like "*\.policy" |
# example checking file names with -In
Foreach { $_.Name -In $azureDeployFiles }
Continuing from my comment. . .with a little help from the pipeline, we can use Where-Object to filter for directories with that specific name:
$azureDeployFiles = #('azureDeploy.parameters.json', 'azureDeploy.json')
Get-ChildItem -Path '.\MyPath' -Include $azureDeployFiles -Recurse |
Where-Object Directory -Like '*\.Policy'
Related
I try to export files of specific named Folders:
Get-ChildItem -Path 'C:\Test' -Name -Recurse -File > C:\Test\Test.txt
I get a list like:
content.csv
Test.txt
Folder 1\INTERESTED_FOLDER\a - Kopie.txt
Folder 1\INTERESTED_FOLDER\a.txt
Folder 1\Neuer Ordner\ttt.txt
Folder 1\Neuer Ordner - Kopie\ttt.txt
Folder 2\INTERESTED_FOLDER\b - Kopie.txt
Folder 2\INTERESTED_FOLDER\b.txt
Folder 2\Neuer Ordner\ttt.txt
Folder 2\Neuer Ordner - Kopie\ttt.txt
But what i want is:
Folder 1\INTERESTED_FOLDER\a - Kopie.txt
Folder 1\INTERESTED_FOLDER\a.txt
Folder 2\INTERESTED_FOLDER\b - Kopie.txt
Folder 2\INTERESTED_FOLDER\b.txt
I tried with -Filter "INTERESTED" etc. but then i only get
C:\Test\Folder 1\INTERESTED_FOLDER
C:\Test\Folder 2\INTERESTED_FOLDER
What i do wrong?
If I read your question correctly, you want the FullNames of the files (so their names including the path).
If that is the case, remove the -Name switch and filter on the DirectoryName property like:
(Get-ChildItem -Path 'C:\Test' -Recurse -File |
Where-Object { $_.DirectoryName -match 'INTERESTED_FOLDER' }).FullName | # also matches 'MY_INTERESTED_FOLDER_123'
Set-Content -Path 'C:\Test\Test.txt'
Alternatives for the Where-Object clause:
# also matches 'MY_INTERESTED_FOLDER_123'
Where-Object { $_.DirectoryName -like '*INTERESTED_FOLDER*' }
or if you are looking for a precise match on the complete folder name
# does NOT match 'MY_INTERESTED_FOLDER_123'
Where-Object { $_.DirectoryName -split '[/\\]' -contains 'INTERESTED_FOLDER' }
You can perform a wildcard search using the -Filter parameter:
Get-ChildItem -Path 'C:\Test' -Name -Recurse -File -Filter *INTERESTED_FOLDER* > C:\Test\Test.txt
If for example, you were interested in finding the files in INTERESTED_FOLDER but also only the files that are .txt you could do:
-Filter *INTERESTED_FOLDER*.txt
Using the -Name parameter affects your capabilities because you are returning strings instead of FileInfoObjects. You may then use Where-Object to capture everything.
Get-ChildItem -Path 'C:\Test' -Name -Recurse -File |
Where {$_ -match '\\INTERESTED_FOLDER\\'} |
Set-Content c:\test\test.txt
Note that the matching above assumes INTERESTED_FOLDER is not a direct descendent of your path. If that is a possibility, then your match expression needs to be updated to \\?INTERESTED_FOLDER\\.
I am attempting to delete all directories, sub-directories, and the files contained in them based on a filter that specifies the required directory/sub-directory name.
For example, if I have c:\Test\A\B.doc, c:\Test\B\A\C.doc, and c:\Test\B\A.doc and my filter specifies all directories named 'A', I would expect the remaining folders and files to be c:\Test, c:\Test\B and c:\Test\B\A.doc respectively.
I am trying to do this in PowerShell and am not familiar with it.
The following 2 examples will delete all of the files that match my specified filter, but the files that match the filter as well.
$source = "C:\Powershell_Test" #location of directory to search
$strings = #("A")
cd ($source);
Get-ChildItem -Include ($strings) -Recurse -Force | Remove-Item -Force –Recurse
and
Remove-Item -Path C:\Powershell_Test -Filter A
I would use something like this:
$source = 'C:\root\folder'
$names = #('A')
Get-ChildItem $source -Recurse -Force |
Where-Object { $_.PSIsContainer -and $names -contains $_.Name } |
Sort-Object FullName -Descending |
Remove-Item -Recurse -Force
The Where-Object clause restricts the output from Get-ChildItem to just folders whose names are present in the array $names. Sorting the remaining items by their full name in descending order ensures that child folders get deleted before their parent. That way you avoid errors from attempting to delete a folder that had already been deleted by a prior recursive delete operation.
If you have PowerShell v3 or newer you can do all filtering directly with Get-ChildItem:
Get-ChildItem $source -Directory -Include $names -Recurse -Force |
Sort-Object FullName -Descending |
Remove-Item -Recurse -Force
I don't think you can do it quite that simply. This gets the list of directories, and breaks the path into its constituent parts, and verifies whether the filter matches one of those parts. If so, it removes the whole path.
It adds a little caution to handle if it already deleted a directory because of nesting (the test-path) and the -Confirm helps ensure that if there's a bug here you have a chance to verify the behavior.
$source = "C:\Powershell_Test" #location of directory to search
$filter = "A"
Get-Childitem -Directory -Recurse $source |
Where-Object { $_.FullName.Split([IO.Path]::DirectorySeparatorChar).Contains($filter) } |
ForEach-Object { $_.FullName; if (Test-Path $_) { Remove-Item $_ -Recurse -Force -Confirm } }
I'm trying to match file name with folder name before move them to an other directory.
For example, my script need to match if "Test.txt" match with a folder named "Test" and move them to a different directory.
Is it possible with the cmdlets Get-ChildItem ? I didn't find any examples :(
Thanks a lot.
PowerShell 3+
Gets all files recursively from the current directory whose names (without extension) matches its directory's name:
Get-ChildItem -Path . -File -Recurse |
Where-Object { $_.BaseName -eq $_.Directory.Name }
PowerShell 1, 2
There is no -File switch until PowerShell 3, so you have to filter out directories with an extra Where-Object.
Get-ChildItem -Path . -Recurse |
Where-Object { -not $_.PsIsContainer } |
Where-Object { $_.BaseName -eq $_.Directory.Name }
Once you've got all the files that match their parent directory name, you should be able to move them. I'm not sure what your logic is for the destination directory structure.
For starters you can use the Directory property of Get-ChildItem
So lets say you are looking for a file test.txt but only if it is in the directory Scripts
Get-ChildItem *.txt -recurse | Where-Object{($_.Name -eq "test.txt") -and ($_.Directory -like "*\scripts")} | Move-Item $_.Directory "C:\NewFolder"
The Where clause will look for a file called text.txt as long as its in a folder called c:\somepath\scripts. So this will not match c:\anotherpath\test.txt. When a match is found Move the located files directory to a new location.
Note I am not sure if the logic will hold if multiple file matches are found. If it fails then we could assign all the matches to a variable and then process the all the unique values.
$foundDirectories = Get-ChildItem *.txt -recurse | Where-Object{($_.Name -eq "test.txt") -and ($_.Directory -like "*\scripts")} | Select-Object -ExpandProperty Directory -Unique
$foundDirectories | ForEach-Object{Move-Item $_ "C:\newfolder"}
I am trying to copy a bunch of directories. I have text files which have a similar name and I need to match the directories to those text files to determine which directories to copy.
This is what I have done and I can't figure out how to fix it. I am going around in circles.
$destination = "..\..\$args\Images\"
$txtfiles = Get-ChildItem $destination -Include *.txt
$source = "..\..\..\Images\" | ?{ $_.PSIsContainer } | Where-Object { $_.Name -Like "*$txtfiles*" } | Copy-Item $destination
An example of a txt file: 1e03655b-0aac-48b2-82f3-75942084af7a.txt
and folder: name.1e03655b-0aac-48b2-82f3-75942084af7a
So I need to find the folders that match the txt files and copy the folder to the txt file directory.
Thanks
You want to expand the BaseName properties of those text files (to cut off the .txt part) and have an array of strings such as:
1e03655b-0aac-48b2-82f3-75942084af7a
1e03655b-0aac-48b2-82f3-75942084af7b
1e03655b-0aac-48b2-82f3-75942084af8g
1e03655b-0aac-48b2-82f3-75942084afba
Then your $source = line needs to pull a directory listing and not just pipe a string down the pipeline, and then I would suggest matching the directory names with a regex match, and checking to see if that match is -in $txtfiles.
$destination = "..\..\$args\Images\"
$txtfiles = Get-ChildItem $destination -Filter *.txt | Select -ExpandProperty BaseName
$source = "..\..\..\Images\"
GCI $source -Directory | ?{ $_.Name -match "(\w{8}-\w{4}-\w{4}-\w{4}-\w{12})$" } | %{If($Matches[1] -in $txtfiles){Copy-Item $_.FullName $destination}}
Is it possible to define an array of filenames (all files in different folders) and then in a loop delete them all, or do something else?
Actually I need to create a few symbolic links using mklink to one file, putting those links in a different folders, replacing the old links if there was any.
Deleting an array of filenames is simple:
Remove-Item foo.txt,c:\temp\bar.txt,baz\baz.txt
Or via a variable:
$files = 'foo.txt','c:\temp\bar.txt','baz\baz.txt'
Remove-Item $files
And then based on all files in different folders:
$folders = 'C:\temp','C:\users\joe\foo'
Get-ChildItem $folders -r | Where {!$_.PSIsContainer} | Remove-Item -WhatIf
Remove the -WhatIf to do the actual removal.
If you want to delete a file with a specific name you could use the -Filter parameter on Get-ChildItem. This would be the best performing approach:
$folders = 'C:\temp','C:\users\joe\foo'
Get-ChildItem $folders -r -filter foo.bak | Remove-Item -WhatIf
If the name requires more sophisticated matching then you can use a regex in a Where scriptblock e.g.:
$folders = 'C:\temp','C:\users\joe\foo'
Get-ChildItem $folders -r | Where {$_.Name -match 'f\d+.bak$'} |
Remove-Item -WhatIf
something like this should work:
can't test right now, sorry
$filenames = #('filename1.txt', 'filename2.txt', 'filename3.txt')
foreach($file in $filenames)
{
#GCI recursive to find all instances of this filename
$filesToDelete = Get-ChildItem -R | where {$_.Name -eq $file}
foreach($f in $filesToDelete)
{
#delete the file that matches, etc. here
# just using Write-Host to echo out the name for now
Write-Host $f.Name
}
}
As with most powershell, you can really compress this, but wanted to extend for explanation.
You could extend this to match your needs. For example if you needed all files that contain the word "delete", you could do gci | where {$_.Name -like "$file"}