Using Powershells Trimstart to remove array of characters from filename
I'm trying to do a simple bulk file rename to remove non-standard characters (e.g. " 0123456789-_.") from the start of a filename and just add a string to the filename.
e.g. '12 -_myfilename.doc' would become '012345 - myfilename.doc'
...where 012345 is my study number. I've tried using the script below but keep on getting the following error when stepping through the script executing the trimstart line...
"'Name' is a ReadOnly" property"
I guess that this is not a trimstart problem but the way in which I'm attempting to gather the result from it.
Any help appreciated.
The relevant part of the code looks like...
$MyFileObject=0
if ($MyRecursiveFlag) {
Get-ChildItem $MyStudyPath -recurse | where {$_.extension -in ".xls",”.xlsx”,".xslt",".pdf",".doc",".docx",".xlsm",".xml",".htm",".ppt"}|
ForEach-Object{
#Check if start of the file is compliant
$mymatch = [Regex]::Match($_, 'd{5}s-s')
if ($mymatch.Success){
#Already renamed correctly so nothing else to do
Write-Host "Okay - no changes $($_.Name)" -ForegroundColor Green
} else {
#No, it's not compliant so let's remove any preceding numbers, spaces and dashes etc
$MyFileObject = $_
$MyFileObject.Name = $MyFileObject.Name.trimstart(" 0123456789-_.")
#...and rename the file
Rename-Item -LiteralPath $MyFileObject.FullName -NewName "$($MyStudyNumber + ' - ' + $MyFileObject.Name)"
Write-Host "Renamed to $($MyFileObject.Name)" -ForegroundColor Yellow
}
}
}
I'm have the same problem when using Windows 7 PowerShell v3 and 5.1
powershell trim readonly
add a comment |
I'm trying to do a simple bulk file rename to remove non-standard characters (e.g. " 0123456789-_.") from the start of a filename and just add a string to the filename.
e.g. '12 -_myfilename.doc' would become '012345 - myfilename.doc'
...where 012345 is my study number. I've tried using the script below but keep on getting the following error when stepping through the script executing the trimstart line...
"'Name' is a ReadOnly" property"
I guess that this is not a trimstart problem but the way in which I'm attempting to gather the result from it.
Any help appreciated.
The relevant part of the code looks like...
$MyFileObject=0
if ($MyRecursiveFlag) {
Get-ChildItem $MyStudyPath -recurse | where {$_.extension -in ".xls",”.xlsx”,".xslt",".pdf",".doc",".docx",".xlsm",".xml",".htm",".ppt"}|
ForEach-Object{
#Check if start of the file is compliant
$mymatch = [Regex]::Match($_, 'd{5}s-s')
if ($mymatch.Success){
#Already renamed correctly so nothing else to do
Write-Host "Okay - no changes $($_.Name)" -ForegroundColor Green
} else {
#No, it's not compliant so let's remove any preceding numbers, spaces and dashes etc
$MyFileObject = $_
$MyFileObject.Name = $MyFileObject.Name.trimstart(" 0123456789-_.")
#...and rename the file
Rename-Item -LiteralPath $MyFileObject.FullName -NewName "$($MyStudyNumber + ' - ' + $MyFileObject.Name)"
Write-Host "Renamed to $($MyFileObject.Name)" -ForegroundColor Yellow
}
}
}
I'm have the same problem when using Windows 7 PowerShell v3 and 5.1
powershell trim readonly
add a comment |
I'm trying to do a simple bulk file rename to remove non-standard characters (e.g. " 0123456789-_.") from the start of a filename and just add a string to the filename.
e.g. '12 -_myfilename.doc' would become '012345 - myfilename.doc'
...where 012345 is my study number. I've tried using the script below but keep on getting the following error when stepping through the script executing the trimstart line...
"'Name' is a ReadOnly" property"
I guess that this is not a trimstart problem but the way in which I'm attempting to gather the result from it.
Any help appreciated.
The relevant part of the code looks like...
$MyFileObject=0
if ($MyRecursiveFlag) {
Get-ChildItem $MyStudyPath -recurse | where {$_.extension -in ".xls",”.xlsx”,".xslt",".pdf",".doc",".docx",".xlsm",".xml",".htm",".ppt"}|
ForEach-Object{
#Check if start of the file is compliant
$mymatch = [Regex]::Match($_, 'd{5}s-s')
if ($mymatch.Success){
#Already renamed correctly so nothing else to do
Write-Host "Okay - no changes $($_.Name)" -ForegroundColor Green
} else {
#No, it's not compliant so let's remove any preceding numbers, spaces and dashes etc
$MyFileObject = $_
$MyFileObject.Name = $MyFileObject.Name.trimstart(" 0123456789-_.")
#...and rename the file
Rename-Item -LiteralPath $MyFileObject.FullName -NewName "$($MyStudyNumber + ' - ' + $MyFileObject.Name)"
Write-Host "Renamed to $($MyFileObject.Name)" -ForegroundColor Yellow
}
}
}
I'm have the same problem when using Windows 7 PowerShell v3 and 5.1
powershell trim readonly
I'm trying to do a simple bulk file rename to remove non-standard characters (e.g. " 0123456789-_.") from the start of a filename and just add a string to the filename.
e.g. '12 -_myfilename.doc' would become '012345 - myfilename.doc'
...where 012345 is my study number. I've tried using the script below but keep on getting the following error when stepping through the script executing the trimstart line...
"'Name' is a ReadOnly" property"
I guess that this is not a trimstart problem but the way in which I'm attempting to gather the result from it.
Any help appreciated.
The relevant part of the code looks like...
$MyFileObject=0
if ($MyRecursiveFlag) {
Get-ChildItem $MyStudyPath -recurse | where {$_.extension -in ".xls",”.xlsx”,".xslt",".pdf",".doc",".docx",".xlsm",".xml",".htm",".ppt"}|
ForEach-Object{
#Check if start of the file is compliant
$mymatch = [Regex]::Match($_, 'd{5}s-s')
if ($mymatch.Success){
#Already renamed correctly so nothing else to do
Write-Host "Okay - no changes $($_.Name)" -ForegroundColor Green
} else {
#No, it's not compliant so let's remove any preceding numbers, spaces and dashes etc
$MyFileObject = $_
$MyFileObject.Name = $MyFileObject.Name.trimstart(" 0123456789-_.")
#...and rename the file
Rename-Item -LiteralPath $MyFileObject.FullName -NewName "$($MyStudyNumber + ' - ' + $MyFileObject.Name)"
Write-Host "Renamed to $($MyFileObject.Name)" -ForegroundColor Yellow
}
}
}
I'm have the same problem when using Windows 7 PowerShell v3 and 5.1
powershell trim readonly
powershell trim readonly
asked Nov 15 '18 at 13:24
RobCodyStuffRobCodyStuff
13
13
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You cannot change the Name
of the current Get-ChildItem
object (in your case $MyFileObject
).
This should work (part of your script):
$MyFileObject = $_
$FileName = $MyFileObject.Name.trimstart(" 0123456789-_.")
#...and rename the file
Rename-Item -LiteralPath $MyFileObject.FullName -NewName "$($MyStudyNumber + ' - ' + $FileName)"
Write-Host "Renamed to $FileName" -ForegroundColor Yellow
To have the terminal Write-Host return the true new$FileName
I'd prepend the StudyNumber directly to it.
– LotPings
Nov 15 '18 at 14:40
Thanks - the code now does the job. Re Write-Host... That's still my problem in that my write-host cannot reference the true new file name. ...unless, as you say, by pre-pending $FileName with the $MyStudyNumber. Is there a way of referencing the new true filename without having to do this? ...such as from $MyFileObject? I've tried and cannot "see" the true new filename in the debugger.
– RobCodyStuff
Nov 15 '18 at 15:48
@RobCodyStuff - the simplest is to simply add a line that defines the $Var before theRename-Item
command. then use it in both that line and theWrite-Host
line. [grin]
– Lee_Dailey
Nov 15 '18 at 16:40
Thanks for all your help. Working well now.
– RobCodyStuff
Nov 15 '18 at 17:28
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53320492%2fusing-powershells-trimstart-to-remove-array-of-characters-from-filename%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You cannot change the Name
of the current Get-ChildItem
object (in your case $MyFileObject
).
This should work (part of your script):
$MyFileObject = $_
$FileName = $MyFileObject.Name.trimstart(" 0123456789-_.")
#...and rename the file
Rename-Item -LiteralPath $MyFileObject.FullName -NewName "$($MyStudyNumber + ' - ' + $FileName)"
Write-Host "Renamed to $FileName" -ForegroundColor Yellow
To have the terminal Write-Host return the true new$FileName
I'd prepend the StudyNumber directly to it.
– LotPings
Nov 15 '18 at 14:40
Thanks - the code now does the job. Re Write-Host... That's still my problem in that my write-host cannot reference the true new file name. ...unless, as you say, by pre-pending $FileName with the $MyStudyNumber. Is there a way of referencing the new true filename without having to do this? ...such as from $MyFileObject? I've tried and cannot "see" the true new filename in the debugger.
– RobCodyStuff
Nov 15 '18 at 15:48
@RobCodyStuff - the simplest is to simply add a line that defines the $Var before theRename-Item
command. then use it in both that line and theWrite-Host
line. [grin]
– Lee_Dailey
Nov 15 '18 at 16:40
Thanks for all your help. Working well now.
– RobCodyStuff
Nov 15 '18 at 17:28
add a comment |
You cannot change the Name
of the current Get-ChildItem
object (in your case $MyFileObject
).
This should work (part of your script):
$MyFileObject = $_
$FileName = $MyFileObject.Name.trimstart(" 0123456789-_.")
#...and rename the file
Rename-Item -LiteralPath $MyFileObject.FullName -NewName "$($MyStudyNumber + ' - ' + $FileName)"
Write-Host "Renamed to $FileName" -ForegroundColor Yellow
To have the terminal Write-Host return the true new$FileName
I'd prepend the StudyNumber directly to it.
– LotPings
Nov 15 '18 at 14:40
Thanks - the code now does the job. Re Write-Host... That's still my problem in that my write-host cannot reference the true new file name. ...unless, as you say, by pre-pending $FileName with the $MyStudyNumber. Is there a way of referencing the new true filename without having to do this? ...such as from $MyFileObject? I've tried and cannot "see" the true new filename in the debugger.
– RobCodyStuff
Nov 15 '18 at 15:48
@RobCodyStuff - the simplest is to simply add a line that defines the $Var before theRename-Item
command. then use it in both that line and theWrite-Host
line. [grin]
– Lee_Dailey
Nov 15 '18 at 16:40
Thanks for all your help. Working well now.
– RobCodyStuff
Nov 15 '18 at 17:28
add a comment |
You cannot change the Name
of the current Get-ChildItem
object (in your case $MyFileObject
).
This should work (part of your script):
$MyFileObject = $_
$FileName = $MyFileObject.Name.trimstart(" 0123456789-_.")
#...and rename the file
Rename-Item -LiteralPath $MyFileObject.FullName -NewName "$($MyStudyNumber + ' - ' + $FileName)"
Write-Host "Renamed to $FileName" -ForegroundColor Yellow
You cannot change the Name
of the current Get-ChildItem
object (in your case $MyFileObject
).
This should work (part of your script):
$MyFileObject = $_
$FileName = $MyFileObject.Name.trimstart(" 0123456789-_.")
#...and rename the file
Rename-Item -LiteralPath $MyFileObject.FullName -NewName "$($MyStudyNumber + ' - ' + $FileName)"
Write-Host "Renamed to $FileName" -ForegroundColor Yellow
edited Nov 15 '18 at 14:05
mklement0
127k20241269
127k20241269
answered Nov 15 '18 at 13:31
TobyUTobyU
2,191721
2,191721
To have the terminal Write-Host return the true new$FileName
I'd prepend the StudyNumber directly to it.
– LotPings
Nov 15 '18 at 14:40
Thanks - the code now does the job. Re Write-Host... That's still my problem in that my write-host cannot reference the true new file name. ...unless, as you say, by pre-pending $FileName with the $MyStudyNumber. Is there a way of referencing the new true filename without having to do this? ...such as from $MyFileObject? I've tried and cannot "see" the true new filename in the debugger.
– RobCodyStuff
Nov 15 '18 at 15:48
@RobCodyStuff - the simplest is to simply add a line that defines the $Var before theRename-Item
command. then use it in both that line and theWrite-Host
line. [grin]
– Lee_Dailey
Nov 15 '18 at 16:40
Thanks for all your help. Working well now.
– RobCodyStuff
Nov 15 '18 at 17:28
add a comment |
To have the terminal Write-Host return the true new$FileName
I'd prepend the StudyNumber directly to it.
– LotPings
Nov 15 '18 at 14:40
Thanks - the code now does the job. Re Write-Host... That's still my problem in that my write-host cannot reference the true new file name. ...unless, as you say, by pre-pending $FileName with the $MyStudyNumber. Is there a way of referencing the new true filename without having to do this? ...such as from $MyFileObject? I've tried and cannot "see" the true new filename in the debugger.
– RobCodyStuff
Nov 15 '18 at 15:48
@RobCodyStuff - the simplest is to simply add a line that defines the $Var before theRename-Item
command. then use it in both that line and theWrite-Host
line. [grin]
– Lee_Dailey
Nov 15 '18 at 16:40
Thanks for all your help. Working well now.
– RobCodyStuff
Nov 15 '18 at 17:28
To have the terminal Write-Host return the true new
$FileName
I'd prepend the StudyNumber directly to it.– LotPings
Nov 15 '18 at 14:40
To have the terminal Write-Host return the true new
$FileName
I'd prepend the StudyNumber directly to it.– LotPings
Nov 15 '18 at 14:40
Thanks - the code now does the job. Re Write-Host... That's still my problem in that my write-host cannot reference the true new file name. ...unless, as you say, by pre-pending $FileName with the $MyStudyNumber. Is there a way of referencing the new true filename without having to do this? ...such as from $MyFileObject? I've tried and cannot "see" the true new filename in the debugger.
– RobCodyStuff
Nov 15 '18 at 15:48
Thanks - the code now does the job. Re Write-Host... That's still my problem in that my write-host cannot reference the true new file name. ...unless, as you say, by pre-pending $FileName with the $MyStudyNumber. Is there a way of referencing the new true filename without having to do this? ...such as from $MyFileObject? I've tried and cannot "see" the true new filename in the debugger.
– RobCodyStuff
Nov 15 '18 at 15:48
@RobCodyStuff - the simplest is to simply add a line that defines the $Var before the
Rename-Item
command. then use it in both that line and the Write-Host
line. [grin]– Lee_Dailey
Nov 15 '18 at 16:40
@RobCodyStuff - the simplest is to simply add a line that defines the $Var before the
Rename-Item
command. then use it in both that line and the Write-Host
line. [grin]– Lee_Dailey
Nov 15 '18 at 16:40
Thanks for all your help. Working well now.
– RobCodyStuff
Nov 15 '18 at 17:28
Thanks for all your help. Working well now.
– RobCodyStuff
Nov 15 '18 at 17:28
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53320492%2fusing-powershells-trimstart-to-remove-array-of-characters-from-filename%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