Valutazione 4.87/ 5 (100.00%) 5838 voti

Condividi:        

VBScript: eliminare cartelle vuote

Problemi di HTML? Di PHP, ASP, .NET, JSP, Perl, SQL, JavaScript, Visual Basic..?
Vuoi realizzare programmi in C, C++, Java, Ruby o Smalltalk, e non sai da che parte cominciare?
Entra qui e troverai le risposte!

Moderatori: Triumph Of Steel, archimede

VBScript: eliminare cartelle vuote

Postdi evvivame » 07/08/13 22:35

Ciao !
Con soddisfazione, sto usando da diversi giorni questo script trovato sul web:
Codice: Seleziona tutto
 Objective: To delete old files from a given folder and all subfolders below
'
' Created by: MAK June 21, 2005
'
' Format: cscript deloldfiles.vbs {DriveLetter:\FolderName} {#ofDays}
'     or: cscript deloldfiles.vbs {\\servername\FolderName} {#ofDays}
' Example: cscript deloldfiles.vbs c:\dba\log 3
'    (deletes files older than 3 days from the \dba\log file on drive C
Set objArgs = WScript.Arguments
FolderName =objArgs(0)
Days=objArgs(1)

set fso = createobject("scripting.filesystemobject")
set folders = fso.getfolder(FolderName)
datetoday = now()
newdate = dateadd("d", Days*-1, datetoday)
wscript.echo "Today:" & now()
wscript.echo "Started deleting files older than :" & newdate
wscript.echo "________________________________________________"
wscript.echo ""
recurse folders
wscript.echo ""
wscript.echo "Completed deleting files older than :" & newdate
wscript.echo "________________________________________________"

sub recurse( byref folders)
  set subfolders = folders.subfolders
  set files = folders.files
  wscript.echo ""
  wscript.echo "Deleting Files under the Folder:" & folders.path
  wscript.echo "__________________________________________________________________________"
  for each file in files
    if file.datelastmodified < newdate then
      wscript.echo "Deleting " & folders.path & "\" & file.name & " last modified: " & file.datelastmodified
      on error resume next
' === to test this script but not actually delete files, comment out the next line ===
    file.delete
    end if
   
  next 

  for each folder in subfolders
    recurse folder
  next 

  set subfolders = nothing
  set files = nothing

end sub

che, come vedete,cancella i files piu vecchi di xx giorni.

Ha solo un piccolissssssimo/banalissimo neo:
Dopo aver eliminato i files obsoleti, mi rimane (giustamente!!!) la relativa cartella vuota.
Premesso che so 'zero' di vb, chiedo gentilmente a chi lo conosce:
è' banale eliminare (anche) ste cartelle vuote ?
Se non è banale vorra dire che continuerò a eliminarle a manina.

Ringrazio in ogni caso !
evvivame
Utente Senior
 
Post: 133
Iscritto il: 19/09/05 20:38

Sponsor
 

Postdi archimede » 08/08/13 09:02

Non l'ho provato, quindi ATTENZIONE:
Codice: Seleziona tutto
     Objective: To delete old files from a given folder and all subfolders below
    '
    ' Created by: MAK June 21, 2005
    '
    ' Format: cscript deloldfiles.vbs {DriveLetter:\FolderName} {#ofDays}
    '     or: cscript deloldfiles.vbs {\\servername\FolderName} {#ofDays}
    ' Example: cscript deloldfiles.vbs c:\dba\log 3
    '    (deletes files older than 3 days from the \dba\log file on drive C
    Set objArgs = WScript.Arguments
    FolderName =objArgs(0)
    Days=objArgs(1)

    set fso = createobject("scripting.filesystemobject")
    set folders = fso.getfolder(FolderName)
    datetoday = now()
    newdate = dateadd("d", Days*-1, datetoday)
    wscript.echo "Today:" & now()
    wscript.echo "Started deleting files older than :" & newdate
    wscript.echo "________________________________________________"
    wscript.echo ""
    recurse folders
    wscript.echo ""
    wscript.echo "Completed deleting files older than :" & newdate
    wscript.echo "________________________________________________"

    sub recurse( byref folders)
      set subfolders = folders.subfolders
      set files = folders.files
      wscript.echo ""
      wscript.echo "Deleting Files under the Folder:" & folders.path
      wscript.echo "__________________________________________________________________________"
      for each file in files
        if file.datelastmodified < newdate then
          wscript.echo "Deleting " & folders.path & "\" & file.name & " last modified: " & file.datelastmodified
          on error resume next
    ' === to test this script but not actually delete files, comment out the next line ===
        file.delete
        end if

        If folders.SubFolders.Count = 0 And folders.Files.Count = 0 Then
           WScript.Echo "Deleting " & folders.path
           folders.Delete
        End If
      next

      for each folder in subfolders
        recurse folder
      next

      set subfolders = nothing
      set files = nothing

    end sub
HTH.

Alessandro
archimede
Moderatore
 
Post: 2753
Iscritto il: 07/11/02 12:41
Località: Genova

Postdi evvivame » 08/08/13 12:38

Alessandro,
grazie di cuore per il tuo aiuto !
purtroppo mi sembra non funzioni.
Per aiutarti ad aiutarmi ti allego il log:
Microsoft (R) Windows Script Host Versione 5.7
Copyright (C) Microsoft Corporation 1996-2001. Tutti i diritti riservati.

Today:08/08/2013 13.34.21
Started deleting files older than :07/08/2013 13.34.21
________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\alarm
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\alarm\B2
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\alarm\B2\2013-08-07
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\alarm\B2\2013-08-07\02.06.13
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\alarm\B2\2013-08-07\02.11.41
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\alarm\B2\2013-08-07\05.59.39
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\alarm\B2\2013-08-07\06.04.01
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\alarm\B2\2013-08-07\06.14.40
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\alarm\B2\2013-08-07\08.32.55
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\alarm\B2\2013-08-07\09.16.45
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\alarm\B2\2013-08-07\09.18.09
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\alarm\B2\2013-08-07\09.19.33
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\alarm\B2\2013-08-07\09.22.49
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\alarm\B2\2013-08-07\09.24.48
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\alarm\B2\2013-08-07\09.27.37
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\alarm\B2\2013-08-07\10.24.41
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\alarm\B2\2013-08-07\14.00.24
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\video
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\video\B1
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\video\B1\2013-08-06
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\video\B1\2013-08-08
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\video\B2
__________________________________________________________________________
Deleting Files under the Folder:D:\Documenti\IP Camera Super Client Full\video\B2\2013-08-06
__________________________________________________________________________
Completed deleting files older than :07/08/2013 13.34.21
________________________________________________


e il tree dopo l'esecuzione del vbs
Immagine

Come vedi le (2) cartelle 2013-08-06 sono, sebbene vuote, ancora presenti

Grazie ancora
evvivame
Utente Senior
 
Post: 133
Iscritto il: 19/09/05 20:38

Postdi archimede » 09/08/13 18:06

Prova così:
Codice: Seleziona tutto
' Objective: To delete old files from a given folder and all subfolders below
'
' Created by: MAK June 21, 2005
'
' Format: cscript deloldfiles.vbs {DriveLetter:\FolderName} {#ofDays}
'     or: cscript deloldfiles.vbs {\\servername\FolderName} {#ofDays}
' Example: cscript deloldfiles.vbs c:\dba\log 3
'    (deletes files older than 3 days from the \dba\log file on drive C
Set objArgs = WScript.Arguments
FolderName =objArgs(0)
Days=objArgs(1)

set fso = createobject("scripting.filesystemobject")
set folders = fso.getfolder(FolderName)
datetoday = now()
newdate = dateadd("d", Days*-1, datetoday)
wscript.echo "Today:" & now()
wscript.echo "Started deleting files older than :" & newdate
wscript.echo "________________________________________________"
wscript.echo ""
recurse folders
wscript.echo ""
wscript.echo "Completed deleting files older than :" & newdate
wscript.echo "________________________________________________"

sub recurse( byref folders)
  set subfolders = folders.subfolders
  set files = folders.files

  for each folder in subfolders
    recurse folder
  next

  wscript.echo ""
  wscript.echo "Deleting Files under the Folder " & folders.path
  wscript.echo "__________________________________________________________________________"
  for each file in files
    if file.datelastmodified < newdate then
      wscript.echo "Deleting File " & folders.path & "\" & file.name & " last modified: " & file.datelastmodified
      on error resume next
      file.delete ' === to test this script but not actually delete files, comment out this line ===
    end if
  next

  if subfolders.Count = 0 And files.Count = 0 then
     wscript.echo "Deleting Folder " & folders.path
     folders.delete
  end if

  set subfolders = nothing
  set files = nothing

end sub
HTH.

Alessandro
archimede
Moderatore
 
Post: 2753
Iscritto il: 07/11/02 12:41
Località: Genova

Postdi evvivame » 09/08/13 19:01

Alessandro,
1. grazie!!!!
2. da un primo controllo SEMBRA che funzioni
3. per favore, lasciami qualche giorno di tempo per creare cartelle/files scaduti in modo da collaudare a dovere lo script
4. ti aggiornerò !!

Grazie ancora !!
Ciaooooo
Luciana
evvivame
Utente Senior
 
Post: 133
Iscritto il: 19/09/05 20:38

Postdi evvivame » 12/08/13 02:07

Alessandro,
Ti confermo che purtroppo non so niente di VB tuttavia mi permetto di farti una domanda:

Questa linea
Codice: Seleziona tutto
if subfolders.Count = 0 And files.Count = 0 then
si può cambiare in "nella cartella che sto esaminando c'è solo il file Thumbs.db ?" se si eliminala !!

Ovviamente hai capito il perchè te lo chiedo...

Buona settimana
evvivame
Utente Senior
 
Post: 133
Iscritto il: 19/09/05 20:38

Postdi evvivame » 12/08/13 03:26

p.s. ripensando è meglio dire:

if subfolders.Count = 0 And files.Count = 0 or c'è SOLO il file Thumbs.db ? then
evvivame
Utente Senior
 
Post: 133
Iscritto il: 19/09/05 20:38

Postdi archimede » 18/08/13 14:06

Se ti va bene cancellarlo sempre e comunque, puoi fare così:
Codice: Seleziona tutto
' Objective: To delete old files from a given folder and all subfolders below
'
' Created by: MAK June 21, 2005
'
' Format: cscript deloldfiles.vbs {DriveLetter:\FolderName} {#ofDays}
'     or: cscript deloldfiles.vbs {\\servername\FolderName} {#ofDays}
' Example: cscript deloldfiles.vbs c:\dba\log 3
'    (deletes files older than 3 days from the \dba\log file on drive C
Set objArgs = WScript.Arguments
FolderName =objArgs(0)
Days=objArgs(1)

set fso = createobject("scripting.filesystemobject")
set folders = fso.getfolder(FolderName)
datetoday = now()
newdate = dateadd("d", Days*-1, datetoday)
wscript.echo "Today:" & now()
wscript.echo "Started deleting files older than :" & newdate
wscript.echo "________________________________________________"
wscript.echo ""
recurse folders
wscript.echo ""
wscript.echo "Completed deleting files older than :" & newdate
wscript.echo "________________________________________________"

sub recurse(byref folders)
  set subfolders = folders.subfolders
  set files = folders.files

  for each folder in subfolders
    recurse folder
  next

  wscript.echo ""
  wscript.echo "Deleting Files under the Folder " & folders.path
  wscript.echo "__________________________________________________________________________"
  for each file in files
    if file.datelastmodified < newdate then
      wscript.echo "Deleting File " & folders.path & "\" & file.name & " last modified: " & file.datelastmodified
      on error resume next
      file.delete ' === to test this script but not actually delete files, comment out this line ===
    end if
  next

  on error resume next
  fso.deletefile(folders.path & "\Thumbs.db")
  if subfolders.Count = 0 and files.Count = 0 then
     wscript.echo "Deleting Folder " & folders.path
     folders.delete
  end if

  set subfolders = nothing
  set files = nothing

end sub
HTH.

Alessandro
archimede
Moderatore
 
Post: 2753
Iscritto il: 07/11/02 12:41
Località: Genova

Postdi evvivame » 19/08/13 21:40

ottimo !!
Grazie
evvivame
Utente Senior
 
Post: 133
Iscritto il: 19/09/05 20:38


Torna a Programmazione


Topic correlati a "VBScript: eliminare cartelle vuote":


Chi c’è in linea

Visitano il forum: Nessuno e 7 ospiti