Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 批处理文件从位置到长度的字符串中获取子字符串_String_Variables_Batch File_Substring_String Length - Fatal编程技术网

String 批处理文件从位置到长度的字符串中获取子字符串

String 批处理文件从位置到长度的字符串中获取子字符串,string,variables,batch-file,substring,string-length,String,Variables,Batch File,Substring,String Length,(2015年3月4日编辑) 这是代码的工作更正版本。 这段代码的目的是获取一些目录和子目录的路径,这些目录和子目录的结构如下所示,以生成一个列表 C:\\\\\种类1\目录 @ECHO off setlocal enabledelayedexpansion SET "_sDr=" SET "_sPt0=" SET "_sPt1=" SET "_sPt2=" SET "_sPt1st=" SET "_sPt2nd=" SET "_sDirYo=" SET "_sDirMo=" SET "_iLen

(2015年3月4日编辑) 这是代码的工作更正版本。 这段代码的目的是获取一些目录和子目录的路径,这些目录和子目录的结构如下所示,以生成一个列表

C:\\\\\种类1\目录

@ECHO off
setlocal enabledelayedexpansion
SET "_sDr="
SET "_sPt0="
SET "_sPt1="
SET "_sPt2="
SET "_sPt1st="
SET "_sPt2nd="
SET "_sDirYo="
SET "_sDirMo="
SET "_iLengthDir="
SET "_iFound1="
SET "_iFound2="
SET "_stri="
SET "_cBckslsh="
SET "_iStart="
SET "_cTab= "
FOR /d %%a IN (*) DO (
    SET "_sDr=%%a"
    SET "_sPt0=!_sDr:~0,1!"
    SET "_sPt1=!_sDr:~1,1!"
    SET "_sPt2=!_sDr:~2,1!"
    SET "_sPt1st=%%~dpa"
    IF "!_sPt0!"=="[" (
        ECHO 1. %%a>>List.txt
    ) ELSE IF "!_sPt2!"=="_" (
        ECHO 2. %%a>>List.txt
    ) ELSE IF "!_sPt1!"=="_" (
        FOR /d %%a IN (!_sPt1st!!_sDr!\*) DO (
            SET "_sPt2nd=%%a"
            SET "_cBckslsh=\"
            SET "_iStart=3"
            CALL :instrc !_sPt2nd!, !_cBckslsh!, !_iStart!
            SET "_iFound1=!_resulti!"
            SET /A _iLengthDir=!_iFound1!-!_iStart!
            CALL :substr !_sPt2nd!, !_iStart!, !_iLengthDir!
            SET "_sDirYo=!_results:~2!"
            SET "_sDirYo=!_sDirYo:_= !"
            SET /A _iFound1=!_iFound1!+1
            CALL :instrc !_sPt2nd!, !_cBckslsh!, !_iFound1!
            SET "_iFound2=!_resulti!"
            SET /A _iLengthDir=!_iFound2!-!_iFound1!
            CALL :substr !_sPt2nd!, !_iFound1!, !_iLengthDir!
            SET "_sDirMo=!_results!"
            SET "_sDirMo=!_sDirMo:_= !"
            ECHO !_sDirYo!%_cTab%!_sDirMo!>>List.txt
        )
    ) ELSE IF "!_sPt0!"=="_" (
        ECHO 4. %%a>>List.txt
    ) ELSE (
        ECHO NA!_cTab!!_sDr:_= !>>List.txt
    )
)
goto :eof

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Function name: instrc                                          ::
::_resulti: returns the position of a character in a string      ::
::parameters:                                                    ::
::_sStringfi: a string which a caharacter will be found in it    ::
::_cToBeFoundfi: a caharacter of which position will be found    ::
::_iStartfi: position at which to start looking for _cToBeFoundfi::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:instrc
SETLOCAL
SET "_sStringfi=%1"
SET "_cToBeFoundfi=%2"
SET "_iStartfi=%3"
SET "_iLocAc="
SET "_cLocAc="
FOR /L %%a IN (%_iStartfi%,1,255) DO (
    SET "_iLocAc=%%a"
    SET "_cLocAc=!_sStringfi:~%%a,1!"
    IF "!_cLocAc!"=="!_cToBeFoundfi!" GOTO ENDF
)
:ENDF
ENDLOCAL & SET "_resulti=%_iLocAc%"

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Function name: substr                                                ::
::_results: returns a substring of a string, of which start point      ::
::and length given                                                     ::
::parameters:                                                          ::
::_sStringfs: a string which a substringwill be taken from it          ::
::_iStartfs: an integer value for starting position                    ::
::_iLengthfs: an integer value for length of substring                 ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:substr
setlocal
set "_sStringfs=%1"
set "_iStartfs=%2"
set "_iLengthfs=%3"
set "_substr_res=!_sStringfs:~%_iStartfs%,%_iLengthfs%!"
endlocal & set "_results=%_substr_res%"
C:\\ uu Genus1\目录

@ECHO off
setlocal enabledelayedexpansion
SET "_sDr="
SET "_sPt0="
SET "_sPt1="
SET "_sPt2="
SET "_sPt1st="
SET "_sPt2nd="
SET "_sDirYo="
SET "_sDirMo="
SET "_iLengthDir="
SET "_iFound1="
SET "_iFound2="
SET "_stri="
SET "_cBckslsh="
SET "_iStart="
SET "_cTab= "
FOR /d %%a IN (*) DO (
    SET "_sDr=%%a"
    SET "_sPt0=!_sDr:~0,1!"
    SET "_sPt1=!_sDr:~1,1!"
    SET "_sPt2=!_sDr:~2,1!"
    SET "_sPt1st=%%~dpa"
    IF "!_sPt0!"=="[" (
        ECHO 1. %%a>>List.txt
    ) ELSE IF "!_sPt2!"=="_" (
        ECHO 2. %%a>>List.txt
    ) ELSE IF "!_sPt1!"=="_" (
        FOR /d %%a IN (!_sPt1st!!_sDr!\*) DO (
            SET "_sPt2nd=%%a"
            SET "_cBckslsh=\"
            SET "_iStart=3"
            CALL :instrc !_sPt2nd!, !_cBckslsh!, !_iStart!
            SET "_iFound1=!_resulti!"
            SET /A _iLengthDir=!_iFound1!-!_iStart!
            CALL :substr !_sPt2nd!, !_iStart!, !_iLengthDir!
            SET "_sDirYo=!_results:~2!"
            SET "_sDirYo=!_sDirYo:_= !"
            SET /A _iFound1=!_iFound1!+1
            CALL :instrc !_sPt2nd!, !_cBckslsh!, !_iFound1!
            SET "_iFound2=!_resulti!"
            SET /A _iLengthDir=!_iFound2!-!_iFound1!
            CALL :substr !_sPt2nd!, !_iFound1!, !_iLengthDir!
            SET "_sDirMo=!_results!"
            SET "_sDirMo=!_sDirMo:_= !"
            ECHO !_sDirYo!%_cTab%!_sDirMo!>>List.txt
        )
    ) ELSE IF "!_sPt0!"=="_" (
        ECHO 4. %%a>>List.txt
    ) ELSE (
        ECHO NA!_cTab!!_sDr:_= !>>List.txt
    )
)
goto :eof

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Function name: instrc                                          ::
::_resulti: returns the position of a character in a string      ::
::parameters:                                                    ::
::_sStringfi: a string which a caharacter will be found in it    ::
::_cToBeFoundfi: a caharacter of which position will be found    ::
::_iStartfi: position at which to start looking for _cToBeFoundfi::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:instrc
SETLOCAL
SET "_sStringfi=%1"
SET "_cToBeFoundfi=%2"
SET "_iStartfi=%3"
SET "_iLocAc="
SET "_cLocAc="
FOR /L %%a IN (%_iStartfi%,1,255) DO (
    SET "_iLocAc=%%a"
    SET "_cLocAc=!_sStringfi:~%%a,1!"
    IF "!_cLocAc!"=="!_cToBeFoundfi!" GOTO ENDF
)
:ENDF
ENDLOCAL & SET "_resulti=%_iLocAc%"

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Function name: substr                                                ::
::_results: returns a substring of a string, of which start point      ::
::and length given                                                     ::
::parameters:                                                          ::
::_sStringfs: a string which a substringwill be taken from it          ::
::_iStartfs: an integer value for starting position                    ::
::_iLengthfs: an integer value for length of substring                 ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:substr
setlocal
set "_sStringfs=%1"
set "_iStartfs=%2"
set "_iLengthfs=%3"
set "_substr_res=!_sStringfs:~%_iStartfs%,%_iLengthfs%!"
endlocal & set "_results=%_substr_res%"
C:\目录

@ECHO off
setlocal enabledelayedexpansion
SET "_sDr="
SET "_sPt0="
SET "_sPt1="
SET "_sPt2="
SET "_sPt1st="
SET "_sPt2nd="
SET "_sDirYo="
SET "_sDirMo="
SET "_iLengthDir="
SET "_iFound1="
SET "_iFound2="
SET "_stri="
SET "_cBckslsh="
SET "_iStart="
SET "_cTab= "
FOR /d %%a IN (*) DO (
    SET "_sDr=%%a"
    SET "_sPt0=!_sDr:~0,1!"
    SET "_sPt1=!_sDr:~1,1!"
    SET "_sPt2=!_sDr:~2,1!"
    SET "_sPt1st=%%~dpa"
    IF "!_sPt0!"=="[" (
        ECHO 1. %%a>>List.txt
    ) ELSE IF "!_sPt2!"=="_" (
        ECHO 2. %%a>>List.txt
    ) ELSE IF "!_sPt1!"=="_" (
        FOR /d %%a IN (!_sPt1st!!_sDr!\*) DO (
            SET "_sPt2nd=%%a"
            SET "_cBckslsh=\"
            SET "_iStart=3"
            CALL :instrc !_sPt2nd!, !_cBckslsh!, !_iStart!
            SET "_iFound1=!_resulti!"
            SET /A _iLengthDir=!_iFound1!-!_iStart!
            CALL :substr !_sPt2nd!, !_iStart!, !_iLengthDir!
            SET "_sDirYo=!_results:~2!"
            SET "_sDirYo=!_sDirYo:_= !"
            SET /A _iFound1=!_iFound1!+1
            CALL :instrc !_sPt2nd!, !_cBckslsh!, !_iFound1!
            SET "_iFound2=!_resulti!"
            SET /A _iLengthDir=!_iFound2!-!_iFound1!
            CALL :substr !_sPt2nd!, !_iFound1!, !_iLengthDir!
            SET "_sDirMo=!_results!"
            SET "_sDirMo=!_sDirMo:_= !"
            ECHO !_sDirYo!%_cTab%!_sDirMo!>>List.txt
        )
    ) ELSE IF "!_sPt0!"=="_" (
        ECHO 4. %%a>>List.txt
    ) ELSE (
        ECHO NA!_cTab!!_sDr:_= !>>List.txt
    )
)
goto :eof

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Function name: instrc                                          ::
::_resulti: returns the position of a character in a string      ::
::parameters:                                                    ::
::_sStringfi: a string which a caharacter will be found in it    ::
::_cToBeFoundfi: a caharacter of which position will be found    ::
::_iStartfi: position at which to start looking for _cToBeFoundfi::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:instrc
SETLOCAL
SET "_sStringfi=%1"
SET "_cToBeFoundfi=%2"
SET "_iStartfi=%3"
SET "_iLocAc="
SET "_cLocAc="
FOR /L %%a IN (%_iStartfi%,1,255) DO (
    SET "_iLocAc=%%a"
    SET "_cLocAc=!_sStringfi:~%%a,1!"
    IF "!_cLocAc!"=="!_cToBeFoundfi!" GOTO ENDF
)
:ENDF
ENDLOCAL & SET "_resulti=%_iLocAc%"

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Function name: substr                                                ::
::_results: returns a substring of a string, of which start point      ::
::and length given                                                     ::
::parameters:                                                          ::
::_sStringfs: a string which a substringwill be taken from it          ::
::_iStartfs: an integer value for starting position                    ::
::_iLengthfs: an integer value for length of substring                 ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:substr
setlocal
set "_sStringfs=%1"
set "_iStartfs=%2"
set "_iLengthfs=%3"
set "_substr_res=!_sStringfs:~%_iStartfs%,%_iLengthfs%!"
endlocal & set "_results=%_substr_res%"
C:\\ U系列\目录

@ECHO off
setlocal enabledelayedexpansion
SET "_sDr="
SET "_sPt0="
SET "_sPt1="
SET "_sPt2="
SET "_sPt1st="
SET "_sPt2nd="
SET "_sDirYo="
SET "_sDirMo="
SET "_iLengthDir="
SET "_iFound1="
SET "_iFound2="
SET "_stri="
SET "_cBckslsh="
SET "_iStart="
SET "_cTab= "
FOR /d %%a IN (*) DO (
    SET "_sDr=%%a"
    SET "_sPt0=!_sDr:~0,1!"
    SET "_sPt1=!_sDr:~1,1!"
    SET "_sPt2=!_sDr:~2,1!"
    SET "_sPt1st=%%~dpa"
    IF "!_sPt0!"=="[" (
        ECHO 1. %%a>>List.txt
    ) ELSE IF "!_sPt2!"=="_" (
        ECHO 2. %%a>>List.txt
    ) ELSE IF "!_sPt1!"=="_" (
        FOR /d %%a IN (!_sPt1st!!_sDr!\*) DO (
            SET "_sPt2nd=%%a"
            SET "_cBckslsh=\"
            SET "_iStart=3"
            CALL :instrc !_sPt2nd!, !_cBckslsh!, !_iStart!
            SET "_iFound1=!_resulti!"
            SET /A _iLengthDir=!_iFound1!-!_iStart!
            CALL :substr !_sPt2nd!, !_iStart!, !_iLengthDir!
            SET "_sDirYo=!_results:~2!"
            SET "_sDirYo=!_sDirYo:_= !"
            SET /A _iFound1=!_iFound1!+1
            CALL :instrc !_sPt2nd!, !_cBckslsh!, !_iFound1!
            SET "_iFound2=!_resulti!"
            SET /A _iLengthDir=!_iFound2!-!_iFound1!
            CALL :substr !_sPt2nd!, !_iFound1!, !_iLengthDir!
            SET "_sDirMo=!_results!"
            SET "_sDirMo=!_sDirMo:_= !"
            ECHO !_sDirYo!%_cTab%!_sDirMo!>>List.txt
        )
    ) ELSE IF "!_sPt0!"=="_" (
        ECHO 4. %%a>>List.txt
    ) ELSE (
        ECHO NA!_cTab!!_sDr:_= !>>List.txt
    )
)
goto :eof

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Function name: instrc                                          ::
::_resulti: returns the position of a character in a string      ::
::parameters:                                                    ::
::_sStringfi: a string which a caharacter will be found in it    ::
::_cToBeFoundfi: a caharacter of which position will be found    ::
::_iStartfi: position at which to start looking for _cToBeFoundfi::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:instrc
SETLOCAL
SET "_sStringfi=%1"
SET "_cToBeFoundfi=%2"
SET "_iStartfi=%3"
SET "_iLocAc="
SET "_cLocAc="
FOR /L %%a IN (%_iStartfi%,1,255) DO (
    SET "_iLocAc=%%a"
    SET "_cLocAc=!_sStringfi:~%%a,1!"
    IF "!_cLocAc!"=="!_cToBeFoundfi!" GOTO ENDF
)
:ENDF
ENDLOCAL & SET "_resulti=%_iLocAc%"

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Function name: substr                                                ::
::_results: returns a substring of a string, of which start point      ::
::and length given                                                     ::
::parameters:                                                          ::
::_sStringfs: a string which a substringwill be taken from it          ::
::_iStartfs: an integer value for starting position                    ::
::_iLengthfs: an integer value for length of substring                 ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:substr
setlocal
set "_sStringfs=%1"
set "_iStartfs=%2"
set "_iLengthfs=%3"
set "_substr_res=!_sStringfs:~%_iStartfs%,%_iLengthfs%!"
endlocal & set "_results=%_substr_res%"
C:\\\\\\\\\\\\\种类1\目录

@ECHO off
setlocal enabledelayedexpansion
SET "_sDr="
SET "_sPt0="
SET "_sPt1="
SET "_sPt2="
SET "_sPt1st="
SET "_sPt2nd="
SET "_sDirYo="
SET "_sDirMo="
SET "_iLengthDir="
SET "_iFound1="
SET "_iFound2="
SET "_stri="
SET "_cBckslsh="
SET "_iStart="
SET "_cTab= "
FOR /d %%a IN (*) DO (
    SET "_sDr=%%a"
    SET "_sPt0=!_sDr:~0,1!"
    SET "_sPt1=!_sDr:~1,1!"
    SET "_sPt2=!_sDr:~2,1!"
    SET "_sPt1st=%%~dpa"
    IF "!_sPt0!"=="[" (
        ECHO 1. %%a>>List.txt
    ) ELSE IF "!_sPt2!"=="_" (
        ECHO 2. %%a>>List.txt
    ) ELSE IF "!_sPt1!"=="_" (
        FOR /d %%a IN (!_sPt1st!!_sDr!\*) DO (
            SET "_sPt2nd=%%a"
            SET "_cBckslsh=\"
            SET "_iStart=3"
            CALL :instrc !_sPt2nd!, !_cBckslsh!, !_iStart!
            SET "_iFound1=!_resulti!"
            SET /A _iLengthDir=!_iFound1!-!_iStart!
            CALL :substr !_sPt2nd!, !_iStart!, !_iLengthDir!
            SET "_sDirYo=!_results:~2!"
            SET "_sDirYo=!_sDirYo:_= !"
            SET /A _iFound1=!_iFound1!+1
            CALL :instrc !_sPt2nd!, !_cBckslsh!, !_iFound1!
            SET "_iFound2=!_resulti!"
            SET /A _iLengthDir=!_iFound2!-!_iFound1!
            CALL :substr !_sPt2nd!, !_iFound1!, !_iLengthDir!
            SET "_sDirMo=!_results!"
            SET "_sDirMo=!_sDirMo:_= !"
            ECHO !_sDirYo!%_cTab%!_sDirMo!>>List.txt
        )
    ) ELSE IF "!_sPt0!"=="_" (
        ECHO 4. %%a>>List.txt
    ) ELSE (
        ECHO NA!_cTab!!_sDr:_= !>>List.txt
    )
)
goto :eof

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Function name: instrc                                          ::
::_resulti: returns the position of a character in a string      ::
::parameters:                                                    ::
::_sStringfi: a string which a caharacter will be found in it    ::
::_cToBeFoundfi: a caharacter of which position will be found    ::
::_iStartfi: position at which to start looking for _cToBeFoundfi::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:instrc
SETLOCAL
SET "_sStringfi=%1"
SET "_cToBeFoundfi=%2"
SET "_iStartfi=%3"
SET "_iLocAc="
SET "_cLocAc="
FOR /L %%a IN (%_iStartfi%,1,255) DO (
    SET "_iLocAc=%%a"
    SET "_cLocAc=!_sStringfi:~%%a,1!"
    IF "!_cLocAc!"=="!_cToBeFoundfi!" GOTO ENDF
)
:ENDF
ENDLOCAL & SET "_resulti=%_iLocAc%"

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::Function name: substr                                                ::
::_results: returns a substring of a string, of which start point      ::
::and length given                                                     ::
::parameters:                                                          ::
::_sStringfs: a string which a substringwill be taken from it          ::
::_iStartfs: an integer value for starting position                    ::
::_iLengthfs: an integer value for length of substring                 ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:substr
setlocal
set "_sStringfs=%1"
set "_iStartfs=%2"
set "_iLengthfs=%3"
set "_substr_res=!_sStringfs:~%_iStartfs%,%_iLengthfs%!"
endlocal & set "_results=%_substr_res%"
到目前为止,您还没有:

@ECHO off
setlocal enabledelayedexpansion
SET "_string=abcdefghij"
SET "_length=3"
SET "_substr="

REM See below:
SET "_substr=!_string:~4,%_length%!"

ECHO !_substr!
对于原始代码,可以使用另一个函数来执行此操作。而不是:

SET /A _ptkYou=!_ptke!-24
::::Here is the original problem::::
SET "_ptkYo=!_ptkY!:~24,!_ptkYou!"
ECHO !_ptkYo!>>Liste.txt
使用:

然后在末尾添加以下内容:

:substr
setlocal
set "_a=%1"
set "_b=%2"
set "_substr_res=!_a:~4,%_b%!"
endlocal & set "_result2=%_substr_res%"

这很有效。但在原始代码中,它不起作用_对于循环,
中原始代码中的“长度”变量,它是从每个循环中的其他变量计算出来的;像
SET/A\u length=_位置-24
您可以编辑您的问题以包括for循环和location变量吗?我对您的函数做了一些更改,现在它需要三个参数。我在5月份的消息中更改了所有工作版本的代码。再次感谢您的解决方案。