delete all files in a folder with iLogic

delete all files in a folder with iLogic

Anonymous
Not applicable
4,625 Views
9 Replies
Message 1 of 10

delete all files in a folder with iLogic

Anonymous
Not applicable

Hello,

 

ok the replacig files doesent work. but thank you.

i foung a VBA code to delete all files in the folder, and with my rename (& Parameter &) are the files replaced with currently position, that i define in my linked excel tabell.

 

But the VBA doesent work always fine. I dont know how, but the VBA are deleted after a few savings.

 

Maby it is better just iLogic rules to use.

 

I didnt find a way it works.

Can you please tell, how can i create a rule to delete all files in my folder (mean all, pdf, stp, jpeg...)?

0 Likes
Accepted solutions (1)
4,626 Views
9 Replies
Replies (9)
Message 2 of 10

rossano_praderi
Collaborator
Collaborator
Accepted solution

"Position" is the parameter in your excel

"Old_Position" is an user parameter (IMPORTANT!!!!).

 

Sub Main
InventorVb.DocumentUpdate()

If Old_Position <> Position Then
	RootPath = ThisDoc.Path
		Create(RootPath & "\Steps")
		Create(RootPath & "\Steps\OLD")
		Create(RootPath & "\Zeichnungen")
		Create(RootPath & "\Zeichnungen\OLD")
	
	StepNames = New String(){"Glashalter mittig:1", "Glashalter oben:1", "Glashalter unten:1"}
	
	For Each Name In StepNames
		part = Component.InventorComponent(Name).Definition.Document.FullDocumentName
		oFileName = System.IO.Path.GetFileNameWithoutExtension(part)
		doc = ThisApplication.Documents.Open(part)
			cFile("dm", RootPath & "\Steps\" & oFileName & "_" & Old_Position & ".stp",RootPath & _
				"\Steps\OLD\" & oFileName & "_" & Old_Position & ".stp" )
		doc.saveas(RootPath & "\Steps\" & oFileName & "_" & Position & ".stp", True)
		doc.Close
	Next
	
	PdfNames = New String(){"Glashalter oben", "Glashalter unten", "Glashalter mitte", "Glas", "Glas mit Mittelfosten"}
	
	For Each PdfName In PdfNames
		doc = ThisApplication.Documents.Open(RootPath & "\Zeichnungen\" & PdfName & ".idw")
			cFile("dm", RootPath & "\Zeichnungen\" & PdfName & "_" & Old_Position & ".pdf",RootPath & _
				"\Zeichnungen\OLD\" & PdfName & "_" & Old_Position & ".pdf" )
		doc.saveas(RootPath & "\Zeichnungen\" & PdfName & "_" & Position & ".pdf", True)
		doc.Close
	Next
	Old_Position = Position
End If
End Sub

Private Sub cFile(Action As String, aExist As String, oLocation As String)
	' Action is the required action:
	'	"D"		for delete
	'	"M"		for move
	'	"DM"	for delete and move
	' aExist is the name of the file to check/move/delete
	' oLocation is the destination/deleted filename
	Try
		If System.IO.File.Exists(aExist) = True Then
			Select Case UCase(Action)
			Case "D"
				System.IO.File.Delete(oLocation)
			Case "M"
				System.IO.File.Move(aExist, oLocation)
			Case "DM"
				System.IO.File.Delete(oLocation)
				System.IO.File.Move(aExist, oLocation)
			End Select
		End If

	Catch e As exception
	MessageBox.Show("An error occurred in cFile.", "iLogic", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
	End Try
End Sub

Private Sub Create(oPath As String)
	Try
		If Not System.IO.Directory.Exists(oPath) Then
			System.IO.Directory.CreateDirectory(oPath)
		End If
	Catch e As exception
	MessageBox.Show("An error occurred in Create.", "iLogic", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
	End Try
End Sub

Private Sub DeleteAllFiles(sourceDir As String)
Try
	Dim FileList As String() = System.IO.Directory.GetFiles(sourceDir, "*.*")
	
	For Each f As String In FileList
		System.IO.File.Delete(f)
	Next
					
Catch dirNotFound As DirectoryNotFoundException
	MsgBox(dirNotFound.Message)
End Try																		

This is a solution for many of your posts. This code manage position numbers, pdf, step and delete all the files within one folder (be careful)

and now you gonna create a new post with the same questions again, is it correct?

 

 

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
Message 3 of 10

CCarreiras
Mentor
Mentor

Hi!

 

I need something like that, but not so complicated (i guess)...

I just want to fire a rule mannually to:

 

Check if there are any king of file in pré existent folders and if there are some files, ask the user if we want to "delete", "move" and "delete and move" older files.

 

In case "move or move and delete" asking for destination folder and create that folder is a good feature...

 

Can you help?

CCarreiras

EESignature

0 Likes
Message 4 of 10

MechMachineMan
Advisor
Advisor

Here is a solid start for you Carlos. Look up more info about the syntax on the msdn website under vb.net.

 

'This program will go through a prompted folder and move the higher up revision into the obsolete folder should it find one.
'It works by pulling everthing right of the last \ and then taking the first 10 or 12 chars based upon it being a PO
'It then compares this check string to the rest of the file names


Imports System
Imports System.Diagnostics
Imports System.Threading
Imports System.Windows.Forms

Sub Main()
oFolder = oFolderDlg

r= MsgBox(oFolder & " was selected" &vbLf & vbLf & "Proceed?", MsgBoxStyle.YesNo,"iLogic")
If r = MsgBoxResult.No
Exit Sub
End If


Dim oFiles() As String
Dim oFile As String


Dim oTextSave As String = "C:\Users\Desktop\Test.txt" 'Create a text doc and make this location match for post-processing info

Dim oObsFound As Boolean = False
Dim oSkip As Boolean
Dim circ As Boolean = True

oWrite = System.IO.File.CreateText(oTextSave)

While circ = True

oFiles = System.IO.Directory.GetFiles(oFolder)

'Reset the oSkip
oSkip = False

For Each oFile In oFiles

oCheck = Left(Right(oFile,Len(oFile) - InStrRev(oFile, "\", -1)), 10)
If oCheck Like "*PO*"
oCheck = Left(Right(oFile,Len(oFile) - InStrRev(oFile, "\", -1)), 12)
End If
i=0
For j = 0 To oFiles.Length - 1
oFn = oFiles(j)

If oFn Like "*" & oCheck & "*"
i=i+1
End If
Next

If i >1
oWrite.WriteLine(oFile)
If oObsFound = False
oOBS = Left(oFile, InStrRev(oFile, "\", -1)) & "OBSOLETE\"

If System.IO.Directory.Exists(oOBS) = False
MsgBox("Obsolete folder not found, please select manually")
oOBS = oFolderDlg
End If
oObsFound = True
End If
oNewName = oOBS & Right(oFile,Len(oFile) - InStrRev(oFile, "\", -1))
System.IO.File.Move(oFile, oNewName)
oSkip = True
Exit For
End If

Next
If oSkip = True
Else
circ = False
End If

End While
'MsgBox(oCheck)

oWrite.Close
ThisDoc.Launch(oTextSave)
End Sub


Function oFolderDlg

Dim dialog = New FolderBrowserDialog()
' dialog.SelectedPath = Application.StartupPath
dialog.ShowNewFolderButton = True
' openFileDialog1.InitialDirectory
If DialogResult.OK = dialog.ShowDialog() Then
oPath = dialog.SelectedPath
Else
MsgBox("No File Selected. Aborting Rule")
oPath = ""
End If
Return oPath
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 10

CCarreiras
Mentor
Mentor

Hi!

 

Thank you for your code.

I think it's a little bit complex, i can't start to do anything with that, i don't anderstand the propose of the txt file...

 

I need something like that:

 

I have already folders, where I save files converted from Inventor dwg to pdf and dwg acad and dxf

workspace &”\dxf”

workspace &”\dwg”

 

 

What I need is.

When I create a new parametrization, to have the possibility to fire a rule (clicking in a button in a form for ex.), to ask the user what to do with previous files in that folders… delete, move to a new folder, or move and delete.

In case "move (like copy/paste) or move and delete (like cut/paste)" asking for destination folder and create that folder is a good feature...

For now, after any new parametrization, the user must remember that have to clean the folders after create new pdf or dwg.

 

Thank you.

CCarreiras

EESignature

0 Likes
Message 6 of 10

MechMachineMan
Advisor
Advisor

'This program will go through a prompted folder and move the higher up revision into the obsolete folder should it find one.
'It works by pulling everthing right of the last \ and then taking the first 10 or 12 chars based upon it being a PO
'It then compares this check string to the rest of the file names

 

'Imports are used for external features. Some may be redundant
Imports System
Imports System.Diagnostics
Imports System.Threading
Imports System.Windows.Forms

 

Sub Main()

 

'This Calls the private function oFolder which allows you to select a folder path from a list. It simply returns a string of the folder path
oFolder = oFolderDlg

 

'This is just a confirmation chunk to ensure the right folder was selected.

r= MsgBox(oFolder & " was selected" &vbLf & vbLf & "Proceed?", MsgBoxStyle.YesNo,"iLogic")
If r = MsgBoxResult.No
Exit Sub
End If

 

'Declarations for the File Operations done using the vb.net that interacts with the system
Dim oFiles() As String
Dim oFile As String

 

'This text save file is simply for recording post-processing info. Change the text file to some existing text location on your computer.

'The text file is written to by the oWrite.Writeline() command. If used, it must also include an open and close feature.
Dim oTextSave As String = "C:\Users\Rob\Desktop\Test.txt"

 

'A boolean to check if the obsolete folder was found, so that the user wont be prompted every loop regardless of the outcome.

Dim oObsFound As Boolean = False

'Booleans to control the looping of the program

Dim oSkip As Boolean

Dim circ As Boolean = True

 

'Creating the text file for post processing

oWrite = System.IO.File.CreateText(oTextSave)

 

'The while loop to control the folder search feature

While circ = True

 

'Making a list of the files in the folder, as first found by using the Folder Dialog Private function

oFiles = System.IO.Directory.GetFiles(oFolder)

'Reset the oSkip
oSkip = False

 

'Iterating through the list of files one at a time.

For Each oFile In oFiles

'Just a variable used to shorten the file name for comparison sake; this one is grabbing our part number from the end of the name; ie; 200123-123

'from the string C:/Users/200123-123.ipt


oCheck = Left(Right(oFile,Len(oFile) - InStrRev(oFile, "\", -1)), 10)

'Just a condition check; some titles we have contain PO, so I perform a different check for them, because of how the strings work

If oCheck Like "*PO*"
oCheck = Left(Right(oFile,Len(oFile) - InStrRev(oFile, "\", -1)), 12)
End If

 

'Set the index to 0 again seeing as this is in a loop
i=0

 

'This string checks for as many files are in the array. (0 to n-1)
For j = 0 To oFiles.Length - 1

'This declares oFn as the String that occupies the array oFiles at the location j

oFn = oFiles(j)

'THis is a string comparison; LIKE is an operator, * are wildcards, meaning any character and any amount of them

'It is essentially checking to see if the file name in the folder matches what I am check it against.
If oFn Like "*" & oCheck & "*"
i=i+1
End If
Next

'This whole i+ and i+1 will tell me if i have more than one similiar file.

'Our file system has files designate 201231-123_R1 where R is revision number.

'From there, I remove all revisions that are lower than the highest one; which is what the i index keeps track of

'The delete only needs to occur if there is more than 1 revision in the folder, thus the i>1
'oWrite is just recording what files i delete so i have a log of it in case something goes wrong

 

If i >1
oWrite.WriteLine(oFile)

'This is a check; for the current folder, if it has not found the sub-folder that I want to move the files to, it will prompt the user for input

'This is unnessary to prompt the user for input if the program can find the folder on it's own; the oObsFound or "Obsolete Found" is the controller for 'this

 

If oObsFound = False

 

'This is the string to search for the obsolete folder using the program
oOBS = Left(oFile, InStrRev(oFile, "\", -1)) & "OBSOLETE\"

'This is checking if the folder path that I want to move the files to exists already.
If System.IO.Directory.Exists(oOBS) = False
MsgBox("Obsolete folder not found, please select manually")

 

'Again, the oFolderDlg function to allow the user to find/make the folder to move files to.
oOBS = oFolderDlg
End If

 

'This is telling it that it found the obsolete folder, or one was made, so that it doesn't need to look for it next time through the loop, or until i reset the control again.
oObsFound = True
End If

 

'This is the full file path. The move function requires that you put the whole string of what you want the file to end up as rather than just the directory you want to move it to; ie it's a copy, rename, and delete.

oNewName = oOBS & Right(oFile,Len(oFile) - InStrRev(oFile, "\", -1))

 

'A try loop for moving the file in case there are any errors; such as a file of the same name already existing in the folder.
Try

System.IO.File.Move(oFile, oNewName)
Catch
MsgBox(oNewName)
End Try

 

'I cant recall what the oSkip is for, but it plays some function in controlling the looping

oSkip = True
Exit For


End If

Next
If oSkip = True
Else
circ = False
End If

End While

 

'THis closes it and ends off the function
'MsgBox(oCheck)

oWrite.Close
ThisDoc.Launch(oTextSave)
End Sub


Function oFolderDlg

Dim dialog = New FolderBrowserDialog()
' dialog.SelectedPath = Application.StartupPath
dialog.ShowNewFolderButton = True
' openFileDialog1.InitialDirectory
If DialogResult.OK = dialog.ShowDialog() Then
oPath = dialog.SelectedPath
Else
MsgBox("No File Selected. Aborting Rule")
oPath = ""
End If
Return oPath
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
Message 7 of 10

MechMachineMan
Advisor
Advisor

Based off of my explanations, that should be lots to go off of. You shouldn't need to touch anything in the oFolderDlg function; just simply call oFolderDlg when you want the user to browse for a folder.

For any string operations or vb.net operations, I strongly suggest looking into the msdn website under their vb.net material as that is what iLogic is coded using.

Between MSDN and Programming help within inventor you should be able to accomplish everything you need to with this. If you are lost again, just search in the forums some more. If you can't find anything there, and it is more vb.net programming than inventor specific stuff you are getting stuck on, head over to Stack Overflow and check that out.

If I were to give you any more help I might as well send you an invoice for the projected cost savings haha

 

 

** Looking more into my code, the oSkip is actually used as a controller so that it will rewrite the List of Files everytime I move one. If I weren't to do this, the For Each Loop would cause an error since I would be moving files and there wouldn't be objects to occupy the array bound. 

*** The circ = False line is there to end the while loop for when the program can finally make it through the whole folder without moving a single file, which will then let the program exit.

 

** There are always other ways to do things, this is just what came to mind for me when I initially wrote this program.

 

Good luck.


--------------------------------------
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
Message 8 of 10

CCarreiras
Mentor
Mentor

HI!

 

 

Thank you, i will check.

If it works, you can send me the bill later 🙂

CCarreiras

EESignature

0 Likes
Message 9 of 10

MechMachineMan
Advisor
Advisor
This won't do what you want it to exactly; I just added the explanations to
the same thing I posted before.

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

CCarreiras
Mentor
Mentor

Hi Justin!

 

Yes, i know.

 

If you want to give your ideas and help me, you can do that here:

 

http://forums.autodesk.com/t5/inventor-customization/delete-move-and-move-and-delete-from-one-folder...

 

Thank you!!

CCarreiras

EESignature

0 Likes