2

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.

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

5
  • @Joey Thanks for editing the post Joey! Commented Oct 19, 2016 at 19:14
  • 1
    for %%a in (%out%) do if %%~za == 0 del %out% (I would use == 0 or equ 0 instead of lss 1) Commented Oct 19, 2016 at 19:32
  • You want to write the result only if the folder has some files with 0kb size, or if the folder only has 0kb files and no other files? What's with the "batch script, new to batch script, here's my batch script" - tag [powershell]? Commented Oct 19, 2016 at 19:34
  • @TessellatingHeckler Hello, yes I want to write the output to a file only when any of the files in the folder contains 0kb size and the folder can have "n" number of files with various sizes, but I am interested to write the file if any of the files are of 0kb size. To be more precise, I am looking for this solution either from batch script or powershell script anything will suffice. Hope I answered. Commented Oct 19, 2016 at 19:44
  • What exactly is "the result"? is it the path/name of the first empty file in the each directory, or should it be the respective directory path/name? Commented Oct 19, 2016 at 20:44

4 Answers 4

5

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
Sign up to request clarification or add additional context in comments.

2 Comments

The above is useful and working fine, but when I rerun the powershell script I can see data being duplicated. Instead, is it possible to recreate the file everytime when I rerun the powershell script?
updated with an example. parentheses are added so that all data generated by the set of commands within them is passed to Set-Content only once. Replaced Add-Content with Set-Content.
1

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)

Comments

0

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.

6 Comments

actually the parantheses arount the complete for loop are neccessary to not overwrite the outfile with every finding.
@Ken White Thanks for writing the above, but actually I am looking for a script where I want to write the result to found.txt file if and only if the above folder has any of the 0kb files only. As per your above script it looks like it writes the file everytime we exeute the batch. Let me know if I am missing anything here.
Not using >>, which appends. I should have mentioned that change directly to make it more apparent. (Thanks to @LotPings for adding it.)
@MGM: No, it adds only the zero byte file if any is found. If no files that are 0 bytes in size are found, it does not write anything. (I've tested both ways.) If you need to clean up a file created by a previous run, then delete it at the top of the batch file.
@KenWhite Yes, the above is correct but that is creating a file even after there are no 0kb files in the folder. I wish the file found.txt shouldn't be created if there are any 0kb files in the folder and the found.txt file should only be created and written only if there are any 0kb files residing in the folder. Hope I am clear now.
|
0

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'
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.