Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
DOS/Other
Tag Cloud
access acer asus bios bsod computer crash desktop dns driver drivers error ethernet excel freeze gaming graphics hard drive hardware hdmi internet laptop malware memory monitor motherboard network printer problem ram registry repair router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > DOS/Other >
FOR Loop Batch file not working

Reply  
Thread Tools
keneo's Avatar
Junior Member with 1 posts.
 
Join Date: Nov 2009
10-Nov-2009, 07:21 PM #1
FOR Loop Batch file not working
Hi
I am trying to write a batch file that will list the files and sizes in a directory based on the file that 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 my output
-- -- file1.txt
-- -- file2.txt
-- -- file3.txt
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
11-Nov-2009, 12:03 AM #2
Code:
ECHO OFF

pushd %1
FOR /F "tokens=*" %%A IN ('DIR /A-D /B') DO (
echo -- %%~zA -- %%A
)
popd
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
11-Nov-2009, 02:01 AM #3
Welcome to TSG!

Using the /B switch with Dir means %%A has only the file name. The output of a For loop is just a text string.
With out a path included in the variable, %%~z uses the Current Directory to try to find the file to get the size, so if you haven't switched to that folder, it can't find the file, and won't report the size.

One bug with Squashman's code, PushD won't accept a file name, so if passing a path that includes a file name, you would need to extract the path from the passed file.
Code:
ECHO OFF
PushD "%~dp1"
FOR /F "Tokens=*" %%A IN ('DIR /A-D /B') DO (
echo -- %%~zA -- %%A
)
PopD
The above will work with:
mybatch.bat "C:\My Folder\file1.txt"
or
mybatch.bat "C:\My Folder\"
but not
mybatch.bat "C:\My Folder"
The last one will be seen as just C:\

This will work for all three cases:
Code:
ECHO OFF
PushD "%~1" 2>Nul
If Errorlevel 1 PushD "%~dp1"
FOR /F "delims=" %%A IN ('DIR /A-D /B') DO (
echo -- %%~zA -- %%A
)
PopD
Delims= and Tokens=* are equivalent, either can be used
Quotes shouldn't be needed with PushD, but there are settings that would require a path with spaces to be quoted, so best to use "%~1" to remove any passed quotes and then quote the parameter, just in case.
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
11-Nov-2009, 10:01 AM #4
Not sure why I removed the ~dp option. I had it in there and then removed it at the last minute. Quick brain fart.

I could have sworn I ran the batch like this.
mybatch.bat "C:\My Folder"
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 07:53 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.