1.

Solve : Dir command in For loop?

Answer»

Win XP Home.

I have two files with identical names on two physical hard drives, partitions C: and E:, the Dir outputs are shown below:

Quote

Volume in drive C is MASTER
Volume Serial Number is 2fd1-ab4e
Directory of c:\

Mon 01/26/2009 13:45 2,811 dates.bat
1 File(s) 2,811 bytes


Quote
Volume in drive E is SLAVE
Volume Serial Number is f68d-1e3a

Directory of e:\

Fri 12/05/2008 12:54 1,374 dates.bat
1 File(s) 1,374 bytes


When I run the following script 2811 is returned not, as I expected, 1374. Can someone please explain why the Dir command in the For loop is not picking up drive E: and defaulting to C:?

Quote
@echo off
cls

for /f %%A in ('dir/b e:\dates.bat') do (

echo %%~zA
)

I don't want a workaround, I can do that USING CD or Pushd, just an explanation of why Dir is not opting for partition E:. Dir/b e:\dates.bat at the Command Prompt works perfectly.

Thanks.

V.



My guess is that you are running the batch file from the directory that the 2811 byte file is in. I think the issue is that DIR /B will probably return just the filename instead of the entire path without the /S. And your parameters are not following the syntax for the DIR command (I don't think that is an issue in this case, but I would clean it up anyway). To verify my theory, try this:

Code: [Select]@echo off
cls

for /f %%A in ('dir e:\dates.bat /b') do (
echo FULLPATH=%%~fA
echo Size=%%~zA
)
You said you don't need a work-around, which is good - because the only thing I can think of would be to add a /S to the dir command which could add a significant amount of time to the script because it would search your entire E: drive for files named "dates.bat".Thanks for that GuruGary.

Quote
My guess is that you are running the batch file from the directory that the 2811 byte file is in.
You are correct, the batch script is located in C: which is the default directory.

Your script returned Quote
Fullpath=C:\dates.bat
Size=2811

I just don't understand why dir is defaulting to C: when E: is the target partition.

I'm just going to throw this out there and see if it sticks. Use the set command to see if a dircmd variable exists. Generally USED for default switches, it may have a value that is interfering with your execution of the dir command.

Quote from: ValerieMay on February 09, 2009, 01:26:56 AM
I just don't understand why dir is defaulting to C: when E: is the target partition.

Because using DIR with /B and without the /S the output is just "dates.bat" (without a path). So if a file with the same name happens to be in the current directory, then it uses that file. To see what I am talking about, make one more modification to your FOR loop:
Code: [Select]for /f %%A in ('dir e:\dates.bat /b') do (
echo Full path to file returned by DIR=%%A
echo Fullpath=%%~fA
echo Size=%%~zA
)You can then compare that to using the /S on the DIR command like
Code: [Select]for /f %%A in ('dir e:\dates.bat /b /S') do (
echo Full path to file returned by DIR=%%A
echo Fullpath=%%~fA
echo Size=%%~zA
)My guess is that the 2nd script will work the way you expect, but take longer to finish.
Code: [Select]for /f %%A in ('dir e:\dates.bat /b') do (
echo Full path to file returned by DIR=%%A
echo Fullpath=%%~fA
echo Size=%%~zA
)Returned Quote
Full path to file returned by DIR=dates.bat
Fullpath=C:\dates.bat
Size=2811

Code: [Select]for /f %%A in ('dir e:\dates.bat /b /S') do (
echo Full path to file returned by DIR=%%A
echo Fullpath=%%~fA
echo Size=%%~zA
)Returned Quote
Full path to file returned by DIR=e:\dates.bat
Fullpath=e:\dates.bat
Size=1374

Exactly as you predicted the latter TOOK a while.

I'm still at a loss to understand why Dir will not access the targetted drive without the /S switch.

Thanks for your interest.




Sidewinder - thank you. My Dircmd is set to /OG/ON/P but removing these switches made no difference, my script still defaulted to C:.

Quote from: ValerieMay on February 09, 2009, 01:15:13 PM
I'm still at a loss to understand why Dir will not access the targetted drive without the /S switch.
There are 2 separate tasks involved.
1) The DIR command is returning a file
2) The command interpreter uses the file returned by DIR to give you the size

If you look at the 2 outputs from your last run, you can see that the the first one says
Quote
Full path to file returned by DIR=dates.bat

And the second one (that used the /S) says
Quote
Full path to file returned by DIR=e:\dates.bat

So in the first example all the command interpreter has to go by is just a file name without a path. If a file that matches the request is in the current directory then it returns that INFORMATION (file size).

It is basically the difference of doing:
Code: [Select]for %a in (dates.bat) do echo %~za(which will return the size of dates.bat in whatever the current directory happens to be) and
Code: [Select]for %a in (E:\dates.bat) do echo %~zawhich will return the size of dates.bat in the specified path.

Does that help?Yes, somewhat, though I must admit the fog has not completely cleared. I now realise that /F and the Dir command were not required at all.

Thank you for your time and patience.

V..


Discussion

No Comment Found