Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Kind of an old question but if someone stumbles across this hoping for an answer, perhaps this will help them out.</p> <p>Running this from the windows command line (CMD.exe) use:</p> <pre><code>setlocal enabledelayedexpansion for /f "delims=" %a in ('dir /b /s c:\myfolder"') do (@echo %~nxa &gt;&gt;c:\mylist.txt) endlocal </code></pre> <p>Running this from a windows .BAT script use:</p> <pre><code>setlocal enabledelayedexpansion for /f "delims=" %%a in ('dir /b /s c:\myfolder"') do (@echo %%~nxa &gt;&gt;c:\mylist.txt) endlocal </code></pre> <p>The output might look something like this depending on what files are in the folder you're running the code in:</p> <pre><code>file1.fil file2.fil file3.fil </code></pre> <p>UNDERSTANDING WHAT THE CODE IS DOING</p> <blockquote> <p>for /f</p> </blockquote> <ul> <li>means to run a loop through files in this case using the <code>dir /b /s</code> 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.</li> </ul> <blockquote> <p>"delims="</p> </blockquote> <ul> <li>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.</li> </ul> <blockquote> <p>%a (CMD.exe) %%a (.BAT)</p> </blockquote> <ul> <li>as mentioned above is a variable that changes with each loop. so everytime the command <code>dir /b /s</code> finds a new filename the variable %%a changes to the filename.</li> <li>example: Loop 1: %%a = c:\folder\file1.fil Loop 2: %%a = c:\folder\file2.fil</li> </ul> <blockquote> <p>dir /b /s</p> </blockquote> <ul> <li><p>is the command to print out the files of a directory (folder). By using the <code>/b</code> and <code>/s</code> the questioner is adding additional criteria. the command <code>dir</code> not only prints out the files and directories (folders) in that directory (folder) but also some additional information about the directory.</p> <ul> <li>the <code>/b</code> tells the command dir that it doesn't want the additional information.. just the filenames.</li> <li>The <code>/s</code> tells the command dir to include all the files and subdirectories (subfolders) in that folder.</li> </ul></li> </ul> <blockquote> <p>do </p> </blockquote> <ul> <li>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 <code>(@echo %%~nxa &gt;&gt;c:\mylist.txt)</code></li> </ul> <blockquote> <p>@echo</p> </blockquote> <ul> <li>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 <code>@echo %%~nxa &gt;&gt;c:\mylist.txt</code></li> <li>the <code>&gt;&gt;</code> 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 <code>&gt;</code> 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.</li> </ul> <blockquote> <p>%~nxa (CMD.exe) %%~nxa (.BAT)</p> </blockquote> <ul> <li>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</li> <li>the <code>~</code> in <code>%%~nxa</code> tells the program you want to modify the variable %%a. In this case by adding <code>n</code> and <code>x</code>.</li> <li><p>the <code>n</code> in <code>%%~nxa</code> tells the program you want to modify the variable %%a by excluding the path from the variable. </p> <ul> <li><p>example.</p> <pre><code> -variable %%a = C:\folder\filename.fil -variable %%~na = filename. </code></pre> <p>-If you notice however that it leaves the extension .fil off of the filename.</p></li> </ul></li> <li><p>the <code>x</code> in <code>%%~nxa</code> 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.</p> <ul> <li><p>example.</p> <pre><code> -variable %%a = C:\folder\filename.fil -variable %%~xa = .fil </code></pre></li> </ul></li> <li><p>so if you combine both of the modifiers <code>n</code> and <code>x</code> to the variable %%a you will get the full filename including the extension. </p> <ul> <li><p>example:</p> <pre><code> -variable %%a = c:\folder\filename.fil -variable %%~nxa = filename.fil </code></pre></li> </ul></li> </ul> <blockquote> <p>setlocal enabledelayedexpansion</p> </blockquote> <ul> <li>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".</li> </ul> <blockquote> <p>endlocal</p> </blockquote> <ul> <li>this turns off the setlocal enabledelayedexpansion command</li> </ul> <p>To get a very helpful explanation and reference for CMD commands I recommend reading <a href="http://ss64.com/nt/" rel="nofollow">ss64.com</a> and for a great forum to get CMD answers I'd recommend <a href="http://www.dostips.com/" rel="nofollow">dostips.com</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload