Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Dir for loop get path details.

Posted on 2013-05-23
9
Medium Priority
?
471 Views
Last Modified: 2013-07-04
I have the following code...

FOR /f "delims=" %%f in ('dir /b /a-d-h c:\source\*.*') DO (
 Echo %%f >> Output
)

Open in new window


I know nothing about DOS but am looking to expand this in some way so that I can get both the file path and its name in separate variables? I also need a way of determining whether I am iterating on the first file in the loop - if so I want to use > to echo to a fresh file - otherwise I want to use >> to append to the file? Any ideas?
0
Comment
Question by:Blowfelt82
9 Comments
 
LVL 85

Expert Comment

by:oBdA
ID: 39190162
You can access the path of a "for" variable X using "%~dpX" (see "help call" for details), the name and extension with %~nxX; and if you want to set and expand variables inside a "for" loop, you need delayed variable expansion (see "help set" for details):
@echo off
setlocal enabledelayedexpansion
set OutputFile=Output.txt
if exist "%OutputFile%" del "%OutputFile%"
FOR /f "delims=" %%f in ('dir /b /a-d-h c:\source\*.*') DO (
  set FilePath=%~dpf
  set FileName=%~nxf
  echo File path: !FilePath!
  echo File name: !FileName!
  echo %%f >>"%OutputFile%"
)

Open in new window

0
 
LVL 5

Expert Comment

by:djbaum
ID: 39190163
If you just want the overview, you can use this:
tree /f

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39190164
Ok, try this.  Putting ( ) around commands will do it all in one go so one file operation creating file.  You can get different bits of file name and size etc. using operators on %%f, have shown some here to get path, name, size but see for /? for more.  I have changed %%f to %%F just to make it clearer in the other bits.  The ~ removes the "" characters around any files with spaces in so can re-add them to all for CSV type output.


@echo off
(FOR /f "delims=" %%F in ('dir /b /a-d-h c:\source\*.*') DO (
 Echo "%%~dpF","%%~nxF","%%~zF","%%~tF"
))> Output.csv
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 43

Expert Comment

by:Steve Knight
ID: 39190166
11 seconds slow there in posting :-)
0
 

Author Comment

by:Blowfelt82
ID: 39190282
Thanks for the posts so far, DragonIT your solution looks good but when I run it the %%~dpF command returns the path of the executing batch file and not of the file. This is I think an issue with my original DIR command which looking at it more closely does not include the path of the file in the output?
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 2000 total points
ID: 39190317
Sorry, I missed the fact you weren't doing sub directories.  If you had /s then it would include the path in the dir output and work.

If you want subdirs too add /s and it will work (or if there are no subdirs) or do away with the dir command:

@echo off
(FOR %%F in (c:\source\*.*) DO (
 Echo "%%~dpF","%%~nxF","%%~zF","%%~tF"
))> Output.csv

Steve
0
 

Author Comment

by:Blowfelt82
ID: 39190341
Perfect! Thanks for the help!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39190428
No problem, don't forget to "accept" the answer(s)
0
 

Author Closing Comment

by:Blowfelt82
ID: 39299567
Sorry for the late 'Accept'!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question