Bentley Communities
Bentley Communities
  • Site
  • User
  • Site
  • Search
  • User
ProjectWise
  • Product Communities
ProjectWise
ProjectWise Design Integration Forum Powershell Rename Folder
    • Sign In

    • State Not Answered
    • +1 person also asked this people also asked this
    • Replies 4 replies
    • Subscribers 61 subscribers
    • Views 2635 views
    • Users 0 members are here

    Powershell Rename Folder

    Mark Kovach-WGI
    Offline Mark Kovach-WGI over 4 years ago

    What is the best way to rename a folder in Powershell script?

    I define the variables below through browse and select path and define the Renamed Path variable. I am able to copy the folder (essentially create it) then when it comes to the rename part it does nothing. I tried with the -path and New Name designations but nothing. I read somewhere that the folder cannot be empty if it is to be renamed. I am trying to create a project and add required subfolders based on discipline. Everything is working as intended except the rename process. Any help would be greatly appreciated.

    Rename-Item $CurrentProjectFPIDPath $RenamedFPIDFolderPath

    I am also a bit stuck with Removing variable or Clearing variables. Thought the below would work.

    # Cleanup variables

    Remove-Variable TargetFolderStorageArea, TargetFolderStorageAreaName

    I also read that Get-Variable would list all of the defined variables. Is this not correct?

    Thanks,

    Mark

    • Sign in to reply
    • Cancel
    Parents
    • Kevin van Haaren
      0 Offline Kevin van Haaren Fri, Oct 5 2018 1:46 PM

      ProjectWise files/folder paths don't work with the standard PowerShell move/rename/list commands that you use with file directories/files.

      There is a command in the PWPS_DAB module:

      Update-PWFolderNameProps -FolderPath $CurrentProjectFPIDPath -NewName $RenamedFPIDFolderPath

      I believe, but have not tested, that NewName should just be the new name for the folder, not a full path (if you want to move a folder that's a separate command)

      There usually isn't a need to remove variables unless you're scripts are taking up all of memory. If you're working with a script rather than typing in at the command line then your variables are automatically removed when the script exits.

      If you want to know more do "Help about_scopes". Running in a script is called script scope. Just typing things in at the command line is usually local scope.

       

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel
    • Mark Kovach-WGI
      0 Offline Mark Kovach-WGI Mon, Oct 8 2018 9:12 AM in reply to Kevin van Haaren

      Thank you Kevin. One of my variables is being defined based on what folders are selected in the GUI window. It seems like the process is trying to do the same thing even if the variable value does not equal to the specified search criteria. For example:

      Listed folders: ENV, TRANS, LA

      Selected folders are: TRANS, LA 

      The script is setup to copy the selected folders (which it does) and if the variable equals TRANS then it will copy another folder into the TRANS folder which it does but it seems like it is trying to so the same thing for lets say the next folder selected (LA). This is why I want to reset or remove the variable (once the session is finished before it moves on to the next selected folder) so it doesn't think the variable equals TRANS when in reality it equals LA or another folder selected. Does this make sense?

      The for each routine is not appending the variable information is it? Maybe I need to close Powershell before I test run the script again? I'm still a bit new to Powershell so I'm not 100% certain about all of the rules. In the past when a script or code (other language) reused the same variable I typically reset the variable to "" (Nothing) before it was read each time it defined/redefined or after the commands were finished (at the end of the run) before it tried to read the variable again - next search/read value. Maybe I need to do that here?

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel
    • Kevin van Haaren
      0 Offline Kevin van Haaren Mon, Oct 8 2018 5:05 PM in reply to Mark Kovach-WGI

      the foreach won't append information unless you explicitly make it. However when it reaches the end of the loop and goes back to the top, all the variables you set in the loop will have exactly the same value as they did at the end of the loop.

      If this is causing problems rather than doing a remove-variable, just assign the variable the $null value at the beginning of the loop so it resets each time.

      foreach ($f in $folders) {
          $CopyFrom = $Null
          if ($f.name -eq "TRAN") {
              $CopyFrom = "path\to\source"
          }
          if ($f.name -eq "SOMETHING") {
              $CopyFrom = "other\path"
          }
      }

       

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel
    • Kevin van Haaren
      0 Offline Kevin van Haaren Mon, Oct 8 2018 5:05 PM in reply to Kevin van Haaren

      Assigning an empty string works too

      $CopyFrom = ""

       

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel
    Reply
    • Kevin van Haaren
      0 Offline Kevin van Haaren Mon, Oct 8 2018 5:05 PM in reply to Kevin van Haaren

      Assigning an empty string works too

      $CopyFrom = ""

       

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel
    Children
    No Data

    Communities
    • Home
    • Getting Started
    • Community Central
    • Products
    • Support
    • Secure File Upload
    • Feedback
    Support and Services
    • Home
    • Product Support
    • Downloads
    • Subscription Services Portal
    Training and Learning
    • Home
    • About Bentley Institute
    • My Learning History
    • Reference Books
    Social Media
    •    LinkedIn
    •    Facebook
    •    Twitter
    •    YouTube
    •    RSS Feed
    •    Email

    © 2023 Bentley Systems, Incorporated  |  Contact Us  |  Privacy |  Terms of Use  |  Cookies