How to give extensionless files an extension in SubFolders - powershell

I have a parent folder with 100s of subfolders. In those there are random files that do not have any extension applied to them.
I'm trying to use PS to apply a .txt extension to any file found that just doesn't have an extension.
Things I've attempted:
Get-ChildItem G:\Those -Filter {!($_.Extension)} -Recurse |
Rename-Item -NewName {$_.DirectoryName + '.txt'}
Get-ChildItem G:\Those (gci -File -Recurse | ?{!($_.Extension)}) |
Rename-Item -NewName {$_.Directory.Name + '.txt'}
This one works if I have a file extension but I don't know how to convert it to only find files without an extension.
Get-ChildItem G:\Those -Filter *.ext -Recurse |
Rename-Item -NewName {$_.Directory.Name + '.txt'}
I'm getting this error:
Rename-Item : Cannot rename the specified target, because it represents a path or
device name.
At line:1 char:63
+ ... {-not $_.Extension} | Rename-Item -NewName {$_.DirectoryName+'.txt'}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Rename-Item], PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RenameItemCommand

I figured it out in a roundabout way, but if someone could provide a one-liner for this I would love you.
{-not $_.Extension} got me those files but the rename wouldn't work until I changed $_.DirectoryName+ to $_.FileName+ with the -File added before -Recurse.
This actually renamed the files that had no extension to just ".txt"
Name that they all had the same name I used my original command to rename anything .txt to $_.DirectoryName+'.txt', which did the trick.

Related

Renaming all text files in a directory

I altered some code for powershell:
Get-ChildItem -Filter *.txt | ForEach-Object { # Loop over files of interest
$newName = (Get-Content $_.FullName -Head 1)[-1] # Extract 1st line
$_ | Rename-Item -NewName $newName # Rename input file
}
It is supposed to take each text file in a directory, and rename it to the first line of the file.
Rename-Item : The path is not of a legal form.
At line:3 char:8
+ $_ | Rename-Item -NewName $newName # Rename input file
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Rename-Item], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.RenameItemCommand
But it gives me that error.
Use (Get-Content $_.FullName -First 1) instead of (Get-Content $_.FullName -Head 1)[-1]
-First has been introduced in PowerShell 3.0.

How do I rename each .jpg file name with consecutive numbers in PowerShell

I'm completely new to PowerShell, or any shell for that matter. I'm trying to figure out a way to rename 109 photos that are marked IMG_3571 to IMG_3679. I want to number them consecutively starting at 236. I've tried a few things and this is where I am at right now:
Get-ChildItem "C:\Files to Transfer\test"*.jpg | ForEach-Object -begin {$count=236} -process {rename-item -Path "C:\Files to Transfer\test" -NewName "$count"}
I get this error message 108 times:
At line:1 char:95
+ ... } -process {rename-item -Path "C:\Files to Transfer\test" -NewName "$ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
Also the file named "test" (where all the photos are contained) gets changed to 236...
Edit: I would like to have the files without "IMG" in the name. Only the numbers.
Thanks everyone! Here is what did it:
Get-ChildItem "C:\Files to Transfer\test\*.jpg" | ForEach-Object -begin {$count=236} -process {rename-item -Path $_.fullname -NewName "$count.jpg";$count++}
You are close.
You need $_ in the rename. $_ is the pipeline variable that Get-ChildItem is feeding.
Your output file needs IMG- as a prefix
And you need to increment count.
Try this:
Get-ChildItem "C:\Files to Transfer\test\*.jpg" | ForEach-Object -begin {$count=236} -process {rename-item -Path $_.fullname -NewName "$count.jpg";$count++}
It is very useful to add -WhatIf to things you are trying so that you can see what will happen without actually doing it.

Recursively Search Dirs & SubDirs And Numerically Rename Files

I am attempting to recursively scan a directory and rename all .jpg and .jpeg files in the dirs (and sub dirs) to a numeric naming convention.
I have this syntax
get-childitem -Recurse -path C:\Users\jsimpson\Desktop\Test123 | where {($_.extension -eq '.jpg') -or ($_.extension -eq '.jpeg') | %{Rename-Item $_ -NewName (‘MyFile{0}.txt’ -f $nr++)}
However - this gives me an error of
Missing closing '}' in statement block or type definition.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndCurlyBrace
I am sure this is something mundane on my end that I am overlooking - but what would be the proper syntax to numerically rename all files?
EDIT
Current filename is P1870426.jpeg I want to rename it to 1.jpeg
The files are all an import from a digital camera and Since the files have garbage names - I am basically wanting a way to import them into a program and have the files remain in the same order.
As the error message says, there's a } missing to close Where :
Get-ChildItem -Recurse -Path 'C:\Users\jsimpson\Desktop\Test123' | Where-Object {$_.Extension -match 'jpg|jpeg'} | ForEach-Object {
$newFile = "{0}$($_.Extension)" -f $nr++
Rename-Item $_.FullName -NewName $newFile -Force
}

difficulty renaming batch of files with PowerShell without losing extension

I am trying to use PowerShell to rename a folder containing thousands of images. I want to add both a prefix and a suffix, but this is proving more difficult than I bargained for. I don't understand PowerShell at all... and have dug around for help, and ended up with mixed results.
The best I've found was the example here: Renaming Files with PowerShell
Which yields ...
Dir -recurse | Sort {$.FullName.Length} -Desc | Rename-Item {$.Name -replace ' ','_'}
Now, I'm not wanting to replace anything, so I tried to just
Dir -recurse | Sort {$.FullName.Length} -Desc | Rename-Item { "pre-" + $.Name + "-suff" }
That gave me an error though, and I'm not quite sure how to interpret it;
Rename-Item : A positional parameter cannot be found that accepts argument ' "pre-" + $_.Name + "-suff" '.
At line:1 char:50
+ ... _.FullName.Length} -Desc | Rename-Item { "pre-" + $_.Name + "-suff" }
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Rename-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.RenameItemCommand
I quite literally have no idea what this is trying to say. I've dug around deeper, and found this tutorial: http://blogs.technet.com/b/heyscriptingguy/archive/2013/11/22/use-powershell-to-rename-files-in-bulk.aspx
And it has some slightly different commands, so I tried to plug that in with...
Get-ChildItem -Filter "current" -Recurse | Rename-Item -NewName { "pre-" + $_.name + "-suffix" }
And I get no error, but nothing seems to happen, either. This is proving to be very frustrating, as I keep searching for answers but every page I turn up is nebulous at best. Is there a simpler way to accomplish this? I'm wanting to append the prefix and suffix without affecting the file extension.
I did find something a bit closer to what I needed at: http://social.technet.microsoft.com/wiki/contents/articles/25144.powershell-bulk-renaming-file-names.aspx
And it suggested this script,
Get-ChildItem | Where-Object { $.Extension -eq ".jpg" -or $.Extension -eq ".png"} | rename-item -newname {"CL - " + $_.Name}
Which I changed to ...
Get-ChildItem | Where-Object { $.Extension -eq ".jpg" -or $.Extension -eq ".png"} | rename-item -newname {"prefix-" + $_.Name + "-suffix" }
And that at least did something to the files, but now the suffix is appended after the extension, which is obviously not what I'm going for.
you are almost there :)
Get-ChildItem -Include *.jpg,*.png -Recurse |
Rename-Item -NewName { 'Prefix' + $_.BaseName + 'Suffix' + $_.Extension } -WhatIf
Notice the -whatif at the end...this switch simulates command execution...Run the code first see the whatif output and if you are satisfied with the results then run the same command above without the -whatif to execute.
the $_.name includes the filename with the extension which is why you are getting the filenames with the suffix appended to the extension.
Get-Childitem | fl * will show you what properties you can work with.

PowerShell complaining about path, But path is valid

In the following code, when $client = XRS1
if (Test-Path C:\dbbackups\cm_$client-*.full.bak){
Rename-Item -path C:\dbbackups\cm_$client-*.bak -newname cm_$client.bak
Write-Host "New file found, renamed to: cm_$client.bak"
The Test-Path statement can find C:\dbbackups\cm_xrs1-2013414.full.full.bak but -path in the Rename-Item can't.
The error I get is
Rename-Item : Cannot process argument because the value of argument "path" is not valid. Change the value of the "path" argument and run the operation again.
At C:\Users\Aaron\Documents\0000 - PowerShell DB Update Utility\UpdateCMDatabases.ps1:167 char:1
+ Rename-Item -path C:\dbbackups\cm_$client-*.bak -newname cm_$client.bak
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Rename-Item], PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RenameItemCommand
For those who need only a one line command, please note that this is a powershell only error and that this command works just fine in the good old command prompt.
The full form of the powershell command is
ren -Path [something with a wildcard] -NewName [something else]
The error relates to the value of the Path parameter. It accepts wildcards in the path but it must resolve to a single file [1]. To use wildcards with powershell, you'll need to pipe them one by one to the rename item command. Here is an example to rename txt files to log [2]:
get-childItem *.txt | rename-item -newname { $_.name -replace '\.txt','.log' }
If Rename-Item does not like a wildcard, then do not give it one
Convert-Path C:\dbbackups\cm_$client-*.full.bak | % {
if (Test-Path $_) {
Rename-Item $_ cm_$client.bak
}
}