Solved

Dir for loop get path details.

Posted on 2013-05-23
9
432 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 83

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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