查找大小的文件和排序在Windows批处理文件 [英] Find files and sort by size in a Windows batch file

查看:106
本文介绍了查找大小的文件和排序在Windows批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有作为命令行参数,以我的批处理脚本文件名和文件夹列表。对于每一个文件名,我需要打印文件所在的文件中找到的文件夹(文件的路径)的所有子文件夹。子文件夹名称应该按降序文件大小的顺序(该文件可以在不同的子文件夹不同大小)进行排序。

我已经这样做了,到目前为止,但它不工作:

  ::验证,如果第一个参数就是目录关闭@echo
REM检查参数的数量
如果%2==转到ERR1
REM检查:是第一个参数一个目录?
如果NOT EXIST%1 \\ NUL转到ERR2
集合D =%1
转移
REM遍历其余参数
为%%我在DIR%做(
找到%DIR /名称%I>温度如果有杜/ B温度|切/ F 1页转到ERR3
 MYVAR = TYPE温度
回声文件%i的是:在%MYVAR %% j执行
 回声引用%j 回音后排序
杜/ B%MYVAR |排序/ NR
):ERR1
呼应的两个参数是必要的
转到结束:ERR2
回声第一个参数必须是一个目录。
转到结束:ERR3
回声文件不存在。
转到结束:结束


解决方案

我现在不觉得内疚回答这个功课疑问,学期早已过去。 递归使用Windows批量打印文件夹和文件是一个封闭的重复的问题,讨论的分配。

我最初的解决方案是相当直截了当。有一些技巧,以确保它正确处理与他们的特殊字符的路径,但没有什么太花哨。其他唯一的诀窍是左填充用空格的文件大小,以便SORT正常工作。

就像在原来的问题,第一个参数应该是一个文件夹路径(\\工作得很好),及以后的参数重新present文件名(通配符OK)。

 关闭@echo
SETLOCAL disableDelayedExpansion
设置临时文件=%temp%\\ _ mysort%随机%.TXT集根=
为%% f由于(%*)做(
  如果没有定义根(
    PUSHD %%˚F||退出/ B
    设置根= 1
  )其他(
    回声(
    呼应%%〜NXF
    回声--------------------------------------------
    (
      关闭@echo
      FOR / FEOL =:delims =%%的A('DIR / S / B%%〜NXF')做(
        将mypath中= %%〜DPA
        设置大小= %%〜ZA
        SETLOCAL enableDelayedExpansion
        一套!大小=!〜-12
        回音!大小! !mypath中!
        ENDLOCAL
      )
    )>%临时文件%
        排序/ R%临时文件%
  )

如果存在%临时文件%德尔%临时文件%
如果定义了根POPD

我曾希望通过与管道直接排序代替重定向和随后的排序,避免了临时文件的创建。但是,这是行不通的。 (见我相关的问题:<一href=\"http://stackoverflow.com/questions/8192318/why-does-delayed-expansion-fail-when-inside-a-piped-block-of-$c$c\">Why并推迟扩张失败时,code的管道块内?)

我的第一个解决方案运作良好,除了有对依赖提供什么样的输入输出重复的潜力。我决定写处理掉重复的文件报告的版本。

基本premise很简单 - 保存所有输出给一个临时文件与添加到排序字符串的前面的文件名。然后,我需要通过循环的结果,只有当文件和/或路径更改打印信息。

最后一个循环是棘手的部分,因为文件名可以包含特殊字符,如 ^ &安培; 可能会导致问题取决于使用什么类型的扩展。我需要一个循环,这通常需要延迟扩展中设置和比较变量。但是,当发现推迟扩张导致与FOR变量扩展的问题。我可以通过调用外循环以免延误扩张,但随后的变量变得不可用。我可以通过该变量作为参数传递给被调用函数无延迟扩展,但后来我碰到与 ^ 问题和&安培; 。我可以SETLOCAL / ENDLOCAL玩游戏,但我需要担心整个ENDLOCAL障碍,这需要一个相当复杂的逃跑过程中传递值。这个问题变成一个大的恶性循环。

另外一个自我强加的约束是我不想用引号括文件和路径输出,这样就意味着我必须用延迟扩展,变量或转义的值。

我发现,利用的变量奇数特征的有趣的解决方案。

的变量通常的范围是严格的循环中。如果调用外循环,那么FOR变量值将不再可用。但是,如果你再发出一种在被调用过程声明 - 变量调用者变得可见再次 问题解决了

 关闭@echo
SETLOCAL disableDelayedExpansion
设置临时文件=%temp%\\ _ mysort%随机%.TXT
如果存在%临时文件%德尔%临时文件%集根=

  为%% f由于(%*)做(
    如果没有定义根(
      PUSHD %%˚F||退出/ B
      设置根= 1
    )其他(
      设置文件= %%〜NXF
      FOR / FEOL =:delims =%%的A('DIR / S / B%%〜NXF')做(
        将mypath中= %%〜DPA
        设置大小= %%〜ZA
        SETLOCAL enableDelayedExpansion
        一套!大小=!〜-12
        回声(!文件!/!大小!/!mypath中!
        ENDLOCAL
      )
    )
  )
)&GT;%临时文件%设置文件=
集mypath中=
FOR / F令牌= 1-3 = EOL / delims = /%%的('排序/ R%临时文件%')不要叫答:PROC如果存在%临时文件%德尔%临时文件%
如果定义了根POPD
退出/ B:PROC
  对于%%中的Z(1)做(
    如果文件%%NEQ%% A(
      设置文件= %% A
      集mypath中=
      回声(
      回声%%一
       回声--------------------------------------------
    )
  )
  对于%%中的Z(1)做(
    如果%mypath中%NEQ%% C(
      将mypath中= %% C
      呼应%%乙%%Ç
    )
  )
退出/ B

I have as command-line parameters to my batch script a list of filenames and a folder. For each filename, I need to print all subfolders of the folder where the file is found (the path of that file). The subfolder names should be sorted in descending order of the file sizes (the file can have various sizes in different subfolders).

I have done this so far, but it doesn't work:

::verify if the first parameter is the directory

@echo off
REM check the numbers of parameters
if "%2"=="" goto err1
REM check: is first parameter a directory?
if NOT EXIST %1\NUL goto err2
set d=%1
shift
REM iterate the rest of the parameters


for %%i in %dir  do (
find %dir /name %i > temp

if EXIST du /b temp | cut /f 1 goto err3 
 myvar=TYPE temp
echo "file " %i "is in: "

for %%j in %myvar do
 echo %j

 echo after sort
du /b %myvar | sort /nr       
)

:err1
echo Two parameters are necessary 
goto end

:err2
echo First parameter must be a directory.
goto end

:err3 
echo file does not exist.
goto end

:end

解决方案

I don't feel guilty answering this homework question now that the semester is long past. Print folders and files recursively using Windows Batch is a closed duplicate question that discusses the assignment.

My initial solution is fairly straight forward. There are a few tricks to make sure it properly handles paths with special characters in them, but nothing too fancy. The only other trick is left padding the file size with spaces so that SORT works properly.

Just as in the original question, the 1st parameter should be a folder path (.\ works just fine), and subsequent arguments represent file names (wildcards are OK).

@echo off
setlocal disableDelayedExpansion
set tempfile="%temp%\_mysort%random%.txt"

set "root="
for %%F in (%*) do (
  if not defined root (
    pushd %%F || exit /b
    set root=1
  ) else (
    echo(
    echo %%~nxF
    echo --------------------------------------------
    (
      @echo off
      for /f "eol=: delims=" %%A in ('dir /s /b "%%~nxF"') do (
        set "mypath=%%~dpA"
        set "size=            %%~zA"
        setlocal enableDelayedExpansion
        set "size=!size:~-12!"
        echo !size! !mypath!
        endlocal
      )
    ) >%tempfile%
        sort /r %tempfile%
  )
)
if exist %tempfile% del %tempfile%
if defined root popd

I had hoped to avoid creation of a temporary file by replacing the redirect and subsequent sort with a pipe directly to sort. But this does not work. (see my related question: Why does delayed expansion fail when inside a piped block of code?)

My first solution works well, except there is the potential for duplicate output depending on what input is provided. I decided I would write a version that weeds out duplicate file reports.

The basic premise was simple - save all output to one temp file with the file name added to the front of the sorted strings. Then I need to loop through the results and only print information when the file and/or the path changes.

The last loop is the tricky part, because file names can contain special characters like ! ^ & and % that can cause problems depending on what type of expansion is used. I need to set and compare variables within a loop, which usually requires delayed expansion. But delayed expansion causes problems with FOR variable expansion when ! is found. I can avoid delayed expansion by calling outside the loop, but then the FOR variables become unavailable. I can pass the variables as arguments to a CALLed routine without delayed expansion, but then I run into problems with % ^ and &. I can play games with SETLOCAL/ENDLOCAL, but then I need to worry about passing values across the ENDLOCAL barrier, which requires a fairly complex escape process. The problem becomes a big vicious circle.

One other self imposed constraint is I don't want to enclose the file and path output in quotes, so that means I must use delayed expansion, FOR variables, or escaped values.

I found an interesting solution that exploits an odd feature of FOR variables.

Normally the scope of FOR variables is strictly within the loop. If you CALL outside the loop, then the FOR variable values are no longer available. But if you then issue a FOR statement in the called procedure - the caller FOR variables become visible again! Problem solved!

@echo off
setlocal disableDelayedExpansion
set tempfile="%temp%\_mysort%random%.txt"
if exist %tempfile% del %tempfile%

set "root="
(
  for %%F in (%*) do (
    if not defined root (
      pushd %%F || exit /b
      set root=1
    ) else (
      set "file=%%~nxF"
      for /f "eol=: delims=" %%A in ('dir /s /b "%%~nxF"') do (
        set "mypath=%%~dpA"
        set "size=            %%~zA"
        setlocal enableDelayedExpansion
        set "size=!size:~-12!"
        echo(!file!/!size!/!mypath!
        endlocal
      )
    )
  )
)>%tempfile%

set "file="
set "mypath="
for /f "tokens=1-3 eol=/ delims=/" %%A in ('sort /r %tempfile%') do call :proc

if exist %tempfile% del %tempfile%
if defined root popd
exit /b

:proc
  for %%Z in (1) do (
    if "%file%" neq "%%A" (
      set "file=%%A"
      set "mypath="
      echo(
      echo %%A
       echo --------------------------------------------
    )
  )
  for %%Z in (1) do (
    if "%mypath%" neq "%%C" (
      set "mypath=%%C"
      echo %%B %%C
    )
  )
exit /b

这篇关于查找大小的文件和排序在Windows批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆