此批次代码:
:: File name: jjlist.bat (normally, must be in system path)
::
:: Purpose:
:: batch that lists date/time, justified size and full path and file name.
:: that goes to the screen or into a named file in current directory (folder)
::
:: To use, at the command line type:
:: jjlist *.* [or *.jpg etc]
:: to direct output to screen.
:: or
:: jjlist *.* outfilename.txt
::
:: author: eliasrk(at)gmail.com
::
@echo off
set outfile=%2
if "%1x"=="x" goto :end
if NOT "%2x"=="x" goto :out2file
:
:out2screen
for /r %%I in (%1) do if NOT "%%~tI" == "" call :loop4screen "%%~tI" "%%~zI" "%%I"
goto :end
:
:loop4screen
set Sz= %~2
echo %~1 %Sz:~-10% %~3
goto :end
:
:out2file
for /r %%I in (%1) do if NOT "%%~tI" == "" call :loop4fileOut "%%~tI" "%%~zI" "%%~xI" "%%I"
goto :end
:
:loop4fileOut
set Sz= %~2
set Ex=%~3 x
:echo %~1 %Sz:~-10% %Ex:~0,8% %~4 >> %outfile%
echo %~1 %Sz:~-10% %~4 >> %outfile%
goto :end
:
:end
只要设置了这些注册表项:
[various] "\Control Panel\International" "iTime"="1"
[various] "\Control Panel\International" "iTLZero"="1"
[various] "\Control Panel\International" "sTimeFormat"="HH:mm:ss"
[various] "\Control Panel\International" "sShortDate"="yyyy-MM-dd"
生成此表单的日期字符串可排序列表:
2013-04-16 13:35 1293 D:\Documents\T_Args\Drafts\2013_04_15\1st_circle_claim.txt
2013-04-16 11:51 2110 D:\Documents\T_Args\Drafts\2013_04_15\sources_01.txt
2012-10-08 10:45 13599 D:\Documents\T_Args\images_temp\taut_faq_working\new_tautology.txt
2013-02-05 12:54 8829 D:\Documents\T_Args\Popper\Objective_Knowledge\pages.txt
2013-02-05 11:36 8835 D:\Documents\T_Args\Popper\Objective_Knowledge\pages_org.txt
自写这批文以来,我发现了两个我想要的问题 知道如何纠正:
1)当有“&”时在文件名中生成错误消息指示 那个字符串跟着&被视为“命令”。我想这样 文件名要正常处理。
2)如果我在要列出的目录结构中,说“jjlist * .doc”的输出是 很好,但是如果我提供一个完全限定的路径,如“jjlist d:\ documents * .doc“没有输出。可以修复吗?
感谢您的帮助。
答案 0 :(得分:1)
要保护&
不被解析,您可以将其用双引号括起来或使用delayed expansion
。例如:
@echo off&setlocal set "fname=nice & easy.txt" echo %fname% &rem error message echo "%fname%" &rem output with quotes setlocal enabledelayedexpansion echo !fname! &rem output without quotes
for /r
循环将递归搜索的起始文件夹设为参数,此有效:
for /r "d:\documents" %%I in (*.doc) do echo %%I
但不起作用:
for /r %%I in (d:\documents\*.doc) do echo %%I
如果您需要路径作为递归搜索模式,使用此:
for /f "delims=" %%I in ('dir /b /a-d /s "d:\documents\*.doc"') do echo %%I
答案 1 :(得分:0)
这很慢,但适用于任何区域设置。它需要XP专业版及以上版本。
任何&文件名中的字符处理正常,但路径\文件名中的%和^将导致问题。
可以使用命令行上的path \ filespec以及日志文件名。
:: Syntax: mybatchfile [d:\path\[filespec]] [[d:\path\]logfile.txt]
:: date time size filename
@echo off
for /f "delims=" %%a in ('dir "%~1" /b /s /a-d') do call :next "%%a" "%~2"
if "%~2"=="" pause
goto :EOF
:next
set "file=%~1"
set "file=%file:\=\\%"
WMIC DATAFILE WHERE name="%file%" get lastmodified,size|find ".">file.tmp
for /f "tokens=1,2" %%a in (file.tmp) do set size=%%a&set dt=%%b
set dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2% %dt:~8,2%:%dt:~10,2%
set size= %size%
set size=%size:~-18%
for %%a in ("%dt% %size% %~1") do echo %%~a
if not "%~2"=="" for %%a in ("%dt% %size% %~1") do >>"%~2" echo %%~a
del file.tmp