I am doing some batch file name updates and am having trouble including folders. I have it currently set to target only specific file types, but I also want to include folders. Since folders don't have an extension I am unsure how to specify folders in the "-include" string. Any help would be greatly appreciated.
Here is what I am currently working with, but it ignores folders when I would like them included.
Get-ChildItem k:/toolbox/powershell -Include *.gif, *.jpg, *.png, *.xls,
*.xlsx, *.ppt, *.pptx, *.doc, *.docx, *.pdf -recurse | where {$_.name -match
"_"} | foreach {
$New=$_.name.Replace("_","-")
Rename-Item -path $_.Fullname -newname $New -passthru
}
(Get-Item '~\Desktop\*') | foreach { $FolderName = $_.name.Replace("_","-"); Rename-Item -path $_.fullname -newname $FolderName -passthru }
I would just add 2nd statement:
Get-ChildItem "k:/toolbox/powershell" -Recurse |where mode -eq d----- | where {$_.name -match "_"} | foreach {$New=$_.name.Replace("_","-")
Rename-Item -path $_.Fullname -newname $New -passthru }
If you run Get-ChildItem "k:/toolbox/powershell" you will receive a list of items where the "Mode" is "d-----" for directories, hence you can use that filter criteria for directories
Related
Hello and thanks in advance for the help. I am attempting to remove an extra extension that was added to documents in a library due to a security breach. When i manually remove the extension ".easy2lock" the file becomes available. However, when i try to do this programmatically via powershell it works, but not for folders with long paths. Does anyone know how to get around this in powershell?
I've tried tried updating my registry to allow long paths, however, i don't believe that has any affect on
here is the script and error i recieve:
[string]$DeleteBadExtension = ".easy2lock_read_me"
[string]$BadExtension = ".easy2lock"
Get-ChildItem -Filter "*$BadExtension" -Recurse | Rename-Item -NewName {$_.Name -replace $BadExtension, [string]::Empty}
Get-ChildItem *$DeleteBadExtension -Recurse | foreach { Remove-Item -Path $_.FullName }
I'll have to take your word for it that the files are usable after changing the name, that hasn't been my experience. To be able to access long paths in powershell you'll need to use the -LiteralPath parameter. Unfortunately the -Path parameter will be the default used when piping. -LiteralPath does take pipeline input by propertyname so you can either manipulate the object removing path and adding literal path like this
Get-ChildItem -Filter "*$BadExtension" -Recurse |
Select-Object Name,Basename,Extension,#{n='LiteralPath';e={"\\?\$($_.fullname)"}} |
Rename-Item -NewName {$_.Name -replace $BadExtension}
Or you can use a Foreach-Object or foreach loop and use the required syntax \\?\ for the path.
Get-ChildItem -Filter "*$BadExtension" -Recurse | Foreach-Object {
Rename-Item -LiteralPath "\\?\$($_.FullName)" -NewName ($_.Name -replace ‘$BadExtension’)
}
Edit
Since the basename is the desired filename (just without the unwanted extension) you can use the basename as the newname. All four examples have been tested and confirmed to work. If it's not working for you then there is something else going on.
Get-ChildItem -Filter *$BadExtension -Recurse | ForEach-Object {
Rename-Item -LiteralPath "\\?\$($_.FullName)" -NewName ($_.name -replace $BadExtension)
}
Get-ChildItem -Filter *$BadExtension -Recurse | ForEach-Object {
Rename-Item -LiteralPath "\\?\$($_.FullName)" -NewName $_.basename
}
Get-ChildItem -Filter "*$BadExtension" -Recurse |
Select-Object Name,#{n='LiteralPath';e={"\\?\$($_.fullname)"}} |
Rename-Item -NewName {$_.Name -replace $BadExtension}
Get-ChildItem -Filter "*$BadExtension" -Recurse |
Select-Object Basename,#{n='LiteralPath';e={"\\?\$($_.fullname)"}} |
Rename-Item -NewName {$_.baseName}
I want to get all files in subfolders, of the same root folder, that all contain the same string ("foo") in the name of the subfolder(s). Below gives me no error, and no output. I don't know what I'm missing.
Get-ChildItem $rootfolder | where {$_.Attributes -eq 'Directory' -and $_.BaseName -contains 'foo'}) | echo $file
Ultimately, I would like to not just echo their names, but move each file to a target folder.
Thank you.
Here is a solution that includes moving the child files of each folder to a new target folder:
$RootFolder = '.'
$TargetFolder = '.\Test'
Get-ChildItem $RootFolder | Where-Object {$_.PSIsContainer -and $_.BaseName -match 'foo'} |
ForEach-Object { Get-ChildItem $_.FullName |
ForEach-Object { Move-Item $_.FullName $TargetFolder -WhatIf } }
Remove -WhatIf when you are happy it's doing what it should be.
You might need to modify the Get-ChildItem $_.FullName part if you (for example) want to exclude sub-directories of the folders, or if you want to include child items in all subfolders of those paths, but not the folders themselves.
replace
Get-ChildItem $rootfolder | where {$_.Attributes -match 'Directory' -and $_.basename -Match 'foo'}) | echo $file
with
Get-ChildItem $rootfolder | where {($_.Attributes -eq 'Directory') -and ($_.basename -like '*foo*')} | Move-Item $targetPath
your request:
that all contain the same string ("foo")
you have to use the -like comparison operator. Also for exact match I would use -eq (case sensitive version is -ceq) instead of -match since its used for matching substrings and patterns.
workflow:
Gets all the files in directory, sending it through pipe to Where-Object cmdlet where you are filtering based on properties Attributes and Basename. When the filtering is done, its being sent to cmdlet Move-Item.
Adapt the first two vars to your environment.
$rootfolder = 'C:\Test'
$target = 'X:\path\to\whereever'
Get-ChildItem $rootfolder -Filter '*foo*' |
Where {$_.PSiscontainer} |
ForEach-Object {
"Processing folder: {0} " -f $_
Move $_\* -Destination $target
}
I have a piece of software which looks for a files named "report.txt". However, the text files aren't all named report.txt and I have hundreds of sub folders to go through.
Scenario:
J:\Logs
26-09-16\log.txt
27-09-16\report270916.txt
28-09-16\report902916.txt
I want to search through all the sub folders for the files *.txt in J:\logs and rename them to report.txt.
I tried this but it complained about the path:
Get-ChildItem * |
Where-Object { !$_.PSIsContainer } |
Rename-Item -NewName { $_.name -replace '_$.txt ','report.txt' }
Get-ChildItem * will get your current path, so in instead let's use the define the path you want Get-ChildItem -Path "J:\Logs" and add recurse because we want the files in all the subfolders.
Then let's add use the include and file parameter of Get-ChildItem rather than Where-Object
Then if we pipe that to ForEach, we can use the Rename-Item on each object, where the object to rename will be $_ and the NewName will be report.txt.
Get-ChildItem -Path "J:\Logs" -include "*.txt" -file -recurse | ForEach {Rename-Item -Path $_ -NewName "report.txt"}
We can trim this down a bit in one-liner fashion with a couple aliases and rely on position rather than listing each parameter
gci "J:\Logs" -include "*.txt" -file -recurse | % {ren $_ "report.txt"}
To replace certin word in all files in all sub folders
Get-ChildItem C:\Path -Recurse -Filter *OldWord* | Rename-Item -NewName { $_.name -replace 'OldWord', 'NewWord'} -verbose
I have to go through many levels of child folders and remove special characters that are invalid in SharePoint, mainly '#&'
I have scoured the internet trying different commands; rename-item/move-item, variations of the two, all to no avail. The closest i've gotten is using:
Get-ChildItem -Recurse | Rename-Item -NewName {$_.Name -replace'[!##&]','_'}
but i keep getting this error: Rename-item: Source and destination path must be different.
Could someone point me in the right direction?
Regards
That error only happens when you attempt to rename a directory to the same NewName as the current name, you can safely ignore it.
Add -ErrorAction SilentlyContinue to silently suppress the error message:
Get-ChildItem -Recurse | Rename-Item -NewName {$_.Name -replace'[!##&]','_'} -ErrorAction SilentlyContinue
You need to filter out the files that you're not planning to rename:
Get-ChildItem -Recurse |
Where-Object { $_.Name -match '[!##&]' } |
Rename-Item -NewName {$_.Name -replace '[!##&]','_'}
something like this may work
dir -Recurse -File | ? basename -Match '[!##&]' | % {
# if the file.txt already exists, rename it to file-1.txt and so on
$num = 1
$base = $_.basename -replace'[!##&]', '_'
$ext = $_.extension
$destdir = Split-Path $_.FullName
$newname = Join-Path $destdir "$base$ext"
while (Test-Path $newname) {
$newname = Join-Path $destdir "$base-$num$ext"
$num++
}
ren $_.fullname $newname
}
I have subfolders, and subsubfolders. In the subsubfolders, I want to find all subfolders without a file named PKA.dump. Can this be done in powershell?
The subfolders go from Angle1, Angle2, etc up to Angle24
The subsubfolders go from 1eV, 2eV, to 150eV.
I can find when they are less than a certain size:
Get-Childitem -path . -filter "PKA.dump" -recurse | where {$_.Length -le 500}
But what if they dont exist?
If you have just 2 levels of directories, don't recurse. Do something like this instead:
Get-ChildItem -Path . -Directory | Get-ChildItem -Directory | ? {
-not (Test-Path -LiteralPath (Join-Path $_.FullName 'PKA.dump'))
}
For a deeper folder structure this should be ok:
Get-ChildItem -Path C:\yourpath\ -recurse | where {$_.psiscontainer} | % {
if((Get-ChildItem -Path $_.FullName -File).name -notcontains "pka.dump"){ $_.FullName }
}