Simple FOR Loop not working

Microsoft Windows xp professional w/serv...
November 10, 2009 at 15:39:51
Specs: Windows XP
Hi

I am trying to write a batch file that will list the files and sizes in a directory based on the file that is passed to the batch file as a parameter.

So for example, Let's say my batch file is called "mybatch.bat" and I have a folder "C:\My Folder" with 3 files in it "file1.txt", "file2.txt", "file3.txt".


If I type:

mybatch.bat "C:\My Folder\file1.txt"

The output should be:

-- 77 -- file1.txt
-- 395 -- file2.txt
-- 465 -- file3.txt


Here is the batch file I wrote:

ECHO OFF

FOR /F "delims=" %%A IN ('DIR "%~dp1" /A-D /B') DO (
echo -- %%~zA -- %%A
)


But this is not working. Here is what I get instead

-- -- file1.txt
-- -- file2.txt
-- -- file3.txt


can someone find the mistake?


See More: Simple FOR Loop not working

Report •


#1
November 10, 2009 at 16:33:57
Good question why it doesn't work, it doesn't seem to like to give you file sizes if you're not in the working directory. Would something like the following work for you?


@ECHO OFF


pushd %~dp1
FOR /F "tokens=*" %%A IN ('DIR /A-D /B') DO (
echo -- %%~zA -- %%A
)
popd


Report •

#2
November 10, 2009 at 23:08:22
The "correct" version is:
@FOR %%A IN ("%~dp1\*") DO @echo -- %%~zA -- %%A


Report •

Related Solutions


Ask Question