Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command Prompt "dir /b /s > file.txt" need to remove directories from results

Tags:

command

prompt

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 the filenames.

Anyone any ideas?

like image 328
fightstarr20 Avatar asked Dec 28 '22 19:12

fightstarr20


2 Answers

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 command dir 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.

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 adding n and x.
  • 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 and x 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

like image 120
julesverne Avatar answered Jan 13 '23 11:01

julesverne


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.

like image 23
manojlds Avatar answered Jan 13 '23 12:01

manojlds