5

此批代码:

    :: 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) 如果我在要列出的 dir 结构中,say "jjlist *.doc" 的输出很好,但是如果我在 "jjlist d:\documents*.doc" 中提供完全限定的路径,则没有输出。这可以解决吗?

谢谢你的帮助。

4

2 回答 2

1

好又容易

为了防止&解析,您可以将其括在双引号中使用delayed expansion. 例子:

@echo off&setlocal
设置“fname=nice & easy.txt”
echo %fname% &rem 错误信息
echo "%fname%" &rem 输出带引号
setlocal enabledelayedexpansion     
回声!fname!&rem 输出不带引号

为 /r

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
于 2013-05-25T16:50:30.427 回答
0

这很慢,但它适用于任何区域设置。它需要 XP pro 及以上版本。

文件名中的任何 & 字符都可以处理,但路径\文件名中的 % 和 ^ 会导致问题。

可以使用命令行上的 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
于 2013-05-25T16:41:35.520 回答