-1

I am trying to write a batch file that allows me to delete all the files in a specific folder which contains only one line. I can count the lines, but then I cannot use it to delete those files.

Can you please help?

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • 3
    Post the code you've wrote to do exactly the task you've outlined, and fully explain what issues you're facing in running it. – Compo Oct 31 '18 at 11:41

1 Answers1

0

I think, you need a batch file like this:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "eol=| delims=" %%I in ('dir "C:\Temp\*" /A-D-H /B 2^>nul') do call :DeleteFile "C:\Temp\%%I"
endlocal
goto :EOF

:DeleteFile
for /F "usebackq skip=1 eol=" %%J in (%1) do goto :EOF
del /F %1
goto :EOF

The first FOR executes in a separate command process started with cmd.exe /C in background the command line

dir "C:\Temp\*" /A-D-H /B 2>nul

The command DIR lists to STDOUT of separate command process

  • the file names of all non-hidden files in specified directory C:\Temp because of /A-D-H
  • in bare format with just file name and file extension because of /B
  • matching the simple wildcard pattern *.

DIR outputs an error message to STDERR redirected with 2>nul to device NUL to suppress it if no directory entry is found matching these requirements.

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 captures everything output to STDOUT of separate command process and process the captured text line by line.

Empty lines are ignored by FOR which do not occur here except no matching file name was found by DIR.

FOR ignores by default on using /F all lines starting with a semicolon. A file name without path can start with ; and therefore eol=| is used to redefine the end of line character to a vertical bar which a file name cannot contain.

FOR splits up a line by default into substrings (tokens) using space/tab as delimiters and assigns just first space/tab delimited string to specified loop variable I. This line splitting behavior is disabled by delims= which defines an empty list of delimiters.

So the entire file name is assigned to loop variable I which is concatenated with specified folder path to a full qualified file name and passed to subroutine DeleteFile.

The command FOR in subroutine DeleteFile processes the lines in the file of which full qualified file name is passed in double quotes to the subroutine.

The FOR option usebackq is needed because of full qualified file name in double quotes should not be interpreted as string to process by FOR, but as file name of which lines should be processed by FOR.

The FOR option skip=1 is used to always skip the first line of the specified file.

The FOR option eol= defines no end of line character which is required to avoid that a line starting with ; is ignored by FOR.

Normal space and horizontal tab are kept as delimiters as defined by default.

So FOR opens the file and skips the first line independent on what this line contains. Then it processes the remaining lines with skipping empty lines and lines containing only spaces/tabs. On a line after first line containing a character not being a normal space or horizontal tab the command goto :EOF is executed resulting in exiting the FOR loop and the subroutine DeleteFile.

For that reason the command DEL after the FOR loop is executed only if the file does not contain any line at all, or just one line, or one line at top and just multiple empty or blank lines below.

After deletion of the file the subroutine is exited and batch file execution continues on first FOR with next file name.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • del /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • setlocal /?

See also Where does GOTO :EOF return to?

Mofi
  • 46,139
  • 17
  • 80
  • 143