Deep Space Sync Automation for BIM 360 Folders

A PowerShell script that removes _REV_ and merges PDF files into a "Current" subdirectory

This PowerShell script is used to clean up BIM 360 files and filenames, primarily to support downstream applications such as Revizto.

 

Code embedded below:

 

#DISCLAIMER - USE AT YOUR OWN RISK
#NOTE - ONLY WORKS FOR FOLDERS SYNCED WITH DESKTOP CONNECTOR
#WIP1 - should find a way to trigger right click Sync for Autodesk Docs folder FIRST

#Configure - add your monitored folders in the block below
$allfolders =
'firstFolderPathHere',
'lastFolderPathHere'

#WIP2 - We can open explorer with below
#$path = 'C:\Users\lukes\ACCDocs\VBT\Test Project 1\Project Files'
#explorer /select,$path
#Invoke-Expression "explorer '/select,$path'"

foreach ($monfolder in $allfolders){
#$subfolders = Get-ChildItem $monfolder

$DirectoryToCreate = $monfolder + "\Current"

#Set up logging
$Logfile = $monfolder + "\Current\" + "deepSpaceSync.log"

Function LogWrite
{
Param ([string]$logstring)

Add-content $Logfile -value $logstring
}
Function PathRevision
{
Param ([string]$apath)
$thispdfname = Split-Path $apath -leaf
$base = $thispdfname -replace "\..+"
$rev = $base.Split("_REV_")
$revpart = $rev[-1]
try {
$tempinta = [int]$revpart
}
catch
{
$tempinta = 0
}
return $tempinta
}
Function NoRevision
{
Param ([string]$apath)
$thispdffolder = Split-Path $apath -Parent
$thispdfname = Split-Path $apath -leaf
$rev = $thispdfname.Split("_REV_")
$revpart = $rev[0]
$newname = $monfolder + "\Current\" + $revpart + ".pdf"
return $newname
}
#Create the Current folder
if (-not (Test-Path -LiteralPath $DirectoryToCreate)) {

try {
New-Item -Path $DirectoryToCreate -ItemType Directory -ErrorAction Stop | Out-Null #-Force
}
catch {
LogWrite "Unable to create directory '$DirectoryToCreate'. Error was: $_" -ErrorAction Stop
}
LogWrite "Successfully created directory '$DirectoryToCreate'."

}
else {
LogWrite "Directory already existed"
}

$filtered = $monfolder + "\*.pdf"

$filfiles = Get-ChildItem $filtered

$revarray = @()

foreach ($pdf in $filfiles){
$temp = PathRevision $pdf
try {
$tempint = [int]$temp
}
catch
{
$tempint = 0
}
$revarray += [int]$tempint;
#LogWrite "Found $revpart"
}

$revunq = $revarray | Sort-Object | Get-Unique

foreach ($revnum in $revunq){
LogWrite "Starting $revnum."
foreach ($pdf in $filfiles){
#$tempa = NoRevision $pdf
#LogWrite $tempa
$tempb = PathRevision $pdf
#LogWrite $tempb
$existscheck = Split-Path $pdf -leaf
if ($tempb -eq $revnum) {
$SEL = get-content $Logfile
if( $SEL -imatch $existscheck )
{LogWrite "$pdf Previously Copied."}

else {
$recordtime = Get-Date
LogWrite "Copying $pdf at $recordtime"
$tempc = NoRevision $pdf
Copy-Item $pdf $tempc
LogWrite "Copied to $tempc"
}
}
#else {LogWrite "No Match"}
}
LogWrite "Finished '$revnum'. Sleeping..."
#Sleep to allow Autodesk Docs to catch up
Start-Sleep -Seconds 300
}
$enddate = Get-Date
LogWrite "Finished this directory sync at $enddate"

}

 

Full explanatory post on What Revit Wants here.