Computer Hope

Microsoft => Microsoft DOS => Topic started by: Handler on April 17, 2017, 03:04:03 PM

Title: How to output echo'd info from a for loop
Post by: Handler on April 17, 2017, 03:04:03 PM
Hello!

Fairly simple problem that I can't figure out.  :(

I managed to get this far :

Code: [Select]
for %F in ("e:\*") do @echo %F=:=%~zF
Which does what I want.  The =:= is there just so I have something to explode off of when reading this in to a PHP script.  Now I tried using the > filename.txt to direct the output to a file but in the end I would only get the name of the that file "filename.txt" and it's file size.

This will be my one learned thing for the day!  ;D

Thanks for any and all help, super appreciated!

Dave
Title: Re: How to output echo'd info from a for loop
Post by: Hackoo on April 17, 2017, 09:02:18 PM
Hi  ;)

So what is the result of those commands ?

Code: [Select]
for %F in ("e:\*") do @echo %F=:=%~zF > e:\File_Size1.txt
and this :

Code: [Select]
for %F in ("e:\*") do @echo %F=:=%~zF >> e:\File_Size2.txt
Title: Re: How to output echo'd info from a for loop
Post by: Handler on April 19, 2017, 09:24:46 AM
Sorry I didn't respond earlier but yes it seems that second option does work and lists all the files in that directory the first just shows the last file.  Which leads me to believe it just kept over writing the file every time. Ugh!

Thanks for getting me un-stuck!  ;D
Title: Re: How to output echo'd info from a for loop
Post by: strollin on April 21, 2017, 09:39:27 PM
As you found, a single chevron > overwrites the file while double chevrons append.  I've found it works best to write the batch file in such a way that the first file echoed to the file is done with one >, while the rest are echoed with two >>.  That way, I get a new file each time but I still get everything.

You could also just delete the file first, then use >> to do the echoing.