Computing.Net > Forums > Programming > Script to Maintain Files in Folders

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Script to Maintain Files in Folders

Reply to Message Icon

Name: SchizophrenicX
Date: August 4, 2008 at 00:46:26 Pacific
OS: Windows XP Pro 2002 SP 2
CPU/Ram: Inter(R) Pentrium(R) M Pr
Product: IBM
Comment:

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.cde

So 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=%%I

Echo 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.tmp

Currently there is | unexpected error and I don't know other alternative to this


For example in C:\Somewhere\Test Folder\ I have

New 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).bmp

I 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"



Sponsored Link
Ads by Google

Response Number 1
Name: tonysathre
Date: August 4, 2008 at 16:31:40 Pacific
Reply:

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.


0

Response Number 2
Name: SchizophrenicX
Date: August 4, 2008 at 23:24:34 Pacific
Reply:

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"


0

Response Number 3
Name: tonysathre
Date: August 8, 2008 at 15:24:10 Pacific
Reply:

You mean in the find command? You can use findstr /i instead for non case-sensitivity.


0

Response Number 4
Name: SchizophrenicX
Date: August 22, 2008 at 02:46:30 Pacific
Reply:

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"


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


help me!!!! Change target type of a s...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Script to Maintain Files in Folders

Script to merge files in one www.computing.net/answers/programming/script-to-merge-files-in-one/17116.html

script to delete files after ftp www.computing.net/answers/programming/script-to-delete-files-after-ftp/18347.html

batchRenaming files in Folder tree www.computing.net/answers/programming/batchrenaming-files-in-folder-tree-/18621.html