Path inclusion in Dir
Moderator: DosItHelp
Path inclusion in Dir
How to get the path included in the results for each file when executing a dir /b command?
Re: Path inclusion in Dir
Depends on how to use DIR /B. Do you execute it across the current working directory?
Regards
aGerman
Regards
aGerman
Path inclusion in Dir
Either way.
I know that if I include the /s switch, I get the path BUT I also get any subdirectories.
I know that if I include the /s switch, I get the path BUT I also get any subdirectories.
Re: Path inclusion in Dir
The command "DIR /b" will only have a result the file name.
With the command "DIR /b /s" will have any file name also the path name included.
Directly is this not possible what you want. But you can use the following code:
With the command "DIR /b /s" will have any file name also the path name included.
Directly is this not possible what you want. But you can use the following code:
Code: Select all
for /f %a in ('dir /b') do echo %cd%\%a
Re: Path inclusion in Dir
trebor68 wrote:The command "DIR /b" will only have a result the file name.
With the command "DIR /b /s" will have any file name also the path name included.
Directly is this not possible what you want. But you can use the following code:Code: Select all
for /f %a in ('dir /b') do echo %cd%\%a
Thanks.
Can we get rid of the echo line and the blank line between each file?
Re: Path inclusion in Dir
Prepend @echo off to the code.
Regards
aGerman
Regards
aGerman
Re: Path inclusion in Dir
aGerman wrote:Prepend @echo off to the code.
Regards
aGerman
Thanks.
I noticed
@echo off and
echo off
do the same thing. Also the prompt disappears.
Can we include the echo command in the for line in such a way that when execution finishes the prompt comes back?
Re: Path inclusion in Dir
Write an echo on as next line.
Regards
aGerman
Regards
aGerman
Re: Path inclusion in Dir
If you use the code at the prompt then use this code:
In a seperate batch file:
When you will save the files with complete path in a text file:
Code: Select all
for /f %a in ('dir /b') do @echo %cd%\%a
In a seperate batch file:
Code: Select all
@echo off
for /f %%a in ('dir /b') do echo %cd%\%%a
When you will save the files with complete path in a text file:
Code: Select all
for /f %a in ('dir /b') do echo %cd%\%a >TestFile.txt
Path inclusion in Dir
Thank you!
That redirection to txt file though, only gives the last file.
That redirection to txt file though, only gives the last file.
Re: Path inclusion in Dir
You can switch to >> instead:
> = redirect output to a new file (overwrite any existing file)
>> = redirect output to a file - append to existing file or create file if it doesn't exist yet.
The append in a loop must repeatedly open and close the file. You can speed the process up by enclosing the entire loop in parentheses and redirecting so that it only opens and closes once:
An alternative to using %cd% is to use the ~f modifier (includes full path)
You can get the same result using the simplest form of FOR without needing the DIR command:
One nice feature of this form is you can use multiple wildcard expressions in the same FOR loop. For example, this will capture all .TXT and .DOC files, but ignore other file extensions:
Dave Benham
> = redirect output to a new file (overwrite any existing file)
>> = redirect output to a file - append to existing file or create file if it doesn't exist yet.
The append in a loop must repeatedly open and close the file. You can speed the process up by enclosing the entire loop in parentheses and redirecting so that it only opens and closes once:
Code: Select all
( for /f %%a in ('dir /b') do echo %cd%\%%a )>TestFile.txt
An alternative to using %cd% is to use the ~f modifier (includes full path)
Code: Select all
( for /f %%a in ('dir /b') do echo %%~fa )>TestFile.txt
You can get the same result using the simplest form of FOR without needing the DIR command:
Code: Select all
( for %%a in (*) do echo %%~fa )>TestFile.txt
Code: Select all
( for %%a in (*.txt *.doc) do echo %%~fa )>TestFile.txt
Dave Benham
Path inclusion in Dir
@Dave
Thanks, however these commands give the following error:
I got the desired results by replacing %% with % and echo with @echo.
Is there a way to get the number of files at the bottom of testfile.txt?
Thanks, however these commands give the following error:
%%a was unexpected at this time.
I got the desired results by replacing %% with % and echo with @echo.
Is there a way to get the number of files at the bottom of testfile.txt?
Re: Path inclusion in Dir
For the prompt all in one row:
For the batch file with writing in the file:
Code: Select all
(set num=0) & (for %a in (*) do @(echo %~fa)&(set/a num+=1 >nul)) & (echo !num! files)
For the batch file with writing in the file:
Code: Select all
@echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
if exist Testfile.txt del Testfile.txt
set num=0
for %%a in (*) do (
echo %%~fa >>Testfile.txt
set/a num+=1
)
echo !num! files >>Testfile.txt
Re: Path inclusion in Dir
trebor68 wrote:For the prompt all in one row:Code: Select all
(set num=0) & (for %a in (*) do @(echo %~fa)&(set/a num+=1 >nul)) & (echo !num! files)
Thanks.
Can you include the redirection to testfile.txt and review the formula because now it displays
instead of a number.!num! files
Re: Path inclusion in Dir
At the prompt and save result in a text file (all in one row):
I will hope that is now all ok.
In Win XP at my systym will all working correct.
File format testfile.txt is:
path\file
path\file
2 files
Code: Select all
(set num=0) & (for %a in (*) do @(echo %~fa >>testfile.txt)&(set/a num+=1 >nul)) & (echo %num% files >>testfile.txt)
I will hope that is now all ok.
In Win XP at my systym will all working correct.
File format testfile.txt is:
path\file
path\file
2 files