Be sure to check out my Scripting4Crypto initiative. It’s a fun way to get into using cryptocurrencies all while getting your PowerShell needs met.
I’ve got another quick one for you today. Had a user ask how to export an entire project out to their local system. They were exporting all of the documents, but some of folders were not being exported. Turns out, these were the empty folders in the project. So, they wanted the full hierarchy and all documents within a project to be exported. The following is what I developed for them.
We will be using the following cmdlets to accomplish this task. All of the ProjectWise related cmdlets are available using the PWPS_DAB module. Take a look at the help for each of the cmdlets to become familiar with their functionality, available parameters, etc.
- Export-PWDocuments (PWPS_DAB)
- Get-PWEmptyLeafFolders (PWPS_DAB)
- New-Item (Windows)
The following shows the sample project that we will be exporting. The two highlighted folders are empty, meaning they contain no documents or sub-folders.
The following shows the target folder within Windows. Currently it is empty.
First, we will populate a few variables with ProjectWise project and Windows folder locations.
# Variables containing the source ProjectWise project and # target folder within Windows. $PWFolderToExport = 'VS_Test' $WindowsFolder = 'd:\temp\export\MyTest'
Next, we will use the Export-PWDocuments cmdlet to export out all of the documents within the source ProjectWise project. The export process will create any of the needed folders within Windows to reproduce the folder hierarchy.
<# Export documents which will create needed folders. The Results variable will contain ProjectWise document objects for all of the documents exported. #> <# Splat containing the parameter names and values for the Export-PWDocuments cmdlet. #> $Splat_ExportPWDocuments = @{ ProjectWiseFolder = $PWFolderToExport OutputFolder = $WindowsFolder } $Results = Export-PWDocuments @Splat_ExportPWDocuments -Verbose
The following shows the results of running the Export-PWDocuments cmdlet. Also, notice that the $Results variable contains 11 ProjectWise Document objects, indicating 11 documents were exported.
The following displays the contents within the target Windows folder. Notice two of the folders were not created. That is because there were no documents or sub-folders contained in those folders.
Sub-Folder01 should have another sub-folder, Sub-Folder01a. And Sub-Folder02 should contain Sub-Folder02a.
Next, we need to get an array of the empty folders within ProjectWise using the Get-PWEmptyLeafFolders cmdlet. We are going to recreate the folder structure within Windows, so we need to include the -FillPaths switch parameter to get the FullPath values for each folder.
# Get list of empty folders in the ProjectWise project. $EmptyFolders = Get-PWEmptyLeafFolders -FolderPath $PWFolderToExport -FillPaths
The following are the results of running the Get-PWEmptyLeafFolders cmdlet. Notice that there were 2 empty folders found.
Finally, we will need to loop through each of the empty folders found and create the corresponding folder within the target folder in Windows using the New-Item cmdlet.
We first need to remove ‘VS_Test’ from the fullpath value, and concatenate it with the $WindowsFolder variable value. The result will look like the following:
d:\temp\export\MyTest\Sub-Folder01\Sub-Folder01a
The following shows the fullpath values for the Empty Folders.
<# Loop through each empty folder object and create a corresponding folder on the local drive. #> foreach ($EmptyFolder in $EmptyFolders) { # Remove the ProjectWise parent folder from the fullpath. $folderPath = ($EmptyFolder.FullPath).Substring($EmptyFolder.FullPath.IndexOf('\')) # Add a new folder within windows using the concatenated value # of the $WindowsFolder value and the $folderPath from above. New-Item -Path "$WindowsFolder$folderPath" -ItemType Directory }
Now if we look at the folders within Windows again, we should see the newly created folders corresponding to the ProjectWise projects, empty folders.
Experiment with it and have fun.
Hopefully, you find this useful. Please let me know if you have any questions or comments.