File name and size

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
aveit
Posts: 8
Joined: 17 Oct 2011 05:16

File name and size

#1 Post by aveit » 26 Nov 2012 07:10

Hi

I have the following code that will give me the file name and size when run directly from the command prompt:

for %l in (readme.txt) do echo %~nl %~zl BYTES >> C:\Paul\output.txt

This puts the details in the output document fine. However when i put this in a batch file it no longer works.

c:
cd\
cd Paul
for %l in (readme.txt) do echo %~nl %~zl BYTES >> C:\Paul\output.txt

Any ideas?

Thanks
Paul

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: File name and size

#2 Post by Squashman » 26 Nov 2012 07:12

Read the FOR command HELP! Reading is FUNdamental!

Code: Select all

H:\>for /?
Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

  %variable  Specifies a single letter replaceable parameter.
  (set)      Specifies a set of one or more files.  Wildcards may be used.
  command    Specifies the command to carry out for each file.
  command-parameters
             Specifies parameters or switches for the specified command.

To use the FOR command in a batch program, specify %%variable instead
of %variable.  Variable names are case sensitive, so %i is different
from %I.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: File name and size

#3 Post by foxidrive » 26 Nov 2012 07:42

aveit wrote:c:
cd\
cd Paul
for %l in (readme.txt) do echo %~nl %~zl BYTES >> C:\Paul\output.txt


While squashman has provided the info, I wanted to comment on using the variable I in the for in do.

It has long been taught in programming that using i I l and o 0 O for variable names is a poor choice, because they can be easily mistaken in many fonts.
I stick to using 'a' and branch out from there when needed.

This should work for you:

cd /d c:\paul
for %%a in (readme.txt) do echo %%~na %%~za BYTES >> "output.txt"

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: File name and size

#4 Post by Squashman » 26 Nov 2012 07:54

I prefer to use a captial G. This way you have a lesser chance of confusing it with one of the command modifiers.

%%~aa just looks goofy.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: File name and size

#5 Post by Squashman » 26 Nov 2012 07:57

And in a question you had here over a year ago, Dave showed you the doubling of the percent signs in the FOR loops he had in the nice batch file he wrote for you.
viewtopic.php?f=3&t=2357&p=10618#p10618

Post Reply