Reputation: 33
I need to search all directories below the batch file for any folders called History
and then delete all *.zip files in those directories.
I have a batch file that currently deletes all files with 2 numbers and a $ symbol using the following code:
For /F "delims=" %%A in (
'Dir /B/S "*,*" ^| findstr ",[0-9][0-9]*$" '
) Do Del "%%A"
But what about all zip files that are ONLY in a History
folder.
Upvotes: 1
Views: 348
Reputation: 49086
The following command line could be used in a batch file to delete all *.zip files in all directories with name History
found in current directory and its subdirectories:
@for /F "delims=" %%I in ('dir History /AD /B /S 2^>nul') do @del /A /F /Q "%%I\*.zip" 2>nul
The command FOR starts one more command process in background with %ComSpec% /c
and the command line specified between the two '
which results in execution of the following command line on Windows being installed in C:\Windows
:
C:\Windows\System32\cmd.exe /c dir History /AD /B /S 2>nul
The background Windows command process runs internal command DIR which
/S
/AD
(attribute directory)History
and/B
line by line which means just the names of the found directories with full path because of option /S
to handle STDOUT of the background command process.The full qualified directory names are output by DIR never enclosed in double quotes even on containing a space or one of these characters &()[]{}^=;!'+,`~
which require the usage of double quotes around full qualified name on passing it as argument string to a command or application.
It is possible that DIR cannot find any directory matching the search criteria in file system. In this case DIR outputs to handle STDERR of background command process an error message which is a bit confusing for a reader because it contains the word file
and not the word directory
or folder
. This error message on no directory History
found by DIR is suppressed by redirecting it to device NUL.
Read the Microsoft article about Using command redirection operators for an explanation of 2>nul
. The redirection operator >
must be escaped with caret character ^
on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir
command line with using a separate command process started in background.
FOR with option /F
processes the captured output written to handle STDOUT of background command process line by line after started cmd.exe
terminated itself. The text output to handle STDERR of background command process is redirected by FOR to handle STDERR of current command process which is processing the batch file and would be displayed in the console window on running this batch file without 2^>nul
.
Empty lines are always ignored by FOR on processing captured output. But there are no empty lines in this case because of DIR outputs either nothing or the found directories with full path line by line with no empty lines between.
FOR with option /F
would split up a line by default into substrings using normal space and horizontal tab characters as string delimiters and would assign just first space/tab delimited string to loop variable I
. This string splitting behavior is not wanted here because of path to a History
directory could contain one or more spaces. For that reason delims=
specifies an empty list of delimiters which disable line splitting behavior completely and assigned to loop variable I
is the full qualified directory name as output by DIR.
FOR with option /F
would ignore also the current line if the first substring starts after line splitting with a semicolon because of eol=;
is the default for end of line option. This default can be kept here because of a full qualified directory name starts either with a drive letter or with a backslash (UNC path) and therefore starts definitely never with a semicolon. So every directory name output with full path by DIR is processed by FOR and assigned without any modification one after the other to loop variable I
.
The command DEL is executed for each full qualified directory name assigned to loop variable I
never enclosed in double quotes to delete all files matching the wildcard pattern *.zip
in this directory. The full qualified directory name concatenated with the string \*.zip
must be enclosed in double quotes to be correct passed to command DEL on containing a space or another character special interpreted by Windows command processor.
The DEL option /A
is used to override default /A-H
to delete also ZIP files with hidden attribute set which otherwise would be ignored by DEL.
The DEL option /F
is used to force a deletion of read-only ZIP files.
The DEL option /Q
is used to delete the ZIP files quietly without prompt the user for confirmation.
The command DEL could fail to delete ZIP files, for example if a ZIP file to delete is opened currently by another process or if the user has not the required permissions to delete a ZIP file in a specific directory. In this case the command DEL would output an error message to handle STDERR. Such an error message output by DEL is suppressed by redirecting it to device NUL.
The two @
in the command line just disable output of the FOR command line after parsing by Windows command processor before execution and of the DEL command line before execution on each iteration of the loop. The two @
are not needed if the batch file contains at top @echo off
to disable command line output after parsing before execution for all command lines in batch file.
Help on the three used commands is output on running them with parameter /?
in a Windows command prompt window, i.e. executing for /?
and dir /?
and del /?
.
See also: How does the Windows Command Interpreter (CMD.EXE) parse scripts?
Upvotes: 1