Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dir for loop get path details.

Posted on 2013-05-23
9
Medium Priority
?
462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

715 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