Home > Blockchain >  Script to delete file 0 size in folder but not in subfolder
Script to delete file 0 size in folder but not in subfolder

Time:08-24

I have a script to delete 0 sized txt files inside folder but when i tried to running it, it will delete 0 sized files in subfolders too. here's the script

for /r "\filepath" %F in (*.txt) do @if %~zF==0 del "%F"

How i can delete the files without delete another files in subfolders too?

CodePudding user response:

for %F in ("\filepath\*.txt") do @if %~zF==0 del "%F"

The /r forces a scan of the tree.

CodePudding user response:

for files in `ls *.txt`
do
  if [ -s $files ]; then
    echo "$files is not empty"
  else
    rm -f $files
  fi
done

-s $files expression would be True if the file in variable $files exists with a non-zero size. The else statement will therefore be activated if the file is empty. Here given the for loop is passing the names of the files in the current directory, it is assuming that the file exists and the only check that needs to be made is whether it is empty or not. Also, given the ls *.txt will list only the files of current directory, it won't process any subfolder.

In your script, skip /r (as it recursively processes all folders and subfolders).

  • Related