MS-DOS批处理脚本:赋值操作

发布于 2024-11-14 10:54:23 字数 2003 浏览 2 评论 0原文

我指的是以下威胁 批处理文件:如何读取文件?< /a>.用于从文本文件中逐行检索。我正在使用以下脚本:

@echo off
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ paths.txt"`) do (
    set "var=%%a"
    SETLOCAL EnableDelayedExpansion
    set "var=!var:*:=!"
    echo !var!
    ENDLOCAL
)

代码工作正常! !var! 中的值不能分配给任何变量。 :( 我在那里进行结构,请任何人帮助逐行阅读,我希望分配给某个变量并想要操作该变量。请帮助解决这种情况。

更新:

@ECHO off

CLS

SET PROJ_DIR=D:\workspace\proj
SET PROMO_DIR=D:\TEST
SET SOURCE_CODE=\Source Code

SETLOCAL DisableDelayedExpansion
for /f %%a in (paths.txt) do (
    SET "var=%%a"
    SETLOCAL EnableDelayedExpansion
    set "var=!var:*:=!"
    set FILE_PATH=!var://www.domain.com/path/dir=!
    SET FILE_PATH=!FILE_PATH:/=\!
    SET PROMO_FILE_PATH=!PROMO_DIR!!SOURCE_CODE!!FILE_PATH! 
    FOR %%i IN ("!PROMO_FILE_PATH!") DO SET FOLDER_PATH=%%~dpi
    FOR %%i IN ("!PROMO_FILE_PATH!") DO SET FILE_NAME=%%~nxi
    IF EXIST "!FOLDER_PATH!" GOTO F3
        MKDIR "!FOLDER_PATH!"
    :F3
    IF NOT EXIST "!PROJ_DIR!!FILE_PATH!" GOTO F4
        COPY "!PROJ_DIR!!FILE_PATH!" "!FOLDER_PATH!"
    :F4
        ECHO Cannot find the file under "!PROJ_DIR!!FILE_PATH!"

    ENDLOCAL
)

SET CLOSE_CONFIRM=
SET /P CLOSE_CONFIRM=Press any key to close the window...%=%

路径。 txt

//www.domain.com/path/dir/dir1/dir2/file1.txt 
//www.domain.com/path/dir/dir1/dir2/file2.txt 
//www.domain.com/path/dir/dir1/dir2/file3.txt 
//www.domain.com/path/dir/dir1/dir2/file4.txt 
//www.domain.com/path/dir/dir1/dir3/file1.txt

命令输出

        1 file(s) copied.
Cannot find the file under "D:\workspace\proj\dir1\dir2\file1.txt"
        1 file(s) copied.
Cannot find the file under "D:\workspace\proj\dir1\dir2\file2.txt"
Press any key to close the window...

谢谢..

I am referring to below threat Batch files: How to read a file?. For retrieving the line by line from a text file. I am using the below script:

@echo off
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ paths.txt"`) do (
    set "var=%%a"
    SETLOCAL EnableDelayedExpansion
    set "var=!var:*:=!"
    echo !var!
    ENDLOCAL
)

Code is working fine! The values in !var! can not assign to any variable. :( I struct there, Please anyone help to read line by line and I wish to assign to some variable and want to manipulate that variable. Please help to solve this situation.

Update:

@ECHO off

CLS

SET PROJ_DIR=D:\workspace\proj
SET PROMO_DIR=D:\TEST
SET SOURCE_CODE=\Source Code

SETLOCAL DisableDelayedExpansion
for /f %%a in (paths.txt) do (
    SET "var=%%a"
    SETLOCAL EnableDelayedExpansion
    set "var=!var:*:=!"
    set FILE_PATH=!var://www.domain.com/path/dir=!
    SET FILE_PATH=!FILE_PATH:/=\!
    SET PROMO_FILE_PATH=!PROMO_DIR!!SOURCE_CODE!!FILE_PATH! 
    FOR %%i IN ("!PROMO_FILE_PATH!") DO SET FOLDER_PATH=%%~dpi
    FOR %%i IN ("!PROMO_FILE_PATH!") DO SET FILE_NAME=%%~nxi
    IF EXIST "!FOLDER_PATH!" GOTO F3
        MKDIR "!FOLDER_PATH!"
    :F3
    IF NOT EXIST "!PROJ_DIR!!FILE_PATH!" GOTO F4
        COPY "!PROJ_DIR!!FILE_PATH!" "!FOLDER_PATH!"
    :F4
        ECHO Cannot find the file under "!PROJ_DIR!!FILE_PATH!"

    ENDLOCAL
)

SET CLOSE_CONFIRM=
SET /P CLOSE_CONFIRM=Press any key to close the window...%=%

paths.txt

//www.domain.com/path/dir/dir1/dir2/file1.txt 
//www.domain.com/path/dir/dir1/dir2/file2.txt 
//www.domain.com/path/dir/dir1/dir2/file3.txt 
//www.domain.com/path/dir/dir1/dir2/file4.txt 
//www.domain.com/path/dir/dir1/dir3/file1.txt

Command Output

        1 file(s) copied.
Cannot find the file under "D:\workspace\proj\dir1\dir2\file1.txt"
        1 file(s) copied.
Cannot find the file under "D:\workspace\proj\dir1\dir2\file2.txt"
Press any key to close the window...

thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

童话 2024-11-21 10:54:24

关键是延迟扩展,始终使用 ! 而不是使用 % 扩展括号内的变量。

X 更改为 Y 的示例

@echo off
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ paths.txt"`) do (
    set "var=%%a"
    SETLOCAL EnableDelayedExpansion
    set "var=!var:*:=!"
    set "myVar=!var!"
    set "myVar=!myVar:X=Y!"
    echo X replaced with Y =!myVar! 
    ENDLOCAL
)

在更新的版本中,goto :label 立即停止 for 循环

最好将其重写为 IF 块

IF NOT EXIST "!FOLDER_PATH!" (
  MKDIR "!FOLDER_PATH!"
)
IF EXIST "!PROJ_DIR!!FILE_PATH!" (
    COPY "!PROJ_DIR!!FILE_PATH!" "!FOLDER_PATH!"
) ELSE
(
    ECHO Cannot find the file under "!PROJ_DIR!!FILE_PATH!"
)

The key is the delayed expansion, expand your variables inside of parenthesis always with ! not with %.

A sample that changes X with Y

@echo off
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ paths.txt"`) do (
    set "var=%%a"
    SETLOCAL EnableDelayedExpansion
    set "var=!var:*:=!"
    set "myVar=!var!"
    set "myVar=!myVar:X=Y!"
    echo X replaced with Y =!myVar! 
    ENDLOCAL
)

In your updated version the goto :label stops the for-loop immediatly

Better rewrite it to IF-Blocks

IF NOT EXIST "!FOLDER_PATH!" (
  MKDIR "!FOLDER_PATH!"
)
IF EXIST "!PROJ_DIR!!FILE_PATH!" (
    COPY "!PROJ_DIR!!FILE_PATH!" "!FOLDER_PATH!"
) ELSE
(
    ECHO Cannot find the file under "!PROJ_DIR!!FILE_PATH!"
)
恍梦境° 2024-11-21 10:54:24

这里的其他答案涵盖了此代码中延迟扩展的棘手部分。我想补充一点,您通常可以通过将括号放入子例程中来避免大多数延迟扩展问题。

FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ paths.txt"`) do call :HandlePath %%a
goto :eof

================
:HandlePath
set "var=%*"
set "var=%var:*:=%"
echo %var%
goto :eof

我发现这段代码更容易维护,因为每一行都按照您期望的时间进行解析和执行。

The other answers here cover the tricky bits of delayed expansion in this code. I want to add that you can often avoid most delayed expansion problems by rolling the parentheses out into a subroutine.

FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ paths.txt"`) do call :HandlePath %%a
goto :eof

================
:HandlePath
set "var=%*"
set "var=%var:*:=%"
echo %var%
goto :eof

I find this code easier to maintain because each line is parsed an executed exactly when you would expect.

梦一生花开无言 2024-11-21 10:54:24

如果您想读取每一行并操作它:

SETLOCAL EnableDelayedExpansion
FOR /F "tokens=*" %%a IN (paths.txt) DO (
set var=%%a
ECHO %var%
PAUSE
)
ENDLOCAL

如果您尝试从文件中搜索字符串并操作它:

SETLOCAL EnableDelayedExpansion
FOR /F "tokens=* usebackq" %%a IN (`FIND /I 'string to search for' "C:\folder\paths.txt"`) DO (
set var=%%a
ECHO %var%
PAUSE
)
ENDLOCAL

如果您尝试搜索字符串并操作字符串中的每个单词:

SETLOCAL EnableDelayedExpansion
FOR /F "usebackq tokens=1-999 delims= " %%a IN (`FIND /I 'string to search for' "C:\folder\paths.txt"`) DO (
REM %%a = first word  %%b = second word etc. through the alphabet
set var1=%%a%%b%%c
set var2=%%d
ser var3=%%e
ECHO %var1% %var2% %var3%
PAUSE
)
ENDLOCAL

If you want to read each line and manipulate it:

SETLOCAL EnableDelayedExpansion
FOR /F "tokens=*" %%a IN (paths.txt) DO (
set var=%%a
ECHO %var%
PAUSE
)
ENDLOCAL

If you are trying to search a string from a file and manipulate it:

SETLOCAL EnableDelayedExpansion
FOR /F "tokens=* usebackq" %%a IN (`FIND /I 'string to search for' "C:\folder\paths.txt"`) DO (
set var=%%a
ECHO %var%
PAUSE
)
ENDLOCAL

If you are trying to search a string and manipulate each word in the string:

SETLOCAL EnableDelayedExpansion
FOR /F "usebackq tokens=1-999 delims= " %%a IN (`FIND /I 'string to search for' "C:\folder\paths.txt"`) DO (
REM %%a = first word  %%b = second word etc. through the alphabet
set var1=%%a%%b%%c
set var2=%%d
ser var3=%%e
ECHO %var1% %var2% %var3%
PAUSE
)
ENDLOCAL
指尖上得阳光 2024-11-21 10:54:24

这对我有用:

@echo off
SETLOCAL EnableDelayedExpansion

FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ paths.txt"`) do (
set var=%%a
set var=!var:*:=!
echo !var!
)
ENDLOCAL

This works for me:

@echo off
SETLOCAL EnableDelayedExpansion

FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ paths.txt"`) do (
set var=%%a
set var=!var:*:=!
echo !var!
)
ENDLOCAL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文