Solved

Dir for loop get path details.

Posted on 2013-05-23
9
418 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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39190166
11 seconds slow there in posting :-)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now