Count files in folder

Count files in folder

tecnico
Contributor Contributor
313 Views
3 Replies
Message 1 of 4

Count files in folder

tecnico
Contributor
Contributor

Taking inspiration from various forums, I ended up writing this:

 

'DATI DI PARTENZA
NOME = ThisDoc.FileName(False) ' ESTRAE NOME DOCUMENTO
PERCORSOCOM = "\\percorso\Disegni\" 'PERCORSO INIZIALE DA VARIARE IN CASO DI NUOVA DESTINAZIONE
PERCORSOFILE = PERCORSOCOM & NOME 'PERCORSO COMPLETO DI CARTELLA CON CODICE
ESTENSIONEPDF = ".pdf"
'ESTENSIONEDXF = ".dxf"
ESTENSIONEDWFX = ".dwfx"
PERCORSOPDF = PERCORSOFILE & "\" & "PDF\"  'PERCORSO COMPLETO PER FILE PDF
'PERCORSODXF = PERCORSOFILE & "\" & "DXF\"  'PERCORSO COMPLETO PER FILE DXf
PERCORSODWFX = PERCORSOFILE & "\" & "DWFX\"  'PERCORSO COMPLETO PER FILE DXf

NewFileNameAndExtensionPDF = PERCORSOPDF & "\" & NOME & ESTENSIONEPDF  'PERCORSO E NOME FILE COMPLETO PER FILE PDF
'NewFileNameAndExtensionDXF = PERCORSODXF & "\" & NOME & ESTENSIONEDXF  'PERCORSO E NOME FILE COMPLETO PER FILE DXF
NewFileNameAndExtensionDWFX = PERCORSODWFX & "\" & NOME & ESTENSIONEDWFX
PERCORSOPDFOLD = PERCORSOPDF & "OLD\" ' PERCORSO PER VECCHI FILE PDF
'PERCORSODXFOLD = PERCORSODXF & "OLD\" ' PERCORSO PER VECCHI FILE DXF
PERCORSODWFXOLD = PERCORSODWFX & "OLD\"


'CREA CARTELLE SE NON PRSENETI PDF

Dim oDesiredFolder1 As String = PERCORSOPDF ' ASSEGNA VARIABILE CARTELLA PDF
If Not System.IO.Directory.Exists(oDesiredFolder1) Then ' SE NON ESISTE LA CARTELLA
    System.IO.Directory.CreateDirectory(oDesiredFolder1) ' CREA LA CARTELLA
End If

'CREA CARTELLE SE NON PRSENETI DXF

'Dim oDesiredFolder2 As String = PERCORSODXF ' ASSEGNA VARIABILE CARTELLA DXF
'If Not System.IO.Directory.Exists(oDesiredFolder2) Then ' SE NON ESISTE LA CARTELLA
'    System.IO.Directory.CreateDirectory(oDesiredFolder2) ' CREA LA CARTELLA
'End If

'CREA CARTELLE SE NON PRSENETI DWFX

Dim oDesiredFolder5 As String = PERCORSODWFX ' ASSEGNA VARIABILE CARTELLA DXF
If Not System.IO.Directory.Exists(oDesiredFolder5) Then ' SE NON ESISTE LA CARTELLA
    System.IO.Directory.CreateDirectory(oDesiredFolder5) ' CREA LA CARTELLA
End If

'VERIFICA SE ESISTE GIà UN FILE/CARTELLA PDF
Dim currentTime As String
currentTime = Now().ToString("yyyy-MM-dd_HH_mm")

If System.IO.File.Exists(NewFileNameAndExtensionPDF) = True Then ' SE LA CARTELLA ESISTE GIà
	Dim oDesiredFolder3 As String = PERCORSOPDFOLD ' SE NON ESISTE GIA CARTELLA OLD
		If Not System.IO.Directory.Exists(oDesiredFolder3) Then
    	System.IO.Directory.CreateDirectory(oDesiredFolder3)' CREA CARTELLA OLD
		End If
	Dim source As String = NewFileNameAndExtensionPDF
	Dim destination As String = PERCORSOPDFOLD & NOME & "_OLD_" & currentTime & ESTENSIONEPDF
	System.IO.File.Copy(source,destination)
    
End If

'VERIFICA SE ESISTE GIà UN FILE/CARTELLA DXF

'If System.IO.File.Exists(NewFileNameAndExtensionDXF) = True Then ' SE LA CARTELLA ESISTE GIà
'	Dim oDesiredFolder4 As String = PERCORSODXFOLD ' SE NON ESISTE GIA CARTELLA OLD
'		If Not System.IO.Directory.Exists(oDesiredFolder4) Then
'    	System.IO.Directory.CreateDirectory(oDesiredFolder4)' CREA CARTELLA OLD
'		End If
'	Dim source As String = NewFileNameAndExtensionDXF
'	Dim destination As String = PERCORSODXFOLD & NOME & "_OLD_" & currentTime &  ESTENSIONEDXF
'	System.IO.File.Copy(source,destination)
    
'End If

'VERIFICA SE ESISTE GIà UN FILE/CARTELLA DWFX

If System.IO.File.Exists(NewFileNameAndExtensionDWFX) = True Then ' SE LA CARTELLA ESISTE GIà
	Dim oDesiredFolder6 As String = PERCORSODWFXOLD ' SE NON ESISTE GIA CARTELLA OLD
		If Not System.IO.Directory.Exists(oDesiredFolder6) Then
    	System.IO.Directory.CreateDirectory(oDesiredFolder6)' CREA CARTELLA OLD
		End If
	Dim source As String = NewFileNameAndExtensionDWFX
	Dim destination As String = PERCORSODWFXOLD & NOME & "_OLD_" & currentTime &  ESTENSIONEDWFX
	System.IO.File.Copy(source,destination)
End If

'SALVATAGGIO

ThisDoc.Document.SaveAs(NewFileNameAndExtensionPDF, True)
'ThisDoc.Document.SaveAs(NewFileNameAndExtensionDXF, True)
ThisDoc.Document.SaveAs(NewFileNameAndExtensionDWFX, True)

 

However, after using it, I noticed something. If I try to save the PDF within the same minute, it gives me some problems. Additionally, the idea of adding the time in addition to the date is a convenient solution I chose because I didn't have any other ideas.

How could I modify my code to make it check if the "old" folder exists, and if it does, to check its contents and save the various files as "filename_date + sequential number"?

Thank you.

0 Likes
Accepted solutions (2)
314 Views
3 Replies
Replies (3)
Message 2 of 4

Michael.Navara
Advisor
Advisor
Accepted solution

You can try this modified and slightly reworked version. 

 

 

 

Sub Main
	Dim fileNameWithoutExtension = ThisDoc.FileName(False) ' ESTRAE NOME DOCUMENTO
	Dim outputDir = "\\percorso\Disegni\" 'PERCORSO INIZIALE DA VARIARE IN CASO DI NUOVA DESTINAZIONE

	ExportFile(outputDir & "PDF", fileNameWithoutExtension, ".pdf")
	ExportFile(outputDir & "DWFX", fileNameWithoutExtension, ".dwfx")
	
End Sub

Sub ExportFile(targetDir As String, fileNameWithoutExtension As String, ext As String)
	'Ensure target directory
	If Not System.IO.Directory.Exists(targetDir) Then ' SE NON ESISTE LA CARTELLA
		System.IO.Directory.CreateDirectory(targetDir) ' CREA LA CARTELLA
	End If

	Dim oldDir = System.IO.Path.Combine(targetDir, "OLD")
	'Ensure OLD directory
	If Not System.IO.Directory.Exists(oldDir) Then ' SE NON ESISTE LA CARTELLA
		System.IO.Directory.CreateDirectory(oldDir) ' CREA LA CARTELLA
	End If

	Dim newFullFileName = System.IO.Path.Combine(targetDir, fileNameWithoutExtension & ext)
	Dim oldFullFileName As String
	If (System.IO.File.Exists(newFullFileName)) Then

		'Look for first unused file name in OLD directory
		For i As Integer = 1 To 1000 'max versions count
			Dim oldFileName = String.Format("{0}_OLD_{1:000}{2}", fileNameWithoutExtension, i, ext)
			Dim tmpFullFileName = System.IO.Path.Combine(oldDir, oldFileName)
			If Not System.IO.File.Exists(tmpFullFileName) Then
				'If file not found then exit loop
				oldFullFileName = tmpFullFileName
				Exit For
			End If
		Next
		'If max versions count of file reached then show warning and quit
		If String.IsNullOrEmpty(oldFullFileName) Then
			MsgBox("Max old file count reached!")
			Return
		End If

		System.IO.File.Copy(newFullFileName, oldFullFileName)

	End If

	ThisDoc.Document.SaveAs(newFullFileName, True)

End Sub

 

 

 

0 Likes
Message 3 of 4

tecnico
Contributor
Contributor
Hello and thank you for the response, I've tried it, but it only creates the folder with the file name, but nothing else works.
I tried again later, with the folder already created, but it still doesn't generate the PDF.
0 Likes
Message 4 of 4

tecnico
Contributor
Contributor
Accepted solution

I realized that your script was working, but it wasn't putting things in the folders the way I wanted (maybe I explained myself poorly). I used it as a starting point and made some modifications, and now it works exactly the way I want!

Thank you!

This is correct for my needs:

Sub Main
Dim fileNameWithoutExtension As String
fileNameWithoutExtension = ThisDoc.FileName(False)
Dim outputDir As String
outputDir = "\\percorso\Disegni\" & fileNameWithoutExtension & "\"

ExportFile(outputDir & "PDF", fileNameWithoutExtension, ".pdf")
ExportFile(outputDir & "DWFX", fileNameWithoutExtension, ".dwfx")
End Sub

Sub ExportFile(targetDir As String, fileNameWithoutExtension As String, ext As String)
' Assicurati che la cartella di destinazione esista
If Not System.IO.Directory.Exists(targetDir) Then
System.IO.Directory.CreateDirectory(targetDir)
End If

Dim newFullFileName As String
newFullFileName = System.IO.Path.Combine(targetDir, fileNameWithoutExtension & ext)

' Verifica se il file di destinazione esiste già
If System.IO.File.Exists(newFullFileName) Then
Dim oldDir As String
oldDir = System.IO.Path.Combine(targetDir, "OLD\")

' Assicurati che la cartella "OLD" esista
If Not System.IO.Directory.Exists(oldDir) Then
System.IO.Directory.CreateDirectory(oldDir)
End If

' Cerca un nome di file non utilizzato nella cartella "OLD"
Dim oldFullFileName As String
oldFullFileName = ""
For i As Integer = 1 To 1000
Dim oldFileName As String
oldFileName = String.Format("{0}_OLD_{1:000}{2}", fileNameWithoutExtension, i, ext)
Dim tmpFullFileName As String
tmpFullFileName = System.IO.Path.Combine(oldDir, oldFileName)
If Not System.IO.File.Exists(tmpFullFileName) Then
oldFullFileName = tmpFullFileName
Exit For
End If
Next

' Se il numero massimo di versioni di file è stato raggiunto, mostra un avviso
If oldFullFileName = "" Then
MsgBox("Numero massimo di versioni raggiunto!")
Exit Sub
End If

' Copia il file esistente nella cartella "OLD"
System.IO.File.Copy(newFullFileName, oldFullFileName)
End If

' Salva il documento con un nuovo nome nella cartella di destinazione
ThisDoc.Document.SaveAs(newFullFileName, True)
End Sub
0 Likes