Essentially I have a few hundred folders that have trailing periods at the end of the folder names because of my company's cloud service Box.com.
We're trying to remove the periods using a powershell script but admittedly my powershell knowledge is in its beginner stages.
$string = Get-ChildItem -Recurse | ? {$_.PSIsContainer} | Select Name
$string2 = Trim($string)
$string.Length
$string3 = $string.TrimEnd(".")
dir | ? { $string } | % { mv $_ -Destination ($_.Name.$string3) }
So above I am running the first line from within the folder in question to get the PSIsContainer names, then trimming them of white spaces, and then attempting to trim the end where I have my $string3 variable of any trailing periods in the PSIsContainer names. It's throwing an error though, and I can't figure out why.
Method invocation failed because [System.Object[]] doesn't contain a method named 'TrimEnd'.
At line:1 char:27
+ $string3 = $string.TrimEnd <<<< (".")
+ CategoryInfo : InvalidOperation: (TrimEnd:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Any help would be greatly appreciated!
$string is an array of directory names, not a string. You can't TrimEnd() an array.
You'll probably need to create a foreach loop to do what you want with each directory.
$Directories = Get-ChildItem -Recurse | ? {$_.PSIsContainer}
foreach($Dir in $Directories)
{
Rename-Item $Dir Trim($Dir.Name)
Rename-Item $Dir $Dir.Name.TrimEnd(".")
}
Still another way, if you are looking for a one-liner and if regex is not your friend:
ls -Rec -Dir -Incl '*.' | % { $_.MoveTo($_.FullName.Trim().TrimEnd('.')) }
If the pattern is simple enough, you can let the -Include parameter of Get-ChildItem do the filtering for you.
Edit: The -Include parameter does only work in combination with -Recurse.
Another way:
$pattern = '^\s*(.*?)\.+\s*$'
Get-ChildItem -Recurse -Directory |
? { $_.Name -match $pattern } |
Rename-Item -NewName { $_.Name -replace $pattern, '$1' }
Related
So, i need to save a string that was replaced on files.
I'm doing something wrong, but i`m not be able to figure it out, what!
My code:
Get-ChildItem -Path C:\Users\user\Documents -Recurse -Include "*.txt" -File | Select-String -Pattern \b192\.168\.10\.2\b , \b192\.168\.10\.11\b -AllMatches -List | Foreach-Object { $_ -replace '\b192\.168\.10\.2\b', "DEV" -and $_ -replace '\b192\.168\.10\.11\b', "QUAL" | Set-Content $_}
And gives-me the following error:
Set-Content : Could not open the alternate data stream '1:192.168.10.11' of the file 'C:\Users\user\Documents\result.txt'.
At line:1 char:323
+ ... place '\b192\.168\.10\.11\b', "QUAL" | Set-Content $_}
+
+ CategoryInfo : ObjectNotFound: (C:\Users\paulo....ents\result.txt:String) [Set-Content], FileNotFoundException
+ FullyQualifiedErrorId : GetContentWriterFileNotFoundError,Microsoft.PowerShell.Commands.SetContentCommand
Set-Content : Could not open the alternate data stream '1:192.168.10.11' of the file
'C:\Users\user\Documents\test.txt'
At line:1 char:323 ... place '\b192\.168\.10\.11\b', "QUAL" | Set-Content $_}
CategoryInfo : ObjectNotFound: (C:\Users\user\test.txt:String) [Set-Content], FileNotFoundException
FullyQualifiedErrorId : GetContentWriterFileNotFoundError,Microsoft.PowerShell.Commands.SetContentCommand
Thanks for any Help!
Inside the ForEach-Object block, $_ will refer to the current match result as returned by Select-String - to get the file path, reference the Path property:
... | ForEach-Object { ... |Set-Content -LiteralPath $_.Path}
The -and operator is used inside if tests like if(this -and that).
You should change the double replace actions from
$_ -replace '\b192\.168\.10\.2\b', "DEV" -and $_ -replace '\b192\.168\.10\.11\b', "QUAL"
into
$_ -replace '\b192\.168\.10\.2\b', "DEV" -replace '\b192\.168\.10\.11\b', "QUAL"
Also, if I understand the question properly, you want to find all string replacements in the file, and to get all, you need to remove the -List switch from Select-String.
Next, as Mathias explains in his answer, you need to use the Path property from the current match to get the file FullName.
However, if you pipe this through to Set-Content straight away, you will get an exception because the file then is in use and you cannot write to the same file.
Below creates a new file in the same path, with _replacements appended to the filename
# use '-Include' instead of '-Filter' if you need more file extensions to filter on
Get-ChildItem -Path 'C:\Users\user\Documents' -Recurse -Filter "*.txt" -File |
Select-String -Pattern '\b192\.168\.10\.2\b', '\b192\.168\.10\.11\b' -AllMatches |
Foreach-Object {
$file = '{0}_replacements{1}' -f [System.IO.Path]::GetFileNameWithoutExtension($_.Path),
[System.IO.Path]::GetExtension($_.Path)
$target = Join-Path -Path ([System.IO.Path]::GetDirectoryName($_.Path)) -ChildPath $file
$_ -replace '\b192\.168\.10\.2\b', "DEV" -replace '\b192\.168\.10\.11\b', "QUAL" |
Add-Content -Path $target
}
This results in a file called 'C:\Users\user\Documents\test_replacements.txt'
C:\Users\user\Documents\test.txt:4:DEV
C:\Users\user\Documents\test.txt:7:QUAL
The original file 'C:\Users\user\Documents\test.txt' will not be altered.
I have the following script for renaming a bunch of files in a directory, adding the name of the directory to the start of them:
$s = "Y:\Teknisk Arkiv\Likeretter 111-Å1\E2_Kretsskjema\02_Likeretter styring\3AJJ000302-222"
Get-ChildItem -Path $s -Exclude $_.Directory.Name* | rename-item -NewName { $_.Directory.Name + '_' + $_.Name }
Before running the script the files in the folder looks something like this
after like this
As you can see it does more or less what I want, except that -exclude $_.DirectoryName* doesn't prevent files which already have the foldername as a prefix from being renamed. What am I doing wrong here?
$_ in a pipeline is only defined inside a script block used in a non-initial pipeline segment, where it refers to the input object at hand, so in your Get-ChildItem command it is effectively undefined.
Even if $_.Directory.Name did have a value, $_.Directory.Name* wouldn't work as expected, because it would be passed as 2 arguments (you'd have to use "$($_.Directory.Name)*" or ($_.Directory.Name + '*').
You instead want to extract the directory name from the $s input path, which you can do with Split-Path -Leaf, and then append '*'.
In order for -Exclude to be effective, the input path must end in \*, because -Include and -Exclude filters - perhaps surprisingly - operate on the leaf component of the -Path argument, not on the child paths (unless -Recurse is also specified).
To put it all together:
Get-Item -Path $s\* -Exclude ((Split-Path -Leaf $s) + '*') |
Rename-Item -NewName { $_.Directory.Name + '_' + $_.Name }
I've switched to Get-Item, since \* is now being used to enumerate the children, but Get-ChildItem would work too.
The $_ is only valid when it is used on the right-side of a pipeline meaning when you have a collection of items and "pipe" them through the "$_" would represent the current item.
Since the directory name you want excluded is static you can just hardcode it and use as your exclude filter.
$s = "Y:\Teknisk Arkiv\Likeretter 111-Å1\E2_Kretsskjema\02_Likeretter styring\3AJJ000302-222"
$exclude_filter = "3AJJ000302-222*"
Get-ChildItem -Path $s -Exclude $exclude_filter | rename-item -NewName { $_.Directory.Name + '_' + $_.Name }
Also try to use "-whatif" with rename-item so you know what will happen before it happens.
$_ represents the currently processed item, what requires a ForEach-Object or a scriptblock inside a pipe, not present at the begin of your command.
Solution make the path a FileInfoObject and use -Exclude
$s = Get-Item "Y:\Teknisk Arkiv\Likeretter 111-Å1\E2_Kretsskjema\02_Likeretter styring\3AJJ000302-222"
Get-ChildItem -Path $s -Exclude "$($s.Name)*"|Rename-Item -NewName {$_.Directory.Name+'_'+$_.Name}
solution use a Where-Object to filter files already starting with the directory name
Get-ChildItem -Path $s | Where-Object {$_.Directory.Name -notlike "$($_.Name)*"} |
Rename-Item -NewName { $_.Directory.Name + '_' + $_.Name }
Solution use the RegEx based -replace operator to prepend the directory name and use a negative lookahead assertion to exclude files which already have it.
Get-ChildItem -Path $s |
Rename-Item -NewName {$x=$_.Directory.Name;$_.Name -replace "^(?!$x)",$x}
I have a large number of files (>4 million) and need to rename and move them to another folder in small steps.
I am a total beginner in powershell, but I already managed to move them in small packages of 100 files (the powershell script is executed as scheduled task).
But so far I failed with renaming the files. In each file, there are two strings that need to be replaced.
The following codes works fine, except for the renaming part (line 12 and 13):
#Get 'n' number of files
$FileLimit = 100
$PickupDirectory = Get-ChildItem -Path "\\server\path$\ERROR\subfolder\"
$DropDirectory = "\\server\path$\destination\"
$Counter = 0
foreach ($file in $PickupDirectory)
{
if ($Counter -ne $FileLimit)
{
$file | Rename-Item -NewName {$_.name -replace '999999','367'}
$file | Rename-Item -NewName {$_.name -replace 'oldname','newname'}
$Destination = $DropDirectory+$file.Name
Move-Item $file.FullName -destination $Destination
$Counter++
}
}
exit
What is the correct way to rename those files?
Thank you so much for your help!
Best wishes
Philipp
Edit: Sorry, here's the Error Log :
Rename-Item : Cannot rename because item at 'Microsoft.PowerShell.Core\FileSystem::\\server\path$\ERROR\subfolder\1566392#5990762$20180116^999999_2018_01_16_oldname_1566392_Kägi.pdf' does not exist.
At C:\Scripts\mv_Verordnung_für_Physiotherapie.ps1:12 char:28
+ ... pDirectory | Rename-Item -NewName {$_.name -replace '^999999','^367'}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
Edit2: Updated the code with tipps from comments. Error still the same.
You should do the limiting (if necessary at all) earlier with the Get-ChildItem =>
$PickupDirectory = Get-ChildItem -Path "\\server\path$\ERROR\subfolder\" | Select -First $FileLimit
Instead of using the currently iterated item ($file) you use the whole array $PickupDirectory
You can't apply the 2. replace to an already changed value.
instead of rename and move do it in one step.
#Get 'n' number of files
$FileLimit = 100
$PickupDirectory = Get-ChildItem -Path "\\server\path$\ERROR\subfolder\" | Select -First $FileLimit
$DropDirectory = "\\server\path$\destination\"
foreach ($file in $PickupDirectory){
$Destination = Join-Path $DropDirectory ($file.Name -replace '^999999','^367' `
-replace 'oldname','newname')
$file | Move-Item -Destination $Destination
}
You're calling Rename-Item on the $PickupDirectory collection. Instead call it on the $file variable you are using in the foreach loop:
$file | Rename-Item -NewName { $_.name -replace '^999999', '^367' }
$file | Rename-Item -NewName { $_.name -replace 'oldname', 'newname' }
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
}
I have a google drive folder that occasionally gets out of sync. Google (or someone) will append (1) to the directory/file names. Then it will remove the original directory and I'll have a bunch of folders and files named "xxx (1)".
I've been trying to write a powershell script that will crawl the directory tree and rename the folders/files by simply removing the " (1)" portion. I realize that this may result in some collisions, but I was hoping to get most of them replaced using a script. I'm not that concerned about the directory structure, I'll restore if needed, but it's just kind of a nuisance.
I've tried several powershell scripts, and the closest I've come so far is this...
This will return the correct NEW folder names
Get-ChildItem -Path "* (1)" -Recurse | select { $_.FullName.Replace(" (1)", "")}
So I tried this...
Get-ChildItem -Path "* (1)" -Recurse | Replace-Item -Path $_.FullName -Destination $_.FullName.Replace(" (1)", "") -WhatIf
I get the error "You cannot call a method on a null-valued expression."
You cannot call a method on a null-valued expression.
At line:1 char:1
+ Get-ChildItem -Path "* (1)" -Recurse | rename-item $_.FullName $_.Fu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Try surrounding the last half in a foreach-object, using Rename-Item instead of Replace-Item, etc etc:
Get-ChildItem -Path "* (1)" -Recurse | ForEach-Object {Rename-Item -Path $_.Fullname -NewName $($_.Name.Replace(" (1)", "")) -WhatIf}
$_ is only a variable inside a scriptblock. With:
.. | select { $_.FullName.Replace(" (1)", "")}
You can get-help select-object and see the first positional parameter is -Property, so this is short for
.. | Select-Object -Property { $_.FullName.Replace(" (1)", "") }
And now you can see the scriptblock gets the files in via the pipeline, accesses them as $_ and calculates a new Property for the output, and the $_ is used once for each file. When you try to use the same technique in your second line:
.. | Replace-Item -Path $_.FullName -Destination $_.FullName.Replace(" (1)", "") -WhatIf
The $_ is just free-floating, you can't use it like that, outside a {} scriptblock it doesn't mean anything. And you're piping the input files in through the pipeline and also trying to specify them with -Path, which is doubling up - clashing and redundant. And you're using Replace instead of Rename.
So in the other answer, nferrell uses ForEach to create a scriptblock. And pipes the files into ForEach and then specifies their name to the -Path of Rename-Item.
Which works, but it's wordy and roundabout. Why take the filenames out of the pipeline and use ForEach to shuffle them round to the other end of the cmdlet, only to put them straight back in?
Get-ChildItem .. | Rename-Item -NewName { $_.Name.Replace(" (1)", "") }
Files go in by the pipeline, NewName is calculated. No loop, no doubling up of input, no