user1667307
user1667307

Reputation: 2071

How does this particular batch script work

The code below removes the filename exension from files. But I am unable to understand how it does it. It simply list all the file in the current directory in the bare format. But after that I am kind of lost. Could someone please help me out?

@echo off
cd %1
if exist filelisting.txt del filelisting.txt
for /F "delims=" %%j in ('dir /A-D /B /O:GEN') do echo %%~nj >> filelisting.txt

Upvotes: 1

Views: 822

Answers (1)

npocmaka
npocmaka

Reputation: 57322

First files are sorted by extension , then by file name and folders are grouped together (which is redundant) with /O:GEN. With /B full path is excluded and with /A-D folders are excluded. With FOR /F %%J IN ('command') DO (..) every line of command output is set to %%J variable.And with %%~nJ is taken only the file name without extension.Here are some links:

  1. DIR
  2. FOR /F
  3. FILE ATTRIBUTES

EDIT: Example:

@echo off
echo %~0    -   expands %i removing any surrounding quotes (")
echo %~f0   -   expands %i to a fully qualified path name
echo %~d0   -   expands %i to a drive letter only
echo %~p0   -   expands %i to a path only
echo %~n0   -   expands %i to a file name only
echo %~x0   -   expands %i to a file extension only
echo %~s0   -   expanded path contains short names only
echo %~a0   -   expands %i to file attributes of file
echo %~t0   -   expands %i to date/time of file
echo %~z0   -   expands %i to size of file
echo %~$PATH:0  -   searches the directories listed in the PATH environment variable and expands %i to the fully qualified name of the first one found.
echo %~dp0  -   expands %i to a drive letter and path only
echo %~nx0  -   expands %i to a file name and extension only
echo %~fs0  -   expands %i to a full path name with short names only
echo %~dp$PATH:0    -   searches the directories listed in the PATH environment variable for %i and expands to the drive letter and path of the first one found.
echo %~ftza0    -   expands %i to a DIR like output line
pause

Save this s a .bat file.%0 is the path to called .bat file.this will list all %~0 operations. %~ works only over one side enclosed variables - batch file arguments %0, %1 .. or FOR variables.

Upvotes: 1

Related Questions