Using Powershells Trimstart to remove array of characters from filename












0














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










share|improve this question



























    0














    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










    share|improve this question

























      0












      0








      0







      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










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 13:24









      RobCodyStuffRobCodyStuff

      13




      13
























          1 Answer
          1






          active

          oldest

          votes


















          3














          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





          share|improve this answer























          • 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 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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          3














          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





          share|improve this answer























          • 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 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
















          3














          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





          share|improve this answer























          • 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 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














          3












          3








          3






          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





          share|improve this answer














          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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 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


















          • 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 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
















          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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)