2011-06-08 58 views
3

我指的是下面的威脅Batch files: How to read a file?。用於從文本文件中逐行檢索。我正在使用下面的腳本:MS-DOS批處理腳本:賦值操作

@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...%=% 

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 

命令輸出

 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... 

感謝..

+1

一個簡單的'set「myVar2 =!var!」'應該可以工作,或者你試圖在'endlocal'之後訪問變量嗎? – jeb 2011-06-08 12:36:42

+0

它對我不起作用:(我試過'「myvar2 =!var!」'然後'echo%myvar2%'。但是,它不會打印任何內容,只是打印'ECHO is on'。做什麼?請幫忙。謝謝 – 2011-06-08 14:46:12

+0

賦值工作正常,但回聲失敗,因爲百分比擴展只在執行循環之前完成一次。將其更改爲'echo!myvar2!' – jeb 2011-06-08 17:29:35

回答

2

的關鍵是延遲的擴張,!%擴大括號裏面的變量始終。

改變樣品XY

@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 
) 

在更新後的版本轉到:標籤停止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!" 
) 
+0

嗨jeb,謝謝這對我很有用:)但另一個問題。它從文件的5行中讀取2行。 :(爲什麼是這樣? – 2011-06-09 07:49:40

+0

發佈你的文件內容,否則它可能有點棘手 – jeb 2011-06-09 09:09:09

+0

嗨jeb,現在更新!請看看問題......謝謝,它不是讀取文件中的所有行。 ? – 2011-06-09 09:17:56

1

如果你想讀的每一行,並對其進行操作:

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 
+0

它只是打印'ECHO is off'。我不能分配像'var2 =!var!'。 – 2011-06-08 15:48:27

+0

和一個簡短的問題......你是否試圖在文本文件中搜索特定的字符串,或者你想要讀取所有行?我從未見過!var:*:=!之前使用...什麼:*:=應該做的變量? – Mechaflash 2011-06-08 15:54:26

+0

感謝您的及時響應,以及我試圖從文件逐行閱讀,我只是想操縱該行......這就是我想要實現的...... – 2011-06-08 16:09:02

0

這個工作對我來說:

@echo off 
SETLOCAL EnableDelayedExpansion 

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

我引入了延遲切換技術,因爲在您的代碼中,您將失去所有'!',有時也會'^' – jeb 2011-06-08 17:32:06

2

這裏的其他答案涵蓋了這段代碼中延遲擴展的棘手問題。我想補充一點,通過將括號滾動到子例程中,您通常可以避免大多數延遲擴展問題。

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 

我覺得這段代碼更容易維護,因爲每一行都被解析執行,正如你所期望的那樣。

+0

+1,對於簡單問題的好提示,但這將失敗依賴於行的內容,你不能處理像「一個困難的文字」帶百分比擴展的&「&'也不帶調用參數 – jeb 2011-06-29 21:38:51