Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two problems with a batch to recursively list pathfilename with datetime and size

Tags:

batch-file

This batch code:

    :: 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

provided that these registry keys are set:

[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"

produces a date string sortable listing of this form:

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

Since writing this batch I have discovered two problems which I would like to know how to correct:

1) When there is an "&" in a filename an error message is generated indicating that the string following the & was treated as a "command". I would like such filenames to be treated normally.

2) If I am in the dir structure to be listed the output of say "jjlist *.doc" is fine, however if I provide a fully qualified path as in "jjlist d:\documents*.doc" there is no output. Can this be fixed?

Thanks for any help.

like image 252
Friar Broccoli Avatar asked Nov 12 '22 04:11

Friar Broccoli


1 Answers

nice & easy

To protect the & from parsing, you can enclose it in double quotes or use delayed expansion. Example:

@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

The for /r loop gets its start folder for the recursive search as parameter, this works:

for /r "d:\documents" %%I in (*.doc) do echo %%I

but this doesn't work:

for /r %%I in (d:\documents\*.doc) do echo %%I

If you need a path as recursive search pattern, use this:

for /f "delims=" %%I in ('dir /b /a-d /s "d:\documents\*.doc"') do echo %%I
like image 136
Endoro Avatar answered Jan 04 '23 02:01

Endoro