![](/img/trans.png)
[英]Windows batch file to find duplicates size files in harddisk (no matter the name and the extension)
[英]Find files and sort by size in a Windows batch file
我的批处理脚本具有命令行参数,其中包含文件名和文件夹列表。 对于每个文件名,我需要打印找到文件的文件夹的所有子文件夹(该文件的路径)。 子文件夹名称应按文件大小的降序排序(文件在不同的子文件夹中可以具有各种大小)。
到目前为止,我已经做到了,但是没有用:
::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
既然这个学期已经过去了,我回答这个家庭作业问题不会感到内gui。 使用Windows Batch递归打印文件夹和文件是一个封闭的重复问题,讨论了分配。
我最初的解决方法很简单。 有一些技巧可以确保它正确处理其中带有特殊字符的路径,但不要太花哨。 唯一的其他技巧是用空格填充文件大小,以便SORT正常工作。
就像在原始问题中一样,第一个参数应该是文件夹路径(。\\可以正常工作),随后的参数代表文件名(通配符可以)。
@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
我曾希望通过将重定向和后续排序替换为直接进行排序的管道来避免创建临时文件。 但这是行不通的。 (请参阅我的相关问题: 为什么在管道式代码块中延迟扩展失败? )
我的第一个解决方案运行良好,但根据提供的输入,可能会出现重复输出的情况。 我决定写一个可以清除重复文件报告的版本。
基本前提很简单-将所有输出保存到一个临时文件,并将文件名添加到已排序字符串的开头。 然后,我需要遍历结果,仅在文件和/或路径更改时才打印信息。
最后一个循环是棘手的部分,因为文件名可以包含特殊字符,如!
^
&
和%
可能会导致问题,具体取决于所使用的扩展类型。 我需要在循环中设置和比较变量,这通常需要延迟扩展。 但是延迟扩展会导致FOR变量扩展出现问题!
被发现。 我可以通过在循环外部调用来避免延迟扩展,但是之后FOR变量变得不可用。 我可以将变量作为参数传递给CALLed例程,而无需延迟扩展,但是随后遇到%
^
和&
。 我可以使用SETLOCAL / ENDLOCAL玩游戏,但随后我需要担心会在ENDLOCAL障碍中传递值,这需要相当复杂的转义过程。 问题变成一个大恶性循环。
另一种自我强加的约束是我不想将文件和路径输出括在引号中,因此这意味着我必须使用延迟扩展,FOR变量或转义值。
我发现了一个有趣的解决方案,它利用了FOR变量的一个奇怪功能。
通常,FOR变量的范围严格在循环内。 如果在循环外调用,则FOR变量值将不再可用。 但是,如果您随后在被调用的过程中发出FOR语句,则调用方的FOR变量将再次可见! 问题解决了!
@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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.