0

I am using below code to delete .txt files not starting "S":

del [^S]*.txt

but its not working.

How this can be achieved?

Mark Karpov
  • 7,499
  • 2
  • 27
  • 62

3 Answers3

2

Why not just move S*.txt to other folder, delete all, and then move back?

move S*.txt somefolder\
del *.txt
move somefolder\S*.txt .\
Landys
  • 7,169
  • 3
  • 25
  • 34
0

This isn't pretty but you can:

  1. Construct a for loop
  2. Tell for to iterate over the output of...
  3. A bare (/B) dir listing of the files piped to find with /V "S" (which excludes S).
  4. Tell for to call del on each item it processes. Use /f to avoid the need for del confirmation.

Here's an example:

for /f %F in ('dir S*.txt /B') do del %F /f

This does not address recursive searches or anything more advanced than the stated question.

UPDATE: I should have just said 'Use Powershell if you can'. Command prompt is a horrible shell:

gci -Filter S*.txt -recurse | rm
miken32
  • 42,008
  • 16
  • 111
  • 154
Will
  • 453
  • 4
  • 8
  • And I'm not sure how to indicate this but the question should probably be on Server Fault instead of Stack Overflow. – Will Sep 03 '14 at 18:33
  • Your code will filter out any file containing an S anywhere in the filename or extension, and doesn't exclude folders. It also fails on names with spaces. – foxidrive Sep 04 '14 at 07:26
  • Update to correct some of the issues posed by @foxidrive. This still doesn't address files with spaces. – Will Sep 04 '14 at 15:09
  • Welcome to Stack Overflow. You can use `"delims="` to deal with spaces plus enclosing the last `%F` in double quotes, and also add `/a-d` in the `dir` command to exclude folders. – foxidrive Sep 04 '14 at 15:11
0

For a windows Vista or later os, try this

@echo off

    setlocal enableextensions disabledelayedexpansion

    set "targetFolder=%cd%"

    set "tempFolder=%temp%\%~nx0.%random%.tmp"
    md "%tempFolder%" >nul 2>nul
    robocopy "%tempFolder%" "%targetFolder%" /nocopy /purge /xf s* /l
    rd "%tempFolder%" >nul 2>nul

This creates an empty folder that is used as source for a purge of the target folder, that is, files that does not exist in source and exist in target are deleted. As the source is empty, all the files are deleted, but the /xf s* indicates to robocopy that files starting with a s should be excluded. The ending /l switch is included to only list the files that will be deleted. If the output is correct, remove it.

For versions of windows without robocopy, this can be used.

@echo off    
    setlocal enableextensions disabledelayedexpansion

    set "targetFolder=%cd%"

    for /f "delims=" %%a in ('
        dir /b /a-d "%targetFolder%" ^| findstr /i /v /b /c:"s"
    ') do echo del "%%~fa" 

It is less efficient as a del command will be executed for each of the files to be deleted. The idea is to retrieve the list of all files with a dir command and use findstr to, ignoring case (/i) filter to only retrieve the list of elements that does not include (/v) at the beginning of the line (/b) the string S (/c:"s")

del commands are only echoed to console. If the output is correct, remove the echo command

MC ND
  • 69,615
  • 8
  • 84
  • 126