List directory and files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pp8771
Posts: 15
Joined: 12 Jun 2018 06:54

List directory and files

#1 Post by pp8771 » 12 Jun 2018 07:02

Actually i am listing four directories and four files inside those directory and not able to do it properly.

Code: Select all

@echo off
set foldername=D:\Prev_Machine\Pranab\Daimler\Xtech\Training\
for /f "usebackq tokens=*" %%a in (`dir /b/s/a:d %foldername%MigrationPoc`) do (
 echo:%%~nxa
 set file1=%foldername%MigrationPoc\%%~nxa
 echo:%%~nxa
 echo %file1%
for /f "usebackq tokens=*" %%a in (`dir /a-d /b %file1%` ) do  (
echo:%%~na
)
 
echo:777
echo:%%~nxa
)
cd %foldername%
pause

I am getting output as which has problem:

Sch_A
Sch_A
D:\Prev_Machine\Pranab\Daimler\Xtech\Training\MigrationPoc\Sch_D --it is expected as D:\Prev_Machine\Pranab\Daimler\Xtech\Training\MigrationPoc\Sch_A
a1
b1
c1
d1
777
Sch_A
Sch_B
Sch_B
D:\Prev_Machine\Pranab\Daimler\Xtech\Training\MigrationPoc\Sch_D --it is expected as D:\Prev_Machine\Pranab\Daimler\Xtech\Training\MigrationPoc\Sch_B
a1
b1
c1
d1
777
Sch_B
Sch_C
Sch_C
D:\Prev_Machine\Pranab\Daimler\Xtech\Training\MigrationPoc\Sch_D --it is expected as D:\Prev_Machine\Pranab\Daimler\Xtech\Training\MigrationPoc\Sch_C
a1
b1
c1
d1
777
Sch_C
Sch_D
Sch_D
D:\Prev_Machine\Pranab\Daimler\Xtech\Training\MigrationPoc\Sch_D
a1
b1
c1
d1
777
Sch_D

Can any one rectify the code.

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: List directory and files

#2 Post by pieh-ejdsch » 12 Jun 2018 09:26

Hello pp8771,
you have started this batch twice in a command line prompt. Your batch has no setlocal.
In the first run it saved the name of the last file into the variable file1. Your second run goes with the content of file1. You may use Enable delayedexpansion in the same codeline.
But in your situation this will run also

Code: Select all

@echo off
pushD D:\Prev_Machine\Pranab\Daimler\Xtech\Training\
for /f "usebackq tokens=*" %%a in (`dir /b/s/a:d MigrationPoc`) do (
 echo:%%~nxa
 for /f "usebackq tokens=*" %%a in (`dir /a-d /b %%a` ) do  (
  echo:%%~na
 )
 
 echo:777
 echo:%%~nxa
)
pause
popD
Phil

pp8771
Posts: 15
Joined: 12 Jun 2018 06:54

Re: List directory and files

#3 Post by pp8771 » 12 Jun 2018 12:00

Thanks Phil

Post Reply