我正在尝试使用for
循环批量创建自定义目录列表。
这是代码:
for /F "tokens=* UseBackQ" %%g IN (`dir /b %2`) do (
echo ^| %%~ag %%~tg %%~zg %%~nxg && echo ^|
)
E:
%2
的输出为:
| d---------- 2022-08-19 02:14 PM 0 AppTemp
|
| --a-------- 2022-08-06 11:40 PM 971 learned.py
|
| --a-------- 2022-08-19 05:00 PM 4269 main.cmd
|
我希望文件大小是这样的:
| d---------- 2022-08-19 02:14 PM 0 AppTemp
|
| --a-------- 2022-08-06 11:40 PM 971 learned.py
|
| --a-------- 2022-08-19 05:00 PM 4269 main.cmd
|
我怎么能把它变成这样?
可以使用以下一点点注释的批处理文件代码:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Make the directory passed as second argument the current directory
rem checking if a second, non-empty argument string is passed at all to
rem the batch file and with checking if that is really a directory name.
set "RestoreCurrentDirectory="
if "%~2" == "" (
echo No directory specified as second argument, listing the directory:
echo "%CD%"
echo(
) else pushd "%~2" && set "RestoreCurrentDirectory=1" || (
echo "%~2" is not a valid directory.
echo(
exit /B 1
)
rem Get the file size of the largest file in the current directory
rem assigned to environment variable with the name LeadingSpaces.
set "LeadingSpaces="
for /F "eol=| delims=" %%I in ('dir /A-D /B /O-S 2^>nul') do (
set "LeadingSpaces=%%~zI"
if defined LeadingSpaces goto GetSpaces
)
set "LeadingSpaces= "
set "NumberSpaces=1"
goto OutputFileNames
rem Replace all digits in file size of largest file by spaces. The string
rem value of the environment variable LeadingSpaces has exactly the same
rem number of spaces as the file size of the largest file in the current
rem directory has as string. Next determine the number of spaces using code
rem from https://www.dostips.com/DtTipsStringOperations.php#Function.strLen
:GetSpaces
setlocal EnableDelayedExpansion
for %%I in (0 1 2 3 4 5 6 7 8 9) do set "LeadingSpaces=!LeadingSpaces:%%I= !"
set "TempString=#%LeadingSpaces%"
set "NumberSpaces=0"
for /L %%I in (12,-1,0) do (
set /A "NumberSpaces|=1<<%%I"
for %%J in (!NumberSpaces!) do if "!TempString:~%%J,1!" == "" set /A "NumberSpaces&=~1<<%%I"
)
endlocal & set "LeadingSpaces=%LeadingSpaces%" & set "NumberSpaces=%NumberSpaces%"
rem List all files in the specified directory with right aligned file size
rem with the definition of environment variable FileSize with the leading
rem spaces on which the real file size is appended and output only the last
rem NumberSpaces characters of the file size string with spaces at beginning.
:OutputFileNames
for /F "eol=| delims=" %%I in ('dir /A-D /B 2^>nul') do (
set "Attributes= | %%~aI"
set "FileTime=%%~tI"
set "FileSize=%LeadingSpaces%%%~zI"
set "FileName=%%~nxI"
setlocal EnableDelayedExpansion
echo !Attributes! !FileTime! !FileSize:~-%NumberSpaces%! !FileName!& echo ^|
endlocal
)
if defined RestoreCurrentDirectory popd
endlocal
注意力:
与直接从文件系统获取数据的命令DIcmd.exe
R相比,Windows 命令处理器cmd.exe
无法获取具有%%~aI
的文件属性、%%~tI
的上次修改时间和具有共享访问权限的文件%%~zI
的文件大小
。如果在%SystemDrive%
第二个参数的情况下调用此批处理文件,并且系统驱动器的根目录仅包含hiberfil.sys
和pagefile.sys
两个文件,则批处理文件的输出为:
| hiberfil.sys
|
| pagefile.sys
|
隐藏的文件属性和hiberfil.sys
和pagefile.sys
的系统无关紧要。共享冲突是无法获取这两个文件的所有数据以进行输出的原因。
运行DIR并解析其输出可能会更好,但DIR输出的日期/时间格式取决于为当前用户帐户配置的区域/国家/地区,这使得很可能无法对批处理文件进行编码以在所有 Windows 计算机上工作。
要了解使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,然后完整而仔细地阅读每个命令的显示帮助页面。
dir /?
echo /?
endlocal /?
exit /?
for /?
if /?
popd /?
pushd /?
rem /?
set /?
setlocal /?
阅读有关使用命令重定向运算符的Microsoft文档,了解2>nul
的说明。当 Windows 命令解释器在执行命令 FOR 之前处理此命令行时,重定向运算符>
必须使用插入符号^
转义,以便在执行命令FOR之前将其解释为文字字符,该命令使用在后台启动的单独命令进程执行嵌入的dir
命令行。
另请参阅使用 Windows 批处理文件的单行多个命令,了解无条件命令运算符&
和条件命令运算符&&
和||
的说明。