Page 1 of 1

File name and size

Posted: 26 Nov 2012 07:10
by aveit
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

Re: File name and size

Posted: 26 Nov 2012 07:12
by Squashman
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.

Re: File name and size

Posted: 26 Nov 2012 07:42
by foxidrive
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"

Re: File name and size

Posted: 26 Nov 2012 07:54
by Squashman
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.

Re: File name and size

Posted: 26 Nov 2012 07:57
by Squashman
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