Delete Files With Loop

Microsoft Windows vista business w/sp1
April 14, 2010 at 10:41:37
Specs: Windows Vista
I need to delete a single file from 16 computers. While I could create a batch file that has a DEL command for all 16, I would like to learn how to loop through such a task. The computers are named sequentially, i.e. PC1, PC2...PC16.

I have limited experience with CMD syntax, but I would imagine I need something like this:

Foreach number IN (Numbers1-16.txt) DO (DEL \\PC%Number%\C$\FileToBeDeleted)

As far as I know, batch files don't support Do/While loops with a counter...or do they? I can envision a loop such as "DO (Del \\PC%i%\File) WHILE i<17, i++" but I don't know if/how that would work.

Is this possible, and if so, what would be the correct way of doing it?

Many thanks!

See More: Delete Files With Loop

Report •

April 14, 2010 at 10:53:37
FOR /L %%a IN (1,1,16) DO DEL "\\PC%%a\C$\FileToBeDeleted"

Report •

April 14, 2010 at 11:24:17
Thank you very much Razor! That did the trick perfectly. The only issue I encountered was the inclusion of a leading 0 in the single digit computers (which I forgot to mention in my original post).

Not sure if it's the best practice, but I got around that by making two lines in the batch file -

(1,1,9)... PC0 %%a ...
(10,1,16)... PC %%a ...

Thank you again!

Report •
Related Solutions

Ask Question