2013-03-21 47 views
0

我試圖找到並替換。我正在使用for循環。我正在學習,但我幾乎在那裏。我試圖找到答案,但消息來源對我來說有點過於撲朔迷離。幾乎在那裏...需要查找和替換。使用for循環

delim是一個空白區域,您可以告訴我跳過4行並執行第2個標記。

我需要在那個地方找到什麼,以var5a取代。我有它倒退,因爲我需要%% F等於var5a,而不是相反(正如我現在寫的)。但不知道該怎麼寫。請解釋一下如何做到這一點。我試過使用< < =但沒有運氣。

for /f "skip=4 tokens=2 delims= " %%F in (script.vbs) do (
set var5a=!var5a!%%F 
) 

我在學習,所以請親切。

+0

你會* *得多更好寫* *任何像這樣在VBScript。或Powershell。或Perl。 *任何*但.bat文件。恕我直言... – paulsm4 2013-03-21 05:39:49

+0

我更熟悉批次atm。謝謝。 – 2013-03-21 05:40:35

+0

paulsm如何與VBScript批量交換變量?我批量使用了所有變量,但是如何將它們轉換爲vbscript? – 2013-03-21 05:44:52

回答

0
@ECHO OFF 
SETLOCAL 
:: Replace token 2 (space-separated) in line 5 of a file with REPLACEMENT 
:: Assumed the file exists, etc. and no line begins ":" 
SET replacement=THIS IS THE REPLACEMENT TEXT 
DEL newfile.txt 2>nul 
FOR /f "tokens=1*delims=:" %%i IN ('findstr /n /r "$" ^<oldfile.txt') DO (
IF %%i==5 (
FOR /f "tokens=1,2* delims= " %%L IN ("%%j") DO >>newfile.txt ECHO %%L %replacement% %%N 
) ELSE (>>newfile.txt ECHO.%%j) 
) 

TYPE oldfile.txt 
ECHO ==== separator ======= 
FC oldfile.txt newfile.txt 

結果:

Line one should not be changed 
Line two should not be changed 
Line three should not be changed 
Line four should not be changed 
changeme iwillbereplaced but only on this line 
and notbereplaced on subsequent lines 

including the previous line which was empty 
==== separator ======= 
Comparing files oldfile.txt and NEWFILE.TXT 
***** oldfile.txt 
Line four should not be changed 
changeme iwillbereplaced but only on this line 
and notbereplaced on subsequent lines 
***** NEWFILE.TXT 
Line four should not be changed 
changeme THIS IS THE REPLACEMENT TEXT but only on this line 
and notbereplaced on subsequent lines 
***** 

有困難 - 特別是如果你的文件中的行以冒號或包含引號的字符串或任何常規的一批陷阱像%


所以 - 最後我已經弄清楚你在做什麼。您省略了關鍵信息,即%%F中的字符串不僅被引用,還包含空格。

你有沒有說過,首先,你會在你的項目中做更多的工作,而且我會更加富有一些阿司匹林。

爲了用加引號的字符串加載%% F,我從文件中讀取了字符串。

@ECHO OFF 
SETLOCAL enabledelayedexpansion 
:: Replace token 2 (space-separated) in line 5 of a file with REPLACEMENT 
:: Assumed the file exists, etc. and no line begins ":" 
SET replacement="THIS IS THE REPLACEMENT TEXT" 
DEL newfile.txt 2>NUL 

:: iwbr.txt just contains 
:: "i will be replaced" 
:: on a single line for loading into %%F as that is the target to be replaced 


FOR /f "delims=" %%F IN (iwbr.txt) DO (
FOR /f "tokens=1*delims=:" %%i IN (
    'findstr /n /r "$" ^<oldfile.txt' 
) DO >>newfile.txt (
    IF %%i==5 (
    SET newline=%%j 
    CALL SET newline=%%newline:%%F=%replacement%%% 
    ECHO.!newline! 
) ELSE (ECHO.%%j) 
) 
) 
TYPE oldfile.txt 
ECHO ==== separator ======= 
FC oldfile.txt newfile.txt 

結果:

Line one should not be changed 
Line two should not be changed 
Line three "should" not be changed 
Line four should not be changed 
changeme "i will bereplaced" but only on this line 
and notbereplaced on subsequent lines 

including the "previous" line which was empty 
including this "unbalanced-quote line... 
==== separator ======= 
Comparing files oldfile.txt and NEWFILE.TXT 
***** oldfile.txt 
Line four should not be changed 
changeme "i will bereplaced" but only on this line 
and notbereplaced on subsequent lines 
***** NEWFILE.TXT 
Line four should not be changed 
changeme "THIS IS THE REPLACEMENT TEXT" but only on this line 
and notbereplaced on subsequent lines 
***** 
+0

我無法得到這個工作 – 2013-03-21 08:27:12

+0

我用script.vbs替換了oldfile.txt,然後做了'set replacement =!var5a!' – 2013-03-21 08:28:15

+0

變量%% F確實包含帶引號的字符串。怎麼修? – 2013-03-21 08:36:41

0

如果您在%% F具有Henri,你想的Henri值設置爲var5a當前值,那麼

set %%F=!var5a! 

!var5a!,如果你想的運行時值var5a(前提是你已經ENABLEDELAYEDEXPANSION模式調用)或%var5a%如果你想分析時值)


程序來演示日在回答不工作:

@ECHO OFF 
SETLOCAL ENABLEDELAYEDEXPANSION 

::: WARNING! This will overwrite SCRIPT.VBS 

DEL script.vbs 2>nul 
FOR /l %%i IN (1,1,4) DO >>script.vbs ECHO ignore this line %%i 
>>script.vbs ECHO Oh Henri - it does work 
>>script.vbs ECHO Now try again 

ECHO ===== here is script.vbs 

TYPE script.vbs 

ECHO ===== script.vbs ends 

SET var5a=this is the value 

SET henri 
SET var5a 

ECHO ===== now run the FOR loop... 

for /f "skip=4 tokens=2 delims= " %%F in (script.vbs) do (
set %%F=!var5a! 
) 

SET henri 

GOTO :eof 

結果:

===== here is script.vbs 
ignore this line 1 
ignore this line 2 
ignore this line 3 
ignore this line 4 
Oh Henri - it does work 
Now try again 
===== script.vbs ends 
Environment variable henri not defined 
var5a=this is the value 
===== now run the FOR loop... 
Henri=this is the value 

現在 - 你是什麼意思"it doesn't work"?它當然會。如果它沒有做你想做的事情,那就證明你爲什麼要提出要求,否則我們就會猜測。

例如,上面也會將try設置爲該值,因爲Henri不是唯一的標記,它是數字2,在源文件的4行之後用空格分隔。這是很容易解決(但我沒有摸透別人的心思 - 只是一個不起眼的程序員)

(set notsetyet=Y) 
for /f "skip=4 tokens=2 delims= " %%F in (script.vbs) do (
IF DEFINED NOTSETYET set %%F=!var5a!&(set notsetyet=) 
) 

但也許"it didn't work"因爲它Henri被設置爲錯誤的值。或者它可能會崩潰。我們無法從您的回覆中得知。

+0

這不起作用 – 2013-03-21 06:18:58

+0

它不起作用,因爲%% F的值始終不同。這可能是亨利,大衛甚至耶穌。我怎麼做? – 2013-03-21 07:12:21

+0

那麼,我們如何知道你想設置什麼變量?你還沒有給出一個清楚的例子,你的意思是,只是你有''it'''倒退,然後你說''我需要%% F來等於var5a''這樣的例證。假設%% F的值是'xyz','var5a'的值是'abc' - 你想要發生什麼? – Magoo 2013-03-21 07:24:01