简体   繁体   中英

Batch script to find an empty file in a folder

I have a requirement to identify a zero KB file in a folder and write the output to a text file. Below is the code I found by using batch script and I would like to customize the same as per my below requirement.

@echo off
set out=found.txt
(for /r c:\myfolderfilestosearch %%F in (*.txt) do (if %%~zF LSS 1 echo %%F)) > %out%
pause

I would actually want to create & write the result to file if and only if the above folder has any of the 0kb files only, but my above script creates a txt file for every instance even if there are no 0KB files.

I think we can implement it by using if else but as I said I am new and appreciate if someone guides on it with a script.

PS I am fine to have a script written in powershell as well.Thanks.

The thing is: the redirection doesn't care, if you are actually writing data. It is creating the file anyways. But you can get rid of it afterwards, if it's size is zero (no data written)

@echo off
set out=found.txt
(for /r c:\myfolderfilestosearch %%F in (*.*) do if %%~zF LSS 1 echo %%F) > %out%
for %%a in (%out%) do if %%~za == 0 del %out% 
pause

(I would prefer == 0 or equ 0 instead of lss )

PowerShell

Get-ChildItem -Path C:\myfolderfilestsearch -Recurse -Force | Where-Object { $_.PSIsContainer -eq $false -and $_.Length -eq 0 } | Select -ExpandProperty FullName | Add-Content -Path found.txt

To recreate the output file for each run:

(Get-ChildItem -Path C:\myfolderfilestsearch -Recurse -Force | Where-Object { $_.PSIsContainer -eq $false -and $_.Length -eq 0 } | Select -ExpandProperty FullName) | Set-Content -Path found.txt

You have too many unnecessary parens, and you don't need /r . This works on my machine:

@echo off
set out=found.txt
for %%F in (c:\myfolderfilestosearch\*.*) do if %%~zF LSS 1 echo %%F >> %out%
pause

Note that if you don't specify a specific folder location for found.txt , it will be in whatever folder the batch file itself is located. If you don't want that, either specify a path for that file or change into that folder using cd in the batch file:

@echo off
cd /d "C:\myfolderfilestosearch"
set out=found.txt
for %%F in (*.*) do if %%~zF LSS 1 echo %%F >> %out%
pause

or

@echo off
set out="C:\myfolderfilestosearch\found.txt"
for %%F in (c:\myfolderfilestosearch\*.*) do if %%~zF LSS 1 echo %%F >> %out%
pause

Edit: Use >> to append to the output file.

Collecting output and delaying the "write to file" part isn't that simple in batch. It's easier to write the output to a file regardless, and delete the file if you encounter a file with non-zero size.

@echo off

set "out=found.txt"
set "allzero=y"

(for /r C:\myfolderfilestosearch %%F in (*.*) do (
  if %%~zF gtr 0 (
    set "allzero=n"
    goto :done
  )
  echo %%F
))>"%out%"

:done
if "%allzero%"=="n" del /q "%out%"

In PowerShell you might do it like this:

$allzero = $true
$files = Get-ChildItem C:\myfolderfilestosearch -File -Recurse -Force |
         ForEach-Object {
           if ($_.Length -gt 0) {
             $allzero = $false
             continue
           }
           $_.FullName
         }

if ($allzero) {
  $files | Set-Content 'found.txt'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM