We help IT Professionals succeed at work.

Looping Batch file to Text Log

383 Views
Last Modified: 2010-04-16
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
Comment
Watch Question

CERTIFIED EXPERT

Commented:
use two >> signs:

>> test.txt /close
CERTIFIED EXPERT

Commented:
... 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
CERTIFIED EXPERT

Commented:
or you may be able to remove the  /close   altogether

Author

Commented:
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.
CERTIFIED EXPERT

Commented:
for /f "skip=4 tokens=1" %%a in ('net files') do net files %%a /close  >>"%DATE%.txt"  2>>"%DATE%.err"
Richard QuadlingSenior Software Developer

Commented:
You've got no net files!
CERTIFIED EXPERT

Commented:
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"

CERTIFIED EXPERT

Commented:
(all on one line)
CERTIFIED EXPERT

Commented:
>> 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%

Author

Commented:
knightEknight - It doesn't even output a file - I'd troubleshoot, but that command is beyond my basic knowledge of batch files :-/
CERTIFIED EXPERT

Commented:
... 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%
CERTIFIED EXPERT

Commented:
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"
CERTIFIED EXPERT

Commented:
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"

CERTIFIED EXPERT

Commented:
GRR!  now I've accidentally included an extra space before the .txt and .err -- please remove it.  (sorry about all this)

Author

Commented:
hrm... it's still not outputting to a file :-/
CERTIFIED EXPERT

Commented:
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"


CERTIFIED EXPERT

Commented:
i think RQuadling is right.  ;)

Author

Commented:
Shouldn't it still output a file - even if it's blank?
CERTIFIED EXPERT

Commented:
>> 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"
Richard QuadlingSenior Software Developer

Commented:
@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.
CERTIFIED EXPERT

Commented:
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)
Richard QuadlingSenior Software Developer

Commented:
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
Richard QuadlingSenior Software Developer

Commented:
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.
CERTIFIED EXPERT

Commented:
good point about the empty set.  What do you get when you just type  net files  at the prompt?
CERTIFIED EXPERT

Commented:
^Q for jt

Author

Commented:
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.

Author

Commented:
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>
CERTIFIED EXPERT

Commented:
no, I meant what do you get when you run the  net files  command by itself from the command prompt?
Richard QuadlingSenior Software Developer

Commented:
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.

Richard QuadlingSenior Software Developer

Commented:
@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.

Senior Software Developer
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.