Command Prompt "dir /b /s > file.txt" need to remove directories from results
I am using the following command to dump the complete file listings recursively from a directory.
dir /b /s c:\myfolder > c:\mylist.txt
This works fine but it is display the results with the full path as well, beacuse I am using a regex expression on the results I need them to display only 开发者_JAVA技巧the filenames.
Anyone any ideas?
Kind of an old question but if someone stumbles across this hoping for an answer, perhaps this will help them out.
Running this from the windows command line (CMD.exe) use:
setlocal enabledelayedexpansion
for /f "delims=" %a in ('dir /b /s c:\myfolder"') do (@echo %~nxa >>c:\mylist.txt)
endlocal
Running this from a windows .BAT script use:
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir /b /s c:\myfolder"') do (@echo %%~nxa >>c:\mylist.txt)
endlocal
The output might look something like this depending on what files are in the folder you're running the code in:
file1.fil
file2.fil
file3.fil
UNDERSTANDING WHAT THE CODE IS DOING
for /f
- means to run a loop through files in this case using the
dir /b /s
command to help get those files names from directories (folders) and subdirectories (subfolders). As stated in the question, this will give you complete paths to the files. So instead of file.txt you will get C:\folder\file.txt.
"delims="
- in this case tells the for loop that it wants the variable %a or %%a to only have 1 folderpath and filename for every loop.
%a (CMD.exe) %%a (.BAT)
- as mentioned above is a variable that changes with each loop. so
everytime the command
dir /b /s
finds a new filename the variable %%a changes to the filename. - example: Loop 1: %%a = c:\folder\file1.fil Loop 2: %%a = c:\folder\file2.fil
dir /b /s
is the command to print out the files of a directory (folder). By using the
/b
and/s
the questioner is adding additional criteria. the commanddir
not only prints out the files and directories (folders) in that directory (folder) but also some additional information about the directory.- the
/b
tells the command dir that it doesn't want the additional information.. just the filenames. - The
/s
tells the command dir to include all the files and subdirectories (subfolders) in that folder.
- the
do
- is the part of the loop that tells what to do during that particular
loop. So in this case it is only doing this one command every loop
(@echo %%~nxa >>c:\mylist.txt)
@echo
- is a simple command that prints out whatever you want either to your
computer screen or in this case to a txt file by using
@echo %%~nxa >>c:\mylist.txt
- the
>>
before c:\mylist.txt is especially important. Every time a loop happens it starts a new line in the txt file and writes the variable to that line. If only one>
is specified it will overwrite the line in the txt file everytime the loop happens. Which will defeat the purpose of what this script is designed to do.
%~nxa (CMD.exe) %%~nxa (.BAT)
- is the variable %%a as mentioned above except it is parsed (edited) out the way the questioner @fightstarr20 asked for. Instead of printing out the variable as C:\myfolder\myfile.fil the variable will print out as myfile.fil
- the
~
in%%~nxa
tells the program you want to modify the variable %%a. In this case by addingn
andx
. the
n
in%%~nxa
tells the program you want to modify the variable %%a by excluding the path from the variable.example.
-variable %%a = C:\folder\filename.fil -variable %%~na = filename.
-If you notice however that it leaves the extension .fil off of the filename.
the
x
in%%~nxa
tells the program you want to modify the variable %%a by excluding the path and the filename from the variable, so all you will get is the extension of the filename.example.
-variable %%a = C:\folder\filename.fil -variable %%~xa = .fil
so if you combine both of the modifiers
n
andx
to the variable %%a you will get the full filename including the extension.example:
-variable %%a = c:\folder\filename.fil -variable %%~nxa = filename.fil
setlocal enabledelayedexpansion
- explained simply is a command that needs to be in the script before the for loop in order to allow the variable %%a to be modified or "expanded".
endlocal
- this turns off the setlocal enabledelayedexpansion command
To get a very helpful explanation and reference for CMD commands I recommend reading ss64.com and for a great forum to get CMD answers I'd recommend dostips.com
Change your regex to get the filename from the entire path.
If you can use powershell, look at Get-ChildItem
. You can have more powerful options with it.
Use it like this
dir /b /s C:\myfolder>C:\temp.txt
echo exit>>C:\temp.txt
goto loop
:loop
set /p _x=<temp.txt
findstr /v /c:"%_x%" temp.txt>temp2.txt
type temp2.txt>temp.txt
set _x=%_x:*\=%
echo %_x%>file.txt
if "%_x%" == "exit" (
del temp.txt
del temp2.txt
exit
)
goto loop
You can use for instead of goto if you like, but it will be basicaly the same. Sorry about the last one...
I know I'm a bit late, but it hurts me that nobody said to take away the /s
dir /b c:\myfolder > c:\mylist.txt
That should do it.
This would surely work, as it works for me.
dir D:(Path to files) /s /b >d:\filelist.txt
You can just use this code:
dir /b > A_fileslist.txt
Copy inside a notepad editor and save as "Fileslist.bat".
精彩评论