Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi all I was asked to write this batch file to maintain certain folder of its files. But I'm not very experienced in scripting, there might be different 'sets' of files and to delete the old files of certain set to a desired number of files of that set
Type1a.abc
Type1b.abc
Type1c.abc
Type2a.abc
Type2b.abc
Type2c.abc
Type3a.cde
Type3b.cde
Type3c.cdeSo when calling the batch file, the first parameter is to allow the directory, the second is the number of files desired to keep maintained and lastly the type.
So this is what I have right now, it doesn't do exactly its suppose to I'm at a bottleneck right now and need some help.
REM @Echo off
If not exist %1 .\Nul GoTo ERROR:CHECK
Dir %1 /A:-D-H /B /l | Find /C %3> %Temp%.\%0.tmp
For /F "tokens=1" %%I in (%temp%.\%0.tmp) Do Set NoFiles=%%IEcho Folder %1 contains %NoFiles% Files
IF %NoFiles% GEQ %2 GOTO DELOLDEST
GOTO EXIT:DELOLDEST
for /f "delims=" %%a in ('dir %1 /a-d /od /b /l | Find %3') do set Oldest=%%a
echo Oldest=%Oldest%
Del %1\"%oldest%"
Goto CHECK:ERROR
Echo Folder %1 missing or not found
Goto EXIT:EXIT
If exist %temp%.\%0.tmp Del %temp%.\%0.tmpCurrently there is | unexpected error and I don't know other alternative to this
For example in C:\Somewhere\Test Folder\ I haveNew Text Document.txt
New Text Document(2).txt
New Text Document(3).txt
New Text Document(4).txt
New Bitmap Image.bmp
New Bitmap Image(2).bmp
New Bitmap Image(3).bmp
New Bitmap Image(4).bmpI wanna keep only 3 text file and 2 bitmap file
I would call the script twice
>Script "C:\Somewhere\Test Folder" 3 "new next"
>Script "C:\Somewhere\Test Folder" 2 "new bitmap"something like that. any better suggestions on the method is also appreciated. thank you
SchizophrenicX
"I'm not psychic; I'm just damn good"

To fix the | unexpected error, change this:
:DELOLDEST
for /f "delims=" %%a in ('dir %1 /a-d /od /b /l | Find %3') do set Oldest=%%a
echo Oldest=%Oldest%To:
:DELOLDEST
for /f "delims=" %%a in ('dir %1 /a-d /od /b /l ^| Find %3') do set Oldest=%%a
echo Oldest=%Oldest%You have to escape the | with a caret.

Wow !! Such a newbie mistake!! I was so frustrated I thought it was not possible to achieve what I wanted with my method of approach, thanks a bunch tonysathre after the correction the script worked PERFECTLY. Just one small glitch that I was thinking of correcting would be the 3rd parameter, in my code it has to be all lowercase (/l switches in dir) is it possible for non-case-sensitive?
SchizophrenicX
"I'm not psychic; I'm just damn good"

Wow thanks that worked perfectly :P~ I'm still kinda green on scripting. Thanks to this community I'm picking up quite fast. :D
SchizophrenicX
"I'm not psychic; I'm just damn good"

![]() |
help me!!!!
|
Change target type of a s...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |