[Solved] All but the 1st 2 files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DynV
Posts: 3
Joined: 04 Aug 2013 21:24

[Solved] All but the 1st 2 files

#1 Post by DynV » 04 Aug 2013 21:54

For a batch file, I'd like to do a dir in a sub-directory of %APPDATA% using a pattern on file names which will be different each time. IMHO the dir options (ie /on) nor the directory name neither the pattern doesn't need to be specified to get the help. I'd like to do an operation on all but the first 2 files listed by the dir. The end/final operation in question is del.

I don't think the end operation matter as I doubt it has an option to do what I want : skip the 1st 2 files (should there be more than 2). I'd likely have to do some kind of loop ; 1st creating a counter incrementing it each iteration, in the iteration a verification would be made if the counter is greater than 2 (hopefully a constant can be used instead of 2), if passed the end command would be applied to the file name. That of course is just a way to do it, and there could be better ones depending on the availability of (complex) commands/scripts.

Here's an example:
dir /b OPT2 DIR1
fileA
fileB
fileG
fileH
fileT

and as the 1st 2 are fileA & fileB, the is a del for each of the following files ; so del fileG, del fileH & del fileT.
Last edited by DynV on 05 Aug 2013 02:11, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: All but the 1st 2 files

#2 Post by foxidrive » 04 Aug 2013 23:33

Make sure that c:\folder is the right location and you haven't made a typo. test it well and then remove the echo keyword

Code: Select all

@echo off
cd /d "c:\folder" && for /f "skip=2 delims=" %%a in ('dir /b OPT2 DIR1') do echo del "%%a"

DynV
Posts: 3
Joined: 04 Aug 2013 21:24

Re: [Solved] All but the 1st 2 files

#3 Post by DynV » 05 Aug 2013 02:16

As you may see from the post subject, I marked the thread as solved. Thank you kindly for your help with that. There's a small issue left but it's not so important.

Outside the deletes mentioned up to here, I have a "normal" delete which will likely function adequately most times I run the batch file but sometimes no file will match the pattern and it will give an error with the following message: Could Not Find DIR1\PATTERN1. Is there a way to 1st check if a file match the pattern? I hoped del itself had something but it doesn't look like it.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: [Solved] All but the 1st 2 files

#4 Post by Squashman » 05 Aug 2013 05:45

redirect standard error to nul and you won't see the error.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: [Solved] All but the 1st 2 files

#5 Post by foxidrive » 05 Aug 2013 06:52

Either of these will stop the message:

Code: Select all

If exist "DIR1\PATTERN1" del "DIR1\PATTERN1"


Code: Select all

del "DIR1\PATTERN1" 2>nul

DynV
Posts: 3
Joined: 04 Aug 2013 21:24

Re: [Solved] All but the 1st 2 files

#6 Post by DynV » 05 Aug 2013 08:43

I love that if exist trick. I doesn't mess the exit code. :)

Thanks!

Post Reply