Windows Batch & Powershell: list full path of files and folders on a folder

Luis Diaz
Luis Diaz used Ask the Experts™
on
Hello experts,
I am trying to list in 3 commands:
-All full path subfolders (immediate subfolders) on a folder
-All full path files on a folder
-All full path files and (immediate subfolders) on a folder

Drill down not required.
command should output a list.txt in %cd% folder

I tried  for second requirement this:
for /d %%a in ("%CD%\*") do echo %%~fa

Open in new window

but it doesn't work.

Batch and Powershell approaches are more than welcome.

Thank you in advance for your help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014

Commented:
Here's the three commands in Powershell.
Get-ChildItem -Path . -Directory | Select-Object -ExpandProperty fullname | Add-Content list.txt
Get-ChildItem -Path . -Files | Select-Object -ExpandProperty fullname | Add-Content list.txt
Get-ChildItem -Path . | Select-Object -ExpandProperty fullname | Add-Content list.txt

Open in new window

Luis DiazIT consultant

Author

Commented:
Thank you. Path should be protected with double quotes?
Top Expert 2014

Commented:
Doesn't have to be, it depends on the path.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Luis DiazIT consultant

Author

Commented:
Thank you back slash at the end of the path required?
Top Expert 2014

Commented:
No.  Why don't you try it out and see?  That'd be much faster than posting and waiting for a response.  Don't be afraid to experiment.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Here's some BAT approaches to your three needs.

@echo off
setlocal

pushd "c:\temp"

rem All full path subfolders (immediate subfolders) on a folder
for /d %%A in ("%CD%\*.*") do echo %%~A >"%cd%\list.txt"

rem All full path files on a folder
for %%A in ("%CD%\*.*") do echo %%~A >"%cd%\list.txt"

rem All full path files and (immediate subfolders) on a folder
for /d %%A in ("%CD%\*.*") do echo %%~A >"%cd%\list.txt"
for %%A in ("%CD%\*.*") do echo %%~A >"%cd%\list.txt"

popd

Open in new window


»bp
Luis DiazIT consultant

Author

Commented:
Thank you Bill, I will test it this week and keep you informed.
Luis DiazIT consultant

Author

Commented:
@Footech: I don't see the directory in which will be exported list.txt file.
@Bill: pushd/popd is required? I was wondering to launch differents commands with cmd and avoiding saving .bat file.
Thank you.
Top Expert 2014

Commented:
If you don't supply the full path then PowerShell uses the current directory, just like cmd.exe/batch.  You can supply the path to search and for output in variety of ways, I just showed the simplest.
Luis DiazIT consultant

Author

Commented:
Thank you.
Bill, I tested proposal however I just get one single folder and one single file. The rest of files and folders are not displayed.
I am testing with the following version:
@echo off
setlocal

pushd "C:\Temp"

rem All full path subfolders (immediate subfolders) on a folder
for /d %%A in ("%CD%\*.*") do echo %%~A >"%cd%\list1.txt"

rem All full path files on a folder
for %%A in ("%CD%\*.*") do echo %%~A >"%cd%\list2.txt"

rem All full path files and (immediate subfolders) on a folder
for /d %%A in ("%CD%\*.*") do echo %%~A >"%cd%\list3.txt"
for %%A in ("%CD%\*.*") do echo %%~A >"%cd%\list3.txt"

popd

Open in new window


Thank you.
Luis DiazIT consultant

Author

Commented:
@Footech: Could you please one example with directory based on your proposal: https://www.experts-exchange.com/questions/29147181/Windows-Batch-Powershell-list-full-path-of-files-and-folders-on-a-folder.html#a42872205
Just one example and I will do the rest based on your example.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016
Commented:
Sorry, try this approach to get all files / folders in list files.

@echo off
setlocal

pushd "C:\Temp"

rem All full path subfolders (immediate subfolders) on a folder
(
    for /d %%A in ("%CD%\*.*") do echo %%~A
)>"%cd%\list1.txt"

rem All full path files on a folder
(
    for %%A in ("%CD%\*.*") do echo %%~A
)>"%cd%\list2.txt"

rem All full path files and (immediate subfolders) on a folder
(
    for /d %%A in ("%CD%\*.*") do echo %%~A
    for %%A in ("%CD%\*.*") do echo %%~A
)>"%cd%\list3.txt"

popd

Open in new window


»bp
Top Expert 2014

Commented:
I'm not quite sure what you're asking.  Here's an example of supplying the full path for the output file.
Get-ChildItem -Path . -Directory | Select-Object -ExpandProperty fullname | Add-Content c:\temp\list.txt

Open in new window

Luis DiazIT consultant

Author

Commented:
Thank you.
I need to get information for C:\ folder. Where should I report this path. Should I do a CD and then report your proposal?
Bill PrewIT / Software Engineering Consultant
Top Expert 2016
Commented:
And you asked about doing it with PUSHD/POPD, here's a swing at that.  Not sure how you would get this down to a single command line though...

Just in passing, since you are interested in small but powerful one liners, you might want to take a look at this free utility.  It has a lot of powerful features that might be useful for you with minimal "scripting", and tons of options...  Just thought I'd mention it...  You would have to have the EXE on the system you want to use it, but it's just a single portable EXE that needs to be available so pretty lightweight.


@echo off
setlocal

rem All full path subfolders (immediate subfolders) on a folder
(
    for /d %%A in ("C:\Temp\*.*") do echo %%~A
)>"C:\Temp\list1.txt"

rem All full path files on a folder
(
    for %%A in ("C:\Temp\*.*") do echo %%~A
)>"C:\Temp\list2.txt"

rem All full path files and (immediate subfolders) on a folder
(
    for /d %%A in ("C:\Temp\*.*") do echo %%~A
    for %%A in ("C:\Temp\*.*") do echo %%~A
)>"C:\Temp\list3.txt"

Open in new window


»bp
Top Expert 2014
Commented:
Should I do a CD...
In this command
Get-ChildItem -Path . -Directory | Select-Object -ExpandProperty fullname | Add-Content c:\temp\list.txt
the dot after -Path for Get-ChildItem means the current directory.  So yes, either cd to the c:\ location, or you can substitute the full path in place of "." without needing to cd to the location.  Example:
Get-ChildItem -Path c:\ -Directory | Select-Object -ExpandProperty fullname | Add-Content c:\temp\list.txt

Open in new window

Luis DiazIT consultant

Author

Commented:
Thank you very much Bill, I will take the time to check the swize knife application.
In the meantime, your last proposal works for me. However I would like to understand why when I run a single command from cmd I have unexpected %%A was unexpected at this time.
For example If I just want to run from cmd:
(
    for /d %%A in ("C:\Temp\*.*") do echo %%~A
)>"C:\Temp\list1.txt"

Open in new window

how should I proceed.
Just to clarify your proposal works perfectly if I save it as .bat. My concern is through cmd withtout saving .bat file.
IT / Software Engineering Consultant
Top Expert 2016
Commented:
Loop variables are referenced differently in BAT versus at the command prompt.  In BAT they have two percent signs, but at the command prompt the syntax is only one.  So %%A in a BAT file needs to be %A at the command prompt.  This change is needed for all references to FOR loop variables.


»bp
Luis DiazIT consultant

Author

Commented:
Very clear. Thank you again for your help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial