PowerShell - Loop through files and rename
up vote
1
down vote
favorite
newbie here. I am trying to write a PowerShell script to:
- loop through all files in directory
- List item
Get all .pdf files ONLY
Rename them-the file names are long - over 30 chars
-They contain 2 numbers which I need to extract
-Example:
Cumulative Update 11 for Microsoft Dynamics NAV 2018 (Build 25480).pdf ->
RESULT : = 18CU11.pdf
I tried examples from bunch of sites and I can't seem to even loop successfully.
Either get an error - that path doesn't exist or that can't rename files as somehow loop gets a filepath and that I can't rename
Get-ChildItem "C:Users******DesktopPowerShell Practice" -Filter *.pdf | #create list of files
ForEach-Object{
$oldname = $_.FullName;
$newname = $_.FullName.Remove(0,17);
#$newname = $_.FullName.Insert(0,"CU")
Rename-Item $oldname $newname;
$oldname;
$newname; #for testing
}
That's just latest attempt, but any other ways of doing it will be fine - as long as it does the job.
powershell scripting
add a comment |
up vote
1
down vote
favorite
newbie here. I am trying to write a PowerShell script to:
- loop through all files in directory
- List item
Get all .pdf files ONLY
Rename them-the file names are long - over 30 chars
-They contain 2 numbers which I need to extract
-Example:
Cumulative Update 11 for Microsoft Dynamics NAV 2018 (Build 25480).pdf ->
RESULT : = 18CU11.pdf
I tried examples from bunch of sites and I can't seem to even loop successfully.
Either get an error - that path doesn't exist or that can't rename files as somehow loop gets a filepath and that I can't rename
Get-ChildItem "C:Users******DesktopPowerShell Practice" -Filter *.pdf | #create list of files
ForEach-Object{
$oldname = $_.FullName;
$newname = $_.FullName.Remove(0,17);
#$newname = $_.FullName.Insert(0,"CU")
Rename-Item $oldname $newname;
$oldname;
$newname; #for testing
}
That's just latest attempt, but any other ways of doing it will be fine - as long as it does the job.
powershell scripting
2
How do your original filenames look exactly?
– TobyU
Nov 13 at 8:39
4
If it says the path doesn't exist, then probably .. the path doesn't exist? You should include the actual output from your attempts!
– marsze
Nov 13 at 8:53
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
newbie here. I am trying to write a PowerShell script to:
- loop through all files in directory
- List item
Get all .pdf files ONLY
Rename them-the file names are long - over 30 chars
-They contain 2 numbers which I need to extract
-Example:
Cumulative Update 11 for Microsoft Dynamics NAV 2018 (Build 25480).pdf ->
RESULT : = 18CU11.pdf
I tried examples from bunch of sites and I can't seem to even loop successfully.
Either get an error - that path doesn't exist or that can't rename files as somehow loop gets a filepath and that I can't rename
Get-ChildItem "C:Users******DesktopPowerShell Practice" -Filter *.pdf | #create list of files
ForEach-Object{
$oldname = $_.FullName;
$newname = $_.FullName.Remove(0,17);
#$newname = $_.FullName.Insert(0,"CU")
Rename-Item $oldname $newname;
$oldname;
$newname; #for testing
}
That's just latest attempt, but any other ways of doing it will be fine - as long as it does the job.
powershell scripting
newbie here. I am trying to write a PowerShell script to:
- loop through all files in directory
- List item
Get all .pdf files ONLY
Rename them-the file names are long - over 30 chars
-They contain 2 numbers which I need to extract
-Example:
Cumulative Update 11 for Microsoft Dynamics NAV 2018 (Build 25480).pdf ->
RESULT : = 18CU11.pdf
I tried examples from bunch of sites and I can't seem to even loop successfully.
Either get an error - that path doesn't exist or that can't rename files as somehow loop gets a filepath and that I can't rename
Get-ChildItem "C:Users******DesktopPowerShell Practice" -Filter *.pdf | #create list of files
ForEach-Object{
$oldname = $_.FullName;
$newname = $_.FullName.Remove(0,17);
#$newname = $_.FullName.Insert(0,"CU")
Rename-Item $oldname $newname;
$oldname;
$newname; #for testing
}
That's just latest attempt, but any other ways of doing it will be fine - as long as it does the job.
powershell scripting
powershell scripting
edited Nov 16 at 12:44
asked Nov 13 at 8:34
Arthur
135
135
2
How do your original filenames look exactly?
– TobyU
Nov 13 at 8:39
4
If it says the path doesn't exist, then probably .. the path doesn't exist? You should include the actual output from your attempts!
– marsze
Nov 13 at 8:53
add a comment |
2
How do your original filenames look exactly?
– TobyU
Nov 13 at 8:39
4
If it says the path doesn't exist, then probably .. the path doesn't exist? You should include the actual output from your attempts!
– marsze
Nov 13 at 8:53
2
2
How do your original filenames look exactly?
– TobyU
Nov 13 at 8:39
How do your original filenames look exactly?
– TobyU
Nov 13 at 8:39
4
4
If it says the path doesn't exist, then probably .. the path doesn't exist? You should include the actual output from your attempts!
– marsze
Nov 13 at 8:53
If it says the path doesn't exist, then probably .. the path doesn't exist? You should include the actual output from your attempts!
– marsze
Nov 13 at 8:53
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
Try this logic:
[string]$rootPathForFiles = Join-Path -Path $env:USERPROFILE -ChildPath 'DesktopPowerShell Practice'
[string]$listOfFilesToRename = Get-ChildItem -Path $rootPathForFiles -Filter '*.PDF' | Select-Object -ExpandProperty FullName
$listOfFilesToRename | ForEach-Object {
#get the filename wihtout the directory
[string]$newName = Split-Path -Path $_ -Leaf
#use regex replace to apply the new format
$newName = $newName -replace '^Cumulative Update (d+) .*NAV 20(d+).*$', '$2CU$1.pdf' # Assumes a certain format; if the update doesn't match this expectation the original filename is maintained
#Perform the rename
Write-Verbose "Renaming '$_' to '$newName'" -Verbose #added the verbose switch here so you'll see the output without worrying about the verbose preference
Rename-Item -Path $_ -NewName $newName
}
1
Thank you very much that works perfectly.
– Arthur
Nov 16 at 13:53
add a comment |
up vote
3
down vote
Check the Help for Rename-Item. The Parameter -NewName requires the name of the file only, not the full path.
Try out this:
Get-ChildItem "C:Users******DesktopPowerShell Practice-Filter" -Filter *.pdf | #create list of files
ForEach-Object{
$oldname = $_.FullName
$newname = $_.Name.Remove(0,17)
Rename-Item -Path $oldname -NewName $newname
$oldname
$newname #for testing
}
Still throws an error - Rename-Item Cannot rename the specified target, because it represents a path or device name.
– Arthur
Nov 16 at 9:53
add a comment |
up vote
0
down vote
Please try this
Get-ChildItem -Path "C:Users******DesktopPowerShell Practice-Filter" -Filter *.pdf | Rename-Item -NewName $newname
2
and$newnamewould be the same for all files?
– derloopkat
Nov 13 at 9:18
No, but the logic to renaming would be the same for all files.
– Arthur
Nov 13 at 17:10
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Try this logic:
[string]$rootPathForFiles = Join-Path -Path $env:USERPROFILE -ChildPath 'DesktopPowerShell Practice'
[string]$listOfFilesToRename = Get-ChildItem -Path $rootPathForFiles -Filter '*.PDF' | Select-Object -ExpandProperty FullName
$listOfFilesToRename | ForEach-Object {
#get the filename wihtout the directory
[string]$newName = Split-Path -Path $_ -Leaf
#use regex replace to apply the new format
$newName = $newName -replace '^Cumulative Update (d+) .*NAV 20(d+).*$', '$2CU$1.pdf' # Assumes a certain format; if the update doesn't match this expectation the original filename is maintained
#Perform the rename
Write-Verbose "Renaming '$_' to '$newName'" -Verbose #added the verbose switch here so you'll see the output without worrying about the verbose preference
Rename-Item -Path $_ -NewName $newName
}
1
Thank you very much that works perfectly.
– Arthur
Nov 16 at 13:53
add a comment |
up vote
0
down vote
accepted
Try this logic:
[string]$rootPathForFiles = Join-Path -Path $env:USERPROFILE -ChildPath 'DesktopPowerShell Practice'
[string]$listOfFilesToRename = Get-ChildItem -Path $rootPathForFiles -Filter '*.PDF' | Select-Object -ExpandProperty FullName
$listOfFilesToRename | ForEach-Object {
#get the filename wihtout the directory
[string]$newName = Split-Path -Path $_ -Leaf
#use regex replace to apply the new format
$newName = $newName -replace '^Cumulative Update (d+) .*NAV 20(d+).*$', '$2CU$1.pdf' # Assumes a certain format; if the update doesn't match this expectation the original filename is maintained
#Perform the rename
Write-Verbose "Renaming '$_' to '$newName'" -Verbose #added the verbose switch here so you'll see the output without worrying about the verbose preference
Rename-Item -Path $_ -NewName $newName
}
1
Thank you very much that works perfectly.
– Arthur
Nov 16 at 13:53
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Try this logic:
[string]$rootPathForFiles = Join-Path -Path $env:USERPROFILE -ChildPath 'DesktopPowerShell Practice'
[string]$listOfFilesToRename = Get-ChildItem -Path $rootPathForFiles -Filter '*.PDF' | Select-Object -ExpandProperty FullName
$listOfFilesToRename | ForEach-Object {
#get the filename wihtout the directory
[string]$newName = Split-Path -Path $_ -Leaf
#use regex replace to apply the new format
$newName = $newName -replace '^Cumulative Update (d+) .*NAV 20(d+).*$', '$2CU$1.pdf' # Assumes a certain format; if the update doesn't match this expectation the original filename is maintained
#Perform the rename
Write-Verbose "Renaming '$_' to '$newName'" -Verbose #added the verbose switch here so you'll see the output without worrying about the verbose preference
Rename-Item -Path $_ -NewName $newName
}
Try this logic:
[string]$rootPathForFiles = Join-Path -Path $env:USERPROFILE -ChildPath 'DesktopPowerShell Practice'
[string]$listOfFilesToRename = Get-ChildItem -Path $rootPathForFiles -Filter '*.PDF' | Select-Object -ExpandProperty FullName
$listOfFilesToRename | ForEach-Object {
#get the filename wihtout the directory
[string]$newName = Split-Path -Path $_ -Leaf
#use regex replace to apply the new format
$newName = $newName -replace '^Cumulative Update (d+) .*NAV 20(d+).*$', '$2CU$1.pdf' # Assumes a certain format; if the update doesn't match this expectation the original filename is maintained
#Perform the rename
Write-Verbose "Renaming '$_' to '$newName'" -Verbose #added the verbose switch here so you'll see the output without worrying about the verbose preference
Rename-Item -Path $_ -NewName $newName
}
answered Nov 16 at 12:57
JohnLBevan
14.1k145102
14.1k145102
1
Thank you very much that works perfectly.
– Arthur
Nov 16 at 13:53
add a comment |
1
Thank you very much that works perfectly.
– Arthur
Nov 16 at 13:53
1
1
Thank you very much that works perfectly.
– Arthur
Nov 16 at 13:53
Thank you very much that works perfectly.
– Arthur
Nov 16 at 13:53
add a comment |
up vote
3
down vote
Check the Help for Rename-Item. The Parameter -NewName requires the name of the file only, not the full path.
Try out this:
Get-ChildItem "C:Users******DesktopPowerShell Practice-Filter" -Filter *.pdf | #create list of files
ForEach-Object{
$oldname = $_.FullName
$newname = $_.Name.Remove(0,17)
Rename-Item -Path $oldname -NewName $newname
$oldname
$newname #for testing
}
Still throws an error - Rename-Item Cannot rename the specified target, because it represents a path or device name.
– Arthur
Nov 16 at 9:53
add a comment |
up vote
3
down vote
Check the Help for Rename-Item. The Parameter -NewName requires the name of the file only, not the full path.
Try out this:
Get-ChildItem "C:Users******DesktopPowerShell Practice-Filter" -Filter *.pdf | #create list of files
ForEach-Object{
$oldname = $_.FullName
$newname = $_.Name.Remove(0,17)
Rename-Item -Path $oldname -NewName $newname
$oldname
$newname #for testing
}
Still throws an error - Rename-Item Cannot rename the specified target, because it represents a path or device name.
– Arthur
Nov 16 at 9:53
add a comment |
up vote
3
down vote
up vote
3
down vote
Check the Help for Rename-Item. The Parameter -NewName requires the name of the file only, not the full path.
Try out this:
Get-ChildItem "C:Users******DesktopPowerShell Practice-Filter" -Filter *.pdf | #create list of files
ForEach-Object{
$oldname = $_.FullName
$newname = $_.Name.Remove(0,17)
Rename-Item -Path $oldname -NewName $newname
$oldname
$newname #for testing
}
Check the Help for Rename-Item. The Parameter -NewName requires the name of the file only, not the full path.
Try out this:
Get-ChildItem "C:Users******DesktopPowerShell Practice-Filter" -Filter *.pdf | #create list of files
ForEach-Object{
$oldname = $_.FullName
$newname = $_.Name.Remove(0,17)
Rename-Item -Path $oldname -NewName $newname
$oldname
$newname #for testing
}
answered Nov 13 at 11:18
Buxmaniak
1644
1644
Still throws an error - Rename-Item Cannot rename the specified target, because it represents a path or device name.
– Arthur
Nov 16 at 9:53
add a comment |
Still throws an error - Rename-Item Cannot rename the specified target, because it represents a path or device name.
– Arthur
Nov 16 at 9:53
Still throws an error - Rename-Item Cannot rename the specified target, because it represents a path or device name.
– Arthur
Nov 16 at 9:53
Still throws an error - Rename-Item Cannot rename the specified target, because it represents a path or device name.
– Arthur
Nov 16 at 9:53
add a comment |
up vote
0
down vote
Please try this
Get-ChildItem -Path "C:Users******DesktopPowerShell Practice-Filter" -Filter *.pdf | Rename-Item -NewName $newname
2
and$newnamewould be the same for all files?
– derloopkat
Nov 13 at 9:18
No, but the logic to renaming would be the same for all files.
– Arthur
Nov 13 at 17:10
add a comment |
up vote
0
down vote
Please try this
Get-ChildItem -Path "C:Users******DesktopPowerShell Practice-Filter" -Filter *.pdf | Rename-Item -NewName $newname
2
and$newnamewould be the same for all files?
– derloopkat
Nov 13 at 9:18
No, but the logic to renaming would be the same for all files.
– Arthur
Nov 13 at 17:10
add a comment |
up vote
0
down vote
up vote
0
down vote
Please try this
Get-ChildItem -Path "C:Users******DesktopPowerShell Practice-Filter" -Filter *.pdf | Rename-Item -NewName $newname
Please try this
Get-ChildItem -Path "C:Users******DesktopPowerShell Practice-Filter" -Filter *.pdf | Rename-Item -NewName $newname
edited Nov 13 at 9:20
Mehmet Seckin
2,0392034
2,0392034
answered Nov 13 at 9:13
thiyagu selvaraj
413
413
2
and$newnamewould be the same for all files?
– derloopkat
Nov 13 at 9:18
No, but the logic to renaming would be the same for all files.
– Arthur
Nov 13 at 17:10
add a comment |
2
and$newnamewould be the same for all files?
– derloopkat
Nov 13 at 9:18
No, but the logic to renaming would be the same for all files.
– Arthur
Nov 13 at 17:10
2
2
and
$newname would be the same for all files?– derloopkat
Nov 13 at 9:18
and
$newname would be the same for all files?– derloopkat
Nov 13 at 9:18
No, but the logic to renaming would be the same for all files.
– Arthur
Nov 13 at 17:10
No, but the logic to renaming would be the same for all files.
– Arthur
Nov 13 at 17:10
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276843%2fpowershell-loop-through-files-and-rename%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
How do your original filenames look exactly?
– TobyU
Nov 13 at 8:39
4
If it says the path doesn't exist, then probably .. the path doesn't exist? You should include the actual output from your attempts!
– marsze
Nov 13 at 8:53