1

I am trying to make a batch file that deletes files of 0 bytes. I have the following code currently:

@echo off 
setLocal EnableDelayedExpansion 
for /f "tokens=* delims= " %%a in ('dir/s/b/a-d') do ( if %%~Za equ 0 del "%%~a" )

This is code is working fine by deleting files of 0 bytes, but only in the current directory of the batch file.

I want some enhancement with the code which deletes files of 0 bytes in different directory of the bat files current directory.

Please Help Me !

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

3 Answers3

1

This problem is easily solved by just changing to the directory you want to use before looping through the files:

@echo off 
setLocal EnableDelayedExpansion 
set CWD=%CD%
cd /d %1
for /f "tokens=* delims= " %%a in ('dir/s/b/a-d') do ( if %%~Za equ 0 del "%%~a" )
cd /d %CWD%

(See also How do I find the current directory of a batch file, and then use it for the path? for information on changing directories).

Now you mention "only in the current directory of the batch file". This is what would happen if you clicked on the batch file from Windows Explorer rather than called it from a command prompt. Is that what you are doing? If you wanted the batch file to ask the user for the directory to use, rather than have it as an argument, as I did in earlier code, you'll have to put code in for that also:

@echo off 
setLocal EnableDelayedExpansion 
set CWD=%CD%
set /p D=What directory should I clean?
cd /d %D%
for /f "tokens=* delims= " %%a in ('dir/s/b/a-d') do ( if %%~Za equ 0 del "%%~a" )
cd /d %CWD%
Community
  • 1
  • 1
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
0

You could include the target directoryname directly in the command:

for /f "delims=" %%a in ('dir/s/b/a-d "d:\your directory\name\wherever\*.*"') do ( if %%~Za equ 0 ECHO del "%%~a" )

Notes:

There should be no space between the = and " in the delims setting. If you include a space there, then if there is a file named file name.txt, file will be assigned to %%a. Since the output of dir /s/b contains the path, an absolute filename d:\some dir\file name.txt will assign d:\some to %%a and act on that file, even if it is outside of the subtree you intend to scan.

tokens=* simply deletes leading spaces, which won't occur with dir/s/b so is redundant.

I added in an echo to show the filenames which would be deleted. This is a "safe" option - show the names first, so that if there is an error no damage is done. Once you are happy the correct selection has been made, remove the echo keyword to actually delete the files.

Another (but similar) way is

set "targetdir=d:\your directory\name\wherever"
for /f "delims=" %%a in ('dir/s/b/a-d "%targetdir%\*.*"') do ( if %%~Za equ 0 ECHO del "%%~a" )

where targetdir is simply a convenient descriptive name. This is more flexible since you can use code to construct the value in targetdir if you so desire.

Of course, *.* is simply a filemask for all files. If you want to process only the .txt files, then replace *.* with *.txt

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

I've never had good luck with FOR as many people suggest, I usually use FORFILES:

@echo off
FORFILES /P "C:\Path" /C "CMD /C IF @FSIZE==0 del @FILE"
m0chi
  • 158
  • 1
  • 3
  • 13