-1

I have a batch file which uses a For /F loop to find all files with a specific (cadence backup uses <filename.ext,1> for backup) setup.

For /F "delims=" %%A in ( 'Dir /B/S "*,*" ^| findstr ",[0-9][0-9]*$" ' ) Do Del "%%A"

and this works for this special setup (This is a special search for an unusual file name format of ABCDE.ZYX,1).

How do I reuse the findstr to find all .bak files as a second search string?

Separate question:

How does this line ( 'Dir /B/S "*,*" ^| findstr ",[0-9][0-9]*$" ' ) work? What is it doing? Please explain the functions and attributes.

tmd63
  • 21
  • 4
  • `*,*`? Are you sure, you don't want `*.*`? – Stephan May 25 '21 at 11:29
  • 1
    `dir /b /s *.bak` for all `.bak` files, `dir /s /b *.bak *.zip` for all `.bak` *and* all `*.zip` files. – Stephan May 25 '21 at 11:30
  • 1
    `findstr ",[0-9][0-9]*$"` filters the ouput of the `dir` command to lines that end with a comma plus one or more digits. See `findstr /?` for more info. – Stephan May 25 '21 at 11:33
  • Hi Stephan. No, I have a number of files to delete and there is one type that has the form ABCDE.XYZ,1 and ABCDE.XYZ,2 which I need to find and delete. But as this line works for those files but I also need to add other files like all .bak files. – tmd63 May 25 '21 at 13:59
  • I'm not understanding why you're asking what it is doing, when you've already told us it appears to work. If you don't know what it is doing, how do you know that it appears to work? Open a Command Prompt window, type `dir /?`, and press the `[ENTER]` key, to learn how that command works. Next do the same with `findstr /?`, `for /?`, and finally `del /?`. Your command is deleting all files with no extension and which the end user has permission to delete, located in the tree of the current working directory, and which have names ending with a comma, followed by one or more number characters. – Compo May 25 '21 at 14:24
  • Hi Compo. The line works fine for what it is doing. I want to duplicate the line and modify it to do a different delete! I want a separate line in my batch file to delete all .bak files and one for all .zip files so I can manage each type separately. I am also trying to understand what the original code line was doing to better understand coding! – tmd63 May 25 '21 at 16:44
  • Your question asks to `find all .bak files or all .zip files **instead**`, but in a comment, you said `I **also** need to **add** other files like all .bak`. Which is it? – Stephan May 26 '21 at 16:28
  • `dir /b /a-d * |findstr /re ",[0-9][0-9]* \.bak \.zip"` might be what you're after. – Stephan May 26 '21 at 16:32
  • Thank you Stephan, I have removed the **instead** and **also** parts of the question to clarify what I was asking, as IP is an issue and I did not want to give too much away at the time. – tmd63 Jun 11 '21 at 09:20

1 Answers1

0

My Working Solution. Thanks to those who explained the functions involved.

@ECHO off
set cadencedeleted=0
set autodeleted=0
set altiumdeleted=0

ECHO Cadence Back-up Deletion In Progress
For /F "delims=" %%A in ( 'Dir /B/S "*,*" ^| findstr ",[0-9][0-9]*$" ' ) Do (
    Del "%%A"
    set cadencedeleted=1
)

ECHO AutoCAD Back-up Deletion In Progress
For /F "delims=" %%A in ( 'Dir /B/S "*.bak" 2^>nul ' ) Do (
    Del "%%A"
    set autodeleted=1
)
ECHO Altium Back-up Deletion In Progress
ECHO Note Altium *.pcbdoc.zip, *.schdoc.zip, *.outjob.zip, *.log,
ECHO *.prjpcb.zip, *.schlib.zip, *.pcblib.zip are the only ones deleted

For /F "delims=" %%A in ( 'Dir /B/S "*.pcbdoc.zip" 2^>nul ' ) Do (
    Del "%%A"
    set altiumdeleted=1
)

For /F "delims=" %%A in ( 'Dir /B/S "*.SchDoc.zip" 2^>nul ' ) Do (
    Del "%%A"
    set altiumdeleted=1
)

For /F "delims=" %%A in ( 'Dir /B/S "*.OutJob.zip" 2^>nul ' ) Do (
    Del "%%A"
    set altiumdeleted=1
)

For /F "delims=" %%A in ( 'Dir /B/S "*.log" 2^>nul ' ) Do (
    Del "%%A"
    set altiumdeleted=1
)

For /F "delims=" %%A in ( 'Dir /B/S "*.PcbLib.zip" 2^>nul ' ) Do (
    Del "%%A"
    set altiumdeleted=1
)

For /F "delims=" %%A in ( 'Dir /B/S "*.SchLib.zip" 2^>nul ' ) Do (
    Del "%%A"
    set altiumdeleted=1
)

For /F "delims=" %%A in ( 'Dir /B/S "*.prjpcb.zip" 2^>nul ' ) Do (
    Del "%%A"
    set altiumdeleted=1
)
ECHO Deletion checks

IF %cadencedeleted%==1 (
    ECHO Cadence backup files deleted
) ELSE (
    ECHO Cadence backup files not found
)

IF %autodeleted%==1 (
    ECHO AutoCAD backup files deleted
) ELSE (
    ECHO AutoCAD backup files not found
)

IF %altiumdeleted%==1 (
    ECHO Altium Backup files deleted
) ELSE (
    ECHO Altium Backup files not found
)

ECHO on
TIMEOUT 5
tmd63
  • 21
  • 4