115129
Goto Top

Ein Verzeichnis auf Minimal-Maximal-Anzahl Überwachen und Unterverzeichnisse löschen

Hallo zusammen,

ich benutze seit längerer Zeit folgendes Script um verschiedene Verzeichnisse auf eine minimal/maximal-Anzahl von Dateien zu überwachen und ggfls. zu löschen:

@echo off & setlocal

::ZU UEBERWACHENDE VERZEICHNISSE
Set "Dir_01=Y:\Backup"  
Set "Dir_01_min=7"  
Set "Dir_01_max=7"  

::ABARBEITUNG
if defined Dir_01 for /f %%i in ('dir /a-d /b "%Dir_01%\*.*"^|find /c /v ""') do if %%i gtr %Dir_01_max% (  
  for /f "skip=%Dir_01_min% delims=" %%j in ('dir /a-d /b /o-d "%Dir_01%\*.*"') do del "%Dir_01%\%%j"  
)

Exit

Nun soll das Script nicht die in dem angegebenen Verzeichnis (Y:\Backup) enthaltenen Dateien überwachen/löschen, sondern existierende Unterverzeichnisse.
z.B.: Y:\Backup\2016.07.23\ Y:\Backup\2016.07.24\ Y:\Backup\2016.07.25\ Y:\Backup\2016.07.26\

Kann mir jemand sagen was an dem bestehenden Script geändert werden müsste?

Vielen Dank.

Gruß,
Blattlaus

Content-Key: 313232

Url: https://administrator.de/contentid/313232

Printed on: April 23, 2024 at 17:04 o'clock

Member: atze187
atze187 Aug 23, 2016 at 07:12:31 (UTC)
Goto Top
Hi,

wenn es (wie ich vorbehaltlich des konkreten Anwendungsfalls vermute) um das Löschen von Dateien abhängig vom Zeitstempel geht, dann wäre das wohl ein Fall für delage32.

Gruß,
André
Mitglied: 129813
129813 Aug 23, 2016 at 08:17:47 (UTC)
Goto Top
@echo off & setlocal

::ZU UEBERWACHENDE VERZEICHNISSE
Set "Dir_01=Y:\Backup"  
Set "Dir_01_min=7"  
Set "Dir_01_max=7"  

::ABARBEITUNG
for /d %%a in ("%Dir_01%\*") do (  
  for /f %%i in ('dir /a-d /b "%%~a\*.*" ^|find /c /v ""') do if %%i gtr %Dir_01_max% (  
     for /f "skip=%Dir_01_min% delims=" %%j in ('dir /a-d /b /o-d "%%~a\*.*"') do del "%%~a\%%j"  
  )
)
Regards
Mitglied: 115129
115129 Aug 23, 2016 at 15:13:19 (UTC)
Goto Top
Hallo zusammen,

vielen Dank für Eure Hilfe ;)

Es scheint, als hätte ich mich nicht verständlich ausgedrückt...deshalb nochmal:

@echo off & setlocal

::ZU UEBERWACHENDE VERZEICHNISSE
Set "Dir_01=Y:\Backup"  
Set "Dir_01_min=7"  
Set "Dir_01_max=7"  

::ABARBEITUNG
if defined Dir_01 for /f %%i in ('dir /a-d /b "%Dir_01%\*.*"^|find /c /v ""') do if %%i gtr %Dir_01_max% (  
  for /f "skip=%Dir_01_min% delims=" %%j in ('dir /a-d /b /o-d "%Dir_01%\*.*"') do del "%Dir_01%\%%j"  
)

Exit


Das Skript soll das Verzeichnis "Y:\Backup" nach Unterverzeichnissen durchsuchen.
Werden z.B. "12" Ordner unter "Y:\Backup" entdeckt, sollen die ältesten Ordner bis auf "7" gelöscht werden.
Hinweis: Dateien sind egal, die Verzeichnisse unter "Y:\Backup" sollen gelöscht werden.

Hoffentlich habe ich mich nun besser ausgedrückt face-smile

Gruß,
Blattlaus
Mitglied: 129813
129813 Aug 24, 2016 at 08:55:54 (UTC)
Goto Top
@echo off & setlocal

::ZU UEBERWACHENDE VERZEICHNISSE
Set "Dir_01=Y:\Backup"  
Set "Dir_01_min=7"  
Set "Dir_01_max=7"  

::ABARBEITUNG
for /d %%a in ("%Dir_01%\*") do (  
  for /f %%i in ('dir /ad /b "%%~a" ^|find /c /v ""') do if %%i gtr %Dir_01_max% (  
     for /f "skip=%Dir_01_min% delims=" %%j in ('dir /b /ad /o-d "%%~a"') do rd "%%j" /s  
  )
)
Mitglied: 115129
115129 Aug 24, 2016 at 19:26:46 (UTC)
Goto Top
Hallo highload,

habe soeben deinen Ansatz getestet, allerdings ohne Erfolg. Habe als einziges den Pfad geändert.

@echo off & setlocal

::ZU UEBERWACHENDE VERZEICHNISSE
Set "Dir_01=Y:\_Server\Backup"  
Set "Dir_01_min=3"  
Set "Dir_01_max=7"  

::ABARBEITUNG
for /d %%a in ("%Dir_01%\*") do (  
  for /f %%i in ('dir /ad /b "%%~a" ^|find /c /v ""') do if %%i gtr %Dir_01_max% (  
     for /f "skip=%Dir_01_min% delims=" %%j in ('dir /b /ad /o-d "%%~a"') do rd "%%j" /s  
  )
)


Führe ich das oben aufgeführte Skript aus passiert gar nichts, auch wenn ich die erste Zeile ändere auf:

@echo on & setlocal

Wie erwähnt liegen in dem Verzeichnis "Y:\_Server\Backup" Backups die täglich erstellt werden.
Die Backupverzeichnisse heißen 2016.08.20, 2016.08.21, 2016.08.22, 2016.08.24, ... , diese Ordner sollen auf Min/Max überwacht und falls mehr als 7 Stück vorhanden bis auf 3 Stück gelöscht werden.

Vielen Dank für jegliche Unterstützung ;)

Gruß,
Blattlaus
Mitglied: 129813
129813 Aug 24, 2016 updated at 19:29:58 (UTC)
Goto Top
Here it works....it takes the modification date from the filesystem not from your folder names!
Mitglied: 115129
115129 Aug 24, 2016 at 19:46:28 (UTC)
Goto Top
Here it works....it takes the modification date from the filesystem not from your folder names!

Ok, i have create manually in "Y:\_Server\Backup" exactly 10 sub-dirs named "2016.08.20, 2016.08.21, 2016.08.22, 2016.08.24, ... ".

In this case the script should be delete 7 direcorys and let stay the last 3.

But it will nothing delete.

Whats wrong?

Greetings,
Blattlaus
Mitglied: 129813
129813 Aug 24, 2016 updated at 20:19:28 (UTC)
Goto Top
Arg, now I understand, sorry hopefully.
Note this takes the modification date of the folder, not the date of your folder names!
@echo off & setlocal

::ZU UEBERWACHENDE VERZEICHNISSE
Set "Dir_01=Y:\Backup"  
Set "Dir_01_min=3"  
Set "Dir_01_max=7"  

::ABARBEITUNG
for /f %%i in ('dir /ad /b "%Dir_01%\*" ^|find /c /v ""') do set "cnt=%%i"  
if %cnt% GTR %Dir_01_max% (
    for /f "skip=%Dir_01_min% delims=" %%j in ('dir /b /ad /o-d "%Dir_01%\*"') do rd "%Dir_01%\%%j" /s  
)
Mitglied: 115129
115129 Aug 24, 2016 updated at 20:54:58 (UTC)
Goto Top
Thanks, but it dont work...now i get follow error:

Die Datei "'dir /b /ad /o-d "Y:\_Server\Backup"" kann nicht gefunden werden.

My Drive:
Set "Dir_01=Y:\_Server\Backup"

Why the script search after a "file"? Inside "Y:\_Server\Backup" are subfolders named "2016.08.20, 2016.08.21, 2016.08.22, 2016.08.24, ... "
Mitglied: 129813
129813 Aug 25, 2016 updated at 06:31:14 (UTC)
Goto Top
You copied it wrong , works without problems. I suppose you missed a character somewhere.
Mitglied: 115129
115129 Aug 25, 2016 updated at 16:37:09 (UTC)
Goto Top
Hi highload,

ive try to copy and test the script again as follow:

@echo off & setlocal

::ZU UEBERWACHENDE VERZEICHNISSE
Set "Dir_01=Y:\_Server\Backup1"  
Set "Dir_01_min=3"  
Set "Dir_01_max=7"  

::ABARBEITUNG
for /f %%i in ('dir /ad /b "%Dir_01%\*" ^|find /c /v ""') do set "cnt=%%i"  
if %cnt% GTR %Dir_01_max% (
    for /f "skip=%Dir_01_min% delims=" %%j in ('dir /b /ad /o-d "%Dir_01%\*"') do rd "%%j" /s  
)
pause


Now i get a message to delete it. If i choose yes i get the follow error:
D:\_Scripte>´╗┐@echo off & setlocal
Der Befehl "´╗┐@echo" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.

D:\_Scripte>Set "Dir_01=Y:\_Server\Backup1"

D:\_Scripte>Set "Dir_01_min=3"

D:\_Scripte>Set "Dir_01_max=7"

D:\_Scripte>for /F %i in ('dir /ad /b "Y:\_Server\Backup1\*" |find /c /v ""') do
set "cnt=%i"

D:\_Scripte>set "cnt=10"

D:\_Scripte>if 10 GTR 7 (for /F "skip=3 delims=" %j in ('dir /b /ad /o-d "Y:\_Server\Backup1\*"') do rd "%j" /s )

D:\_Scripte>rd "2014.04.15" /s
Möchten Sie "2014.04.15" löschen (J/N)? j
Das System kann die angegebene Datei nicht finden.

D:\_Scripte>rd "2014.04.16" /s
Möchten Sie "2014.04.16" löschen (J/N)? j
Das System kann die angegebene Datei nicht finden.

D:\_Scripte>rd "2014.04.17" /s
Möchten Sie "2014.04.17" löschen (J/N)? j
Das System kann die angegebene Datei nicht finden.

D:\_Scripte>rd "2014.04.18" /s
Möchten Sie "2014.04.18" löschen (J/N)? j
Das System kann die angegebene Datei nicht finden.

D:\_Scripte>rd "2014.04.19" /s
Möchten Sie "2014.04.19" löschen (J/N)? j
Das System kann die angegebene Datei nicht finden.

D:\_Scripte>rd "2014.04.20" /s
Möchten Sie "2014.04.20" löschen (J/N)? j
Das System kann die angegebene Datei nicht finden.

D:\_Scripte>rd "2014.04.11" /s
Möchten Sie "2014.04.11" löschen (J/N)?


Any idea?

Greetings,
Blattlaus
Mitglied: 129813
Solution 129813 Aug 25, 2016 updated at 17:52:05 (UTC)
Goto Top
Yes,
you have saved your file as unicode... ;-P

And you should copy it with from the window behind the button Quelltext

And you have an error in line 11, see my code above.

that's much work with you face-confused my dear ...
Mitglied: 115129
115129 Aug 25, 2016 at 18:34:43 (UTC)
Goto Top
Yeahh my friend...now it works fine!

Thank you very much for your Patience :p