Link to home
Create AccountLog in
Avatar of jtgraphic
jtgraphic

asked on

Looping Batch file to Text Log

I'm trying to ouput a looping batch file to a text log.

for /f "skip=4 tokens=1" %%a in ('net files') do net files %%a /close

is the loop - and works.

for /f "skip=4 tokens=1" %%a in ('net files') do net files %%a > test.txt /close

and

for /f "skip=4 tokens=1" %%a in ('net files') do net files %%a /close > test.txt

don't work.  What am I doing wrong?

-JKT
Avatar of knightEknight
knightEknight
Flag of United States of America image

use two >> signs:

>> test.txt /close
... I don't know what /close does, it may be in the wrong place -- it may go here instead:

for /f "skip=4 tokens=1" %%a in ('net files') do net files %%a /close >> test.txt
or you may be able to remove the  /close   altogether
Avatar of jtgraphic
jtgraphic

ASKER

net files >> %DATE%.txt
for /f "skip=4 tokens=1" %%a in ('net files') do net files %%a /close >> %DATE%.txt

is a blank file.
for /f "skip=4 tokens=1" %%a in ('net files') do net files %%a /close  >>"%DATE%.txt"  2>>"%DATE%.err"
You've got no net files!
Also, you can't create files with / characters in it -- try this:

for /f "skip=4 tokens=1" %%a in ('net files') do net files %%a /close  >>"%date:~10,4%%date:~4,2%%date:~7,2%.txt"  2>>"%date:~10,4%%date:~4,2%%date:~7,2%.err"

(all on one line)
>> Also, you can't create files with / characters in it

To see the difference, do this:

@echo %DATE%

@echo %date:~10,4%%date:~4,2%%date:~7,2%
knightEknight - It doesn't even output a file - I'd troubleshoot, but that command is beyond my basic knowledge of batch files :-/
... or, you can add dash or underscore characters to replace the slash characters:

@echo %date:~10,4%-%date:~4,2%-%date:~7,2%

@echo %date:~10,4%_%date:~4,2%_%date:~7,2%
ah, why are you doing the net files twice?  do this instead:

for /f "skip=4 tokens=1" %%a in ('net files') do @echo %%a >>"%DATE%.txt"  2>>"%DATE%.err"
DOH!  I copied the wrong date format ... try this:

for /f "skip=4 tokens=1" %%a in ('net files') do @echo %%a >>"%date:~10,4%%date:~4,2%%date:~7,2%
.txt"  2>>"%date:~10,4%%date:~4,2%%date:~7,2%
.err"

GRR!  now I've accidentally included an extra space before the .txt and .err -- please remove it.  (sorry about all this)
hrm... it's still not outputting to a file :-/
for /f "skip=4 tokens=1" %%a in ('net files') do @echo %%a >>"%date:~10,4%%date:~4,2%%date:~7,2%.txt"  2>>"%date:~10,4%%date:~4,2%%date:~7,2%.err"


i think RQuadling is right.  ;)
Shouldn't it still output a file - even if it's blank?
>> Shouldn't it still output a file - even if it's blank?

for /f "skip=4 tokens=1" %%a in ('net files') do @echo %%a >>"%date:~10,4%%date:~4,2%%date:~7,2%.txt"  2>>"%date:~10,4%%date:~4,2%%date:~7,2%.err"

This should output two files, blank or not.  Perhaps you need to specify the exact location of the files, like on the root of the C: drive:

for /f "skip=4 tokens=1" %%a in ('net files') do @echo %%a >>"C:\%date:~10,4%%date:~4,2%%date:~7,2%.txt"  2>>"C:\%date:~10,4%%date:~4,2%%date:~7,2%.err"
@echo off
if exist log del log
for /f "skip=4 tokens=1" %%a in ('net files') do echo net files %%a /close >> log

gives me ...

net files 4127898 /close
net files 4131971 /close
net files 4137611 /close
net files 4138164 /close
net files The /close

So, removing the echo SHOULD work.
Also, since you are using double-percents on the variable, I assume you are running the above in a batch file and not as a stand-alone command in the command prompt.  If you are doing the latter then use just one percent sign:

for /f "skip=4 tokens=1" %a in ('net files') do @echo %a >>"%date:~10,4%%date:~4,2%%date:~7,2%.txt"  2>>"%date:~10,4%%date:~4,2%%date:~7,2%.err"

(but in a batch file you MUST use two)
I ran this on my pc and I got no log file as I have no net files.

for /f "skip=4 tokens=1" %a in ('net files') do echo net files %a /close >> log
There was no command to execute as there was no files. The >> bit is part of the command.

for flag (set) do command

Empty set = no commands

And command > log is still a command.
good point about the empty set.  What do you get when you just type  net files  at the prompt?
^Q for jt
I guess I should elaborate.  I'd like a log - whether there was output or not.  I'm running this batch file on a schedule at night, and it isn't working, but when I run it manually it works.
Microsoft Windows [Version 5.2.3790]
(C) Copyright 1985-2003 Microsoft Corp.

C:\Documents and Settings\Administrator>^Q
'Ä' is not recognized as an internal or external command,
operable program or batch file.

C:\Documents and Settings\Administrator>
no, I meant what do you get when you run the  net files  command by itself from the command prompt?
2007/06/08  8:23:36 C:\>net files
There are no entries in the list.


2007/06/08  8:25:20 C:\>FOR /F "tokens=*" %A IN ('net files') DO ECHO %A

2007/06/08  8:25:22 C:\>ECHO There are no entries in the list.
There are no entries in the list.


2007/06/08  8:25:22 C:\>FOR /F "skip=4 tokens=*" %A IN ('net files') DO ECHO %A

2007/06/08  8:25:45 C:\>


The skip is the issue, necessary, but an issue.

There is a sort of workaround, but I have no net files output.

But ...

1 - Don't skip any lines.
2 - Capture the first token.
3 - Test that the token is numeric
4 - Handle the numeric token via net files /close
5 - If the token is NOT numeric then see if the entire line is "There are no entries in the list.".
6 - Report that there are no files.
7 - Ignore all other lines.

@ECHO OFF
IF EXIST Close.log DEL Close.log
IF EXIST Close.err DEL Close.err

:Step1
FOR /F "tokens=1,*" %%A IN ('net files') DO CALL :Step2 %%A "%%B"
GOTO :EOF

:Step2
SET /A Token=1 + %1 > NUL 2>NUL
IF /I %Token% EQU 1 GOTO NotNumber
ECHO Close handle %1 which is reported as "%1 %~2"
ECHO net files %1 /close >> Close.log 2>>Close.err
GOTO :EOF

:NotNumber
IF "%1"=="There" ECHO No files to close>>Close.err
GOTO :EOF

Seems to be OK for no files - you get a Close.err file - I'm trying to get some net files to see if the rest of this works.

Obviously, I'm not REALLY closing my files, but the ECHO net files ... can be edited to remove the ECHO and you'll start closing them.

ASKER CERTIFIED SOLUTION
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer