3

I need to find in a specific folder if there exist a file with zero bytes having the extension *.ff0. The following script is not working since I always get the message "file is non-empty". What is wrong?

@echo off
set out="C:\test_files\*.ff0"

if "%out%" == "" ( 
  echo File does not exist.
) else if "%out%" == "0" (
   echo File is empty.
) else (
   echo File is non-empty.
) 

The second question is that my batch file will be located later on somewhere in an intranet folder, meaning that other people might possibly also have access.
Therefore, what should be the full code so as to do the following :

1-Identify if there is an open instance of the file.
2-If there is, warn the user that someone is already using it and, as soon as it closes, other users may open it

reuben
  • 3,360
  • 23
  • 28
Luiz Vaughan
  • 675
  • 1
  • 15
  • 33
  • It's unclear what you're asking in the second part of your question. If it is how to stop more than one person running the batchfile at a time, one common approach is to have the batchfile check for the presence of a `.lock` file, creating it if it is not there, or erroring if it it, then cleaning up the lock file when the batchfile exits. But it's not clear if that's really what you want... – forsvarir Jul 04 '12 at 11:57
  • Thank you ! The lock has replaced this script but it was nice to learn anyway. See the lock script made by our colleague dbenham on the link http://stackoverflow.com/questions/11619416/identify-running-instances-of-a-batch-file – Luiz Vaughan Jul 25 '12 at 19:35

2 Answers2

3

This will do the first part of your question. This will go through all the .ff0 files in the folder and for each one tell you if it's empty or not. Just replace or add underneath the echo Empty command to do what you want to do with the file.

@echo off
setlocal enabledelayedexpansion
for /r C:\test_files %%i in (*.ff0) do (
set size=%%~zi
if !size! gtr 0 (
echo Not empty
) else (
echo Empty!
)
)
pause >nul

With regards to checking if the file is open I don't think you can do this in batch. I don't even think there is a reliable way to do this in other proper programming languages other than to try and catch the error.

Hope this helps.

Bali C
  • 30,582
  • 35
  • 123
  • 152
3

Best to do it like this:

@echo off
for %%a in (c:\test_files\*.ff0) do (
  if %%~za equ 0 (
    echo %%~na is empty
  ) else (
    echo %%~na is not empty
  )
)
Paul Tomasi
  • 101
  • 2