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

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.
LVL 1
LD16Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
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

LD16Author Commented:
Thank you. Path should be protected with double quotes?
footechCommented:
Doesn't have to be, it depends on the path.
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

LD16Author Commented:
Thank you back slash at the end of the path required?
footechCommented:
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 ConsultantCommented:
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
LD16Author Commented:
Thank you Bill, I will test it this week and keep you informed.
LD16Author 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.
footechCommented:
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.
LD16Author 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.
LD16Author 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 ConsultantCommented:
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
footechCommented:
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

LD16Author 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 ConsultantCommented:
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
footechCommented:
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

LD16Author 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.
Bill PrewIT / Software Engineering ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LD16Author Commented:
Very clear. Thank you again for your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.