Get the path of a file that is not in �%

September 1, 2009 at 12:21:04
Specs: Windows XP Pro SP3
Please, see this simple code:
@echo off
for /f "tokens=* delims=" %%a in ('dir /b /a-d c:\' ) do (
echo %%a
echo %%~pa

The output is:
- %%a = all filenames in c:\
- %%~pa = current path name (diferent of c:\ path)

I thought that if %%a = c:\filename, then %%~pa should de c:
For some reasons I dont want to use PUSHD to get path name, one of these reasons is that in my real bat the input format to path name is pretty variable (it saves typing but raises difficulty to handle).

Is there any way of making %%~pa to express the real path of %%a? I could solve this in another way, with a few lines of string manipulation, but I am wondering whether there is another simple sintax resource that I am missing.

Thank any enlightening.

See More: Get the path of a file that is not in �%

Report •

September 1, 2009 at 14:04:17
Use the traditional/default behavior.
FOR %%a IN (C:\*) DO @ECHO %%~DPa

Report •

September 2, 2009 at 04:57:03
Razor, in my bat the FOR command is like bellow:

FOR /f "tokens=1,* delims=" %%a in ('dir /b /a-d C:\*') DO @ECHO %%~DPa

It still outputs my current path (C:\Documents and Settings\Ricardo\Desktop\) and not c:.

Where I am wrong?

Report •

September 2, 2009 at 08:45:37
Other than not doing the version I just showed?

Okay, it's like this: FOR won't do much to locate the file. If you don't provide a path to the file for FOR /F, the command assumes it should be in the current directory. This is why the FOR loop works as you'd expect when you use DIR /B/S C:\ (the /S provides the full path), or DIR /B (enumerated files will be in the current directory).

By contrast, the loop I posted makes FOR look for the files. Since it finds the files itself, it knows the correct path to the file.

Make sense?

Report •

Related Solutions

September 2, 2009 at 11:32:02
Thank you very much, Razor. You cleared my question. Now I can start with another feasible solution knowing that I am not missing a more simple solution due my incipient knowlegde on sintax.

Report •

Ask Question