You are not logged in.

#1 04 Mar 2007 13:26

jduff
Member
Registered: 04 Mar 2007
Posts: 9

Delete All Files Not Named log.*

I'm trying to delete all files in a directory that are not named log.*.
I've tried the following:

    for %%varFile in ("%myDir%\*") do call deleteNonLogs %%varFile

where deleteNonLogs.bat contains:

    set filename="%1"
    echo %filename% | find "log." > nul
    if not %ERRORLEVEL%==0 goto endDeleteNonLogs
    del "%filename%"
    :endDeleteNonLogs

The above approach does NOT work! deleteNonLogs.bat is called only once and then the script(s) terminate.

Is there a better way to do this? Can this be done without a second batch file, i.e., without the deleteNonLogs.bat. If I need the second batch file, how can I make the call happen repeatedly?

Thanks in advance for any help.
jduff

Offline

#2 05 Mar 2007 21:36

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: Delete All Files Not Named log.*

OK I may be misunderstanding what youre trying to achieve here, but it looks like this might do the trick

:: rename the log files with a .keep extension
REN *log*.* *.keep

:: delete all the .txt files
del *.txt

:: rename back to .txt
REN *.keep *.txt

If you do want to use a FOR loop you can use a CALL statement and a subroutine - see the CALL page

Offline

#3 06 Mar 2007 00:09

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Delete All Files Not Named log.*

Simon ... I not sure your code is going to have the desired effect.  It rather depends on the exact nature of the files in the directory.
jduff ... I think I can see what you're trying to do there, but there are errors in your scripting.  I won't pick through it, but based on what I think you're after, here's how I'd do it:

@echo off

setlocal enabledelayedexpansion

set dir=Z:\whatever

for %%x in ("%dir%\*") do (
    REM get just the file name and extension
    set working=%%~nxx

    REM delete the current file if first 4 characters of the filename aren't "log."
    if /i "!working:~0,4!" NEQ "log." del "%%~x"
)

N.B. - the above code would delete a file called simply "log" -- but you did imply that all the files you don't want to get rid of have an extension, hence my coding it this way.
If you wanted to keep a file called "log" you could tweak the last line of code thus:

if /i "%%~x" NEQ "log" if /i "!working:~0,4!" NEQ "log." del "%%~x"

Last edited by bluesxman (06 Mar 2007 00:10)


cmd | *sh | ruby | chef

Offline

#4 06 Mar 2007 06:22

jduff
Member
Registered: 04 Mar 2007
Posts: 9

Re: Delete All Files Not Named log.*

Yes, thank you! Bluesxman's code works. I needed to learn about the enabledelayedexpansion, ~n, ~nx and the !'s. Your code led me to read the relevant help text.

FYI, after learning from you and doing some thought on my own, I was able to shorten the code to the following:

set dir=Z:\whatever

for %%x in ("%dir%\*") do (    
    if /i "%%~nx" NEQ "log" del %%x
)

jduff

Offline

#5 06 Mar 2007 16:21

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Delete All Files Not Named log.*

Yeah that's one of the ways I considered doing it; however, if you happen to have a file called "log.something.blah" then the extra dot will cause a problem as "%%~nx" would expand to "log.something".


cmd | *sh | ruby | chef

Offline

Board footer

Powered by