0

Hi Am actually trying to redirect the output of a for loop to a file in batch and I don't want to create a empty file if there is no output from the for loop. In the below code, I am trying to redirect the name and date modified of files in a particular directory which are more than 60 mins old to a file called Errorfiles.txt. If there are no files that were modified in the last 60 mins then I should not get a empty file but am getting a file of 0KB size. Is there anyway I can stop generating that file if there's no output from the for loop ? Any help would be appreciated Thanks!
-----------------------------------------------------------MAIN.bat-----------------------------------------------------------

    @Echo off
    @setlocal EnableDelayedExpansion
    CALL function.bat %TIME%,CurrentTime

    (for /f "tokens=2,4" %%A in ('dir c:\Users\Administrator
    \Batch\*.* ^| find "/"') do (
    CALL function.bat %%A,FileTime
    REM echo !CurrentTime! !FileTime!
    set /a diff=!CurrentTime!-!FileTime!  
    if !diff! geq 60 (
    echo Filename: %%B Date Modified %%A 
    )
    )) 1>ErrorFiles.txt 2>nul

-------------------------------------------------------------------Function.bat--------------------------------------------

    @Echo off

    SET time1=%1
    set HH=%time1:~0,2%
    set MM=%time1:~3,2%
    set /a MM=%MM%
    set /a HH=%HH% * 60
    set /a ctime=%HH% + %MM%
    set /a "%~2=%ctime%"
    EXIT /B      
0

2 Answers 2

2

Compare the following two lines. a.txt gets generated, b.txt not.

(for /f %a in ('REM') do echo %a) >a.txt
for /f %a in ('REM') do (echo %a >b.txt)

If you want to keep the speed of redirecting only once (with possibly big files, just delete the file, if it's empty:

for %%a in (a.txt) do if %%~za == 0 del %%a
Sign up to request clarification or add additional context in comments.

2 Comments

The delete file thing is good man ... Is there any other way to redirect only if it has output ?
I'm only aware of the method used with b.bat The trick there is, the command after do is only executed, when there is output from the for /f loop. Downside is, you loose speed (you'll not notice it with small outputs, but can sum up if you have hundrets or thousands of lines of output) Note: you then surely want >> instead of >
0

The method shown below is the simplest and fastest way to test if a for /F command generate no output, so delete the empty output file in such a case:

(for /F %%A in ('a command') do echo %%A) > output.txt || rem
if errorlevel 1 del output.txt

An extensive explanation of this method is given at this answer, below Exit Code management.

Note: the redirection of a command output to a file (and the creation of an empty file) happens before the command is executed, so there is no way to know in advance if such an output will be empty...

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.