string - 使用批处理文件检查文件路径的长度。 #字符串长度

标签 string batch-file filepath string-length

这是我的第一篇文章,希望有人能帮助我。

我对 .bat 文件相当陌生,但我对 php 和 c++ 有基本的了解。

我需要编写一个批处理文件来执行以下操作: 1. 显示当前文件夹中的所有文件,包括完整文件路径。 2. 显示所显示文件的长度,包括每个单独文件的文件路径。 3. 也许会显示总共有多少个文件。

到目前为止我已经写了这个,它执行了作业 #1 和 #3,但我无法让它显示字符串长度。

@echo off
for /r . %%g in (*.*) do (
    echo %%g
)       
set cnt=0
for %%A in (*) do set /a cnt+=1
set /A cnt=cnt-1
echo Dateien im Verzeichnis = %cnt%
PAUSE

我发现了另外两个脚本,每个脚本都标识字符串或路径的长度,但我无法将它们与我的第一个脚本放在一起。

这里是字符串长度:

@echo off
set myvar="some string"
rem compute the length of a string
set #=%myvar%
set length=0
:loop
if defined # (
    rem shorten string by one character
    set #=%#:~1%
    rem increment the string count variable %length%
    set /A length += 1
    rem repeat until string is null
    goto loop
)
echo myvar is %length% characters long!
PAUSE

这里将显示当前文件夹的路径长度:

@echo off
echo %cd%>"%TMP%\Temp.txt"
for %%l in ("%TMP%\Temp.txt") do set /a len=%%~zl
del "%TMP%\Temp.txt"
set /a len-=2
echo Path length = %len% chars.
PAUSE

最佳答案

这是基于 this :length util function 的解决方案。有关批处理脚本函数的更多信息,see this page 。您还应该read more about delayed expansion 。即使不使用延迟扩展,setlocal ought to be included在所有脚本的顶部。

@echo off
setlocal

set "files=0"

for %%I in (*) do (
    set /a files += 1
    call :length len "%%~fI"

    rem // delay expansion of %files% and %len% because of reasons
    setlocal enabledelayedexpansion
    echo !files!: %%~fI is !len! characters long.
    endlocal
)

goto :EOF

:length <return_var> <string>
setlocal enabledelayedexpansion
if "%~2"=="" (set ret=0) else set ret=1
set "tmpstr=%~2"
for %%I in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
    if not "!tmpstr:~%%I,1!"=="" (
        set /a ret += %%I
        set "tmpstr=!tmpstr:~%%I!"
    )
)
endlocal & set "%~1=%ret%"
goto :EOF

关于string - 使用批处理文件检查文件路径的长度。 #字符串长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37345709/

相关文章:

使用 chrome 文件系统写入时,Javascript 字符串中存在 NULL 字符

c++ - 查找字符串的所有可能排列

c# - 从 C# 应用程序调用运行文件时出现问题

c# - 将路径转换为其 GUID 形式

r - 当通过 `r BATCH script file` 调用时,如何在 R 中获取当前执行脚本的名称

c - 将排列后的字符串存储到数组中

mysql - FIND_IN_SET 用于 Mysql 中的部分字符串

batch-file - 如何检测拖放参数是否为文件夹?

windows - 将环境变量设置为函数结果

rust - 如何在文件名之前的路径中添加文件夹?