Folder based on filename

Folder based on filename

Anonymous
Not applicable
789 Views
7 Replies
Message 1 of 8

Folder based on filename

Anonymous
Not applicable

Hi.

I jhave searched thru the forum but i can not find exactly what i am searching for.

 

I want to publish some files PDF´s, DXF´s DWFx´s etc.

 

The root folder is the same for example C:\PDF, then i need the code to create a sub folder based on the files name, and put my files there.

 

So if i put out a PDF and a DWFx of the file 123abc.dwg they will be saved in C:\PDF\123abc

 

The code i now use to save fileformats.

 

SyntaxEditor Code Snippet

strFolder = "C:\Users\lelo\AppData\Local\Autodesk\Autodesk Sync\Cloud\lennart.losjo\Part Drawings\" & ThisDoc.FileName(False) 'without extension

ThisDoc.Document.SaveAs(strFolder & (".dwfx") , True)
ThisDoc.Document.SaveAs(strFolder & (".pdf") , True)
ThisDoc.Document.SaveAs(strFolder & (".dxf") , True)

 

In this case i want to save the files to my A360 folder "part drawings" and i need the code to create a folder based on filename.

 

Thanks

 

Lennart

0 Likes
Accepted solutions (1)
790 Views
7 Replies
Replies (7)
Message 2 of 8

demuff
Enthusiast
Enthusiast

If all you're trying to do is create the subfolders, check to see if the folder exists just in case then call mkdir:

newDir = strfolder & "\PDF"
If Dir(newDir, vbDirectory) = "" Then
    MkDir newDir
End If

Change the "\PDF" to the text of the subfolder you want to create and insert the code before doing your SaveAs command to the newDir path.

0 Likes
Message 3 of 8

Anonymous
Not applicable
Hi. Thank you for the reply. I think i was unclear. The \PDF folder is static. Under the PDF folder i want to automaticly create subfolders witch name should be the same as the name of the file i want to save. Of course should it check if the folder already exists as in your code. 


Med vänliga hälsningar

Lennart Losjö
0 Likes
Message 4 of 8

MechMachineMan
Advisor
Advisor

I guess you could try something like this then. Might need to mess around with where the backslashes go.

 

Sub Main()
    strFolder = "C:\Users\lelo\AppData\Local\Autodesk\Autodesk Sync\Cloud\lennart.losjo\Part Drawings\"

    ThisDoc.Document.SaveAs(GetFolder(strFolder & "DWF\" & ThisDoc.FileName(False)) & "\" & ".dwfx", True)
    ThisDoc.Document.SaveAs(GetFolder(strFolder & "PDF\" & ThisDoc.FileName(False)) & "\" & ".pdf", True)
    ThisDoc.Document.SaveAs(GetFolder(strFolder & "DXF\" & ThisDoc.FileName(False)) & "\" & ".dxf", True)

End Sub

Function GetFolder(ostr) As String
    If Dir(ostr, vbDirectory) = "" Then
        MkDir -p ostr
    End If
    Return ostr
End Function

But if that one doesn't work, trying swapping out the function block for this:

 

Function GetFolder(ostr) As String
If
(Not System.IO.Directory.Exists(ostr)) Then
System
.IO.Directory.CreateDirectory(ostr)
End If
Return ostr
End Function

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 5 of 8

Anonymous
Not applicable
Hi. Thanks for your engagement. 
Now we are getting close. 
But in this case my part drawings folder are static. 
If i export a drawing for example 123abc.dwg to pdf, dwfx and so on i want put all of them in a folder named 123abc. 
And if i export a DXF from a sheet metal with the same name (123abc.ipt) i want to put it in the same folder named 123abc
Sorry for my lack of good explanation. 





Med vänliga hälsningar

Lennart Losjö
0 Likes
Message 6 of 8

MechMachineMan
Advisor
Advisor

I would try this then.

 

Or does this code not save the actual files down properly, but does the location right?

 

Sub Main()
    strFolder = "C:\Users\lelo\AppData\Local\Autodesk\Autodesk Sync\Cloud\lennart.losjo\Part Drawings\"
    oSaveNameWOExtension = GetFolder(strFolder)	& "\" & ThisDoc.FileName(False)

    ThisDoc.Document.SaveAs(oSaveNameWOExtension & ".dwfx", True)
    ThisDoc.Document.SaveAs(oSaveNameWOExtension & ".pdf", True)
    ThisDoc.Document.SaveAs(oSaveNameWOExtension & ".dxf", True)

End Sub

Function GetFolder(ostr) As String
    If(Not System.IO.Directory.Exists(ostr)) Then 
        System.IO.Directory.CreateDirectory(ostr) 
    End If
    Return ostr
End Function

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 7 of 8

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

Hi Justin,

 

Solution is almost closer to requirement. A tiny changes made to code.

 

Lennart - Can you please test the following code and confirm the same?

 

Are we in right direction to requirement?

 

Sub Main()
    strFolder = "C:\Users\lelo\AppData\Local\Autodesk\Autodesk Sync\Cloud\lennart.losjo\Part Drawings\"& ThisDoc.FileName(False)
    oSaveNameWOExtension = GetFolder(strFolder)	& "\" & ThisDoc.FileName(False)

    ThisDoc.Document.SaveAs(oSaveNameWOExtension & ".dwfx", True)
    ThisDoc.Document.SaveAs(oSaveNameWOExtension & ".pdf", True)
    ThisDoc.Document.SaveAs(oSaveNameWOExtension & ".dxf", True)

End Sub

Function GetFolder(ostr) As String
    If(Not System.IO.Directory.Exists(ostr)) Then 
        System.IO.Directory.CreateDirectory(ostr) 
    End If
    Return ostr
End Function

Please feel free to contact if there is any doubt.

 

If solves your problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 8 of 8

Anonymous
Not applicable

Hi.

Perfect, exactly the function i need.

 

Thank you very much.

Smiley Happy

 

 

0 Likes