我有一個批處理文件,從管理門戶網站獲取圖像上傳目錄的內容,並生成一個文本文件,其中包含每個圖像的img src屬性。尋找批處理文件插入新行到文本文件
在這一點上,每個這些文本文件都包含20多個圖像的屬性,然後我通過c#代碼後面的文件將其拉入aspx頁面中的div。
我正在切換到新的佈局,所以我必須在每3行後添加</l> i>>。
我試圖找出如何使用批處理文件來讀取包含文本的IMG SRC的每3行之後插入代碼,但我完全lost.bat
我有一個批處理文件,從管理門戶網站獲取圖像上傳目錄的內容,並生成一個文本文件,其中包含每個圖像的img src屬性。尋找批處理文件插入新行到文本文件
在這一點上,每個這些文本文件都包含20多個圖像的屬性,然後我通過c#代碼後面的文件將其拉入aspx頁面中的div。
我正在切換到新的佈局,所以我必須在每3行後添加</l> i>>。
我試圖找出如何使用批處理文件來讀取包含文本的IMG SRC的每3行之後插入代碼,但我完全lost.bat
@ECHO OFF
SETLOCAL
SET count=0
SET injectevery=3
FOR /f "delims=" %%Z IN ('type njacpm.txt^|findstr /n "^"') DO (
SET /a count+=1
SET line=%%Z
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO(!line:*:=!
IF !count!==%injectevery% ECHO.^</li^>^<li^>
ENDLOCAL
SET /a count=count %% %injectevery%
)
注:調整20130407-1417每dbenham評論
現在 - 如果NJACPM。TXT包含
===============================
line of text 1
line 3 - 2 was empty
: and this behind colon
but this doesn't
A line of ] many <and>varied %poison^characters | like "," and so on
another line
and another
now a real test %path% and !path!
what say you now?
===============================
,輸出是:
===============================
line of text 1
line 3 - 2 was empty
</li><li>
: and this behind colon
but this doesn't
A line of ] many <and>varied %poison^characters | like "," and so on
</li><li>
another line
and another
</li><li>
now a real test %path% and !path!
what say you now?
===============================
但是:
嚴重的計量器會明白......我試着用&
太...工作!
它可以做很容易地用足使用FOR/F遍歷行的純批處理,SET/A遞增計數器,以及延遲擴展或CALL到子例程以及IF語句,以在每行第3行添加額外文本。
但我會用我已經寫過的一個實用程序來執行正則表達式搜索和替換。它是一個名爲REPL.BAT的混合JScript /批處理腳本,適用於任何現代Windows平臺。如果您知道正則表達式,使用起來非常簡單,而且比任何純粹的批處理解決方案快得多。
它有一個M(多行)選項,允許跨換行符進行搜索和替換,以及一個允許替換文本中使用轉義序列的X(擴展替換)選項。
假設REPL.BAT要麼在您的當前文件夾中,要麼在PATH中的某個位置,那麼以下簡單腳本會在原始文件的每一行之後插入</li><li>
作爲新行。
編輯:parametized的「注入每一個」計數,並在替換字符串
@echo off
setlocal
set "file=test.txt"
set "count=3"
<"%file%" call repl "((.*\n){%count%})" "$1</li><li>\r\n" mx >"%file%.new"
move /y "%file%.new" "%file%" >nul
這裏改變\n
到\r\n
是REPL.BAT文件,使上面的腳本工作。它內嵌了完整的文檔。
@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment
::************ Documentation ***********
:::
:::REPL Search Replace [Options [SourceVar]]
:::REPL /?
:::
::: Performs a global search and replace operation on each line of input from
::: stdin and prints the result to stdout.
:::
::: Each parameter may be optionally enclosed by double quotes. The double
::: quotes are not considered part of the argument. The quotes are required
::: if the parameter contains a batch token delimiter like space, tab, comma,
::: semicolon. The quotes should also be used if the argument contains a
::: batch special character like &, |, etc. so that the special character
::: does not need to be escaped with ^.
:::
::: If called with a single argument of /? then prints help documentation
::: to stdout.
:::
::: Search - By default this is a case sensitive JScript (ECMA) regular
::: expression expressed as a string.
:::
::: JScript syntax documentation is available at
::: http://msdn.microsoft.com/en-us/library/ae5bf541(v=vs.80).aspx
:::
::: Replace - By default this is the string to be used as a replacement for
::: each found search expression. Full support is provided for
::: substituion patterns available to the JScript replace method.
::: A $ literal can be escaped as $$. An empty replacement string
::: must be represented as "".
:::
::: Replace substitution pattern syntax is documented at
::: http://msdn.microsoft.com/en-US/library/efy6s3e6(v=vs.80).aspx
:::
::: Options - An optional string of characters used to alter the behavior
::: of REPL. The option characters are case insensitive, and may
::: appear in any order.
:::
::: I - Makes the search case-insensitive.
:::
::: L - The Search is treated as a string literal instead of a
::: regular expression. Also, all $ found in Replace are
::: treated as $ literals.
:::
::: E - Search and Replace represent the name of environment
::: variables that contain the respective values. An undefined
::: variable is treated as an empty string.
:::
::: M - Multi-line mode. The entire contents of stdin is read and
::: processed in one pass instead of line by line.^anchors
::: the beginning of a line and $ anchors the end of a line.
:::
::: X - Enables extended substitution pattern syntax with support
::: for the following escape sequences:
:::
::: \\ - Backslash
::: \b - Backspace
::: \f - Formfeed
::: \n - Newline
::: \r - Carriage Return
::: \t - Horizontal Tab
::: \v - Vertical Tab
::: \xnn - Ascii (Latin 1) character expressed as 2 hex digits
::: \unnnn - Unicode character expressed as 4 hex digits
:::
::: Escape sequences are supported even when the L option is used.
:::
::: S - The source is read from an environment variable instead of
::: from stdin. The name of the source environment variable is
::: specified in the next argument after the option string.
:::
::************ Batch portion ***********
@echo off
if .%2 equ . (
if "%~1" equ "/?" (
findstr "^:::" "%~f0" | cscript //E:JScript //nologo "%~f0" "^:::" ""
exit /b 0
) else (
call :err "Insufficient arguments"
exit /b 1
)
)
echo(%~3|findstr /i "[^SMILEX]" >nul && (
call :err "Invalid option(s)"
exit /b 1
)
cscript //E:JScript //nologo "%~f0" %*
exit /b 0
:err
>&2 echo ERROR: %~1. Use REPL /? to get help.
exit /b
************* JScript portion **********/
var env=WScript.CreateObject("WScript.Shell").Environment("Process");
var args=WScript.Arguments;
var search=args.Item(0);
var replace=args.Item(1);
var options="g";
if (args.length>2) {
options+=args.Item(2).toLowerCase();
}
var multi=(options.indexOf("m")>=0);
var srcVar=(options.indexOf("s")>=0);
if (srcVar) {
options=options.replace(/s/g,"");
}
if (options.indexOf("e")>=0) {
options=options.replace(/e/g,"");
search=env(search);
replace=env(replace);
}
if (options.indexOf("l")>=0) {
options=options.replace(/l/g,"");
search=search.replace(/([.^$*+?()[{\\|])/g,"\\$1");
replace=replace.replace(/\$/g,"$$$$");
}
if (options.indexOf("x")>=0) {
options=options.replace(/x/g,"");
replace=replace.replace(/\\\\/g,"\\B");
replace=replace.replace(/\\b/g,"\b");
replace=replace.replace(/\\f/g,"\f");
replace=replace.replace(/\\n/g,"\n");
replace=replace.replace(/\\r/g,"\r");
replace=replace.replace(/\\t/g,"\t");
replace=replace.replace(/\\v/g,"\v");
replace=replace.replace(/\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}/g,
function($0,$1,$2){
return String.fromCharCode(parseInt("0x"+$0.substring(2)));
}
);
replace=replace.replace(/\\B/g,"\\");
}
var search=new RegExp(search,options);
if (srcVar) {
WScript.Stdout.Write(env(args.Item(3)).replace(search,replace));
} else {
while (!WScript.StdIn.AtEndOfStream) {
if (multi) {
WScript.Stdout.Write(WScript.StdIn.ReadAll().replace(search,replace));
} else {
WScript.Stdout.WriteLine(WScript.StdIn.ReadLine().replace(search,replace));
}
}
}
+1,非常好和有用 – Endoro 2013-04-07 07:10:23
這是可以做到容易地足以與通過管線使用FOR/F以循環純批次,SET/A遞增計數器,並且或者延遲擴展或CALL到子程序使用IF語句沿每隔三行增加額外的文本。
所以在這裏你會得到純淨的感覺一批:-)
@echo off
setlocal
set "file=test.txt"
set /a counter=0
(for /f "delims=" %%i in ('^<%file% findstr /n "^"') do call:processline "%%~i")>%file%.new
goto:eof
:processline
set "line=%~1"
set /a check=counter%%3
if %check% equ 0 if %counter% neq 0 echo(^</li^>^<li^>
set /a counter+=1
setlocal enabledelayedexpansion
echo(!line:*:=!
endlocal
exit /b
編輯:添加findstr
的空行。
+ 1,不需要設置EMPTY和測試是否定義,只需用'echo(!line:*:=!'替換這兩行就可以了,因爲'echo.'可能會失敗,所以我使用'echo('而不是'echo.') 'echo('always works。 – dbenham 2013-04-07 13:33:02
哦,是的!輝煌! – Magoo 2013-04-07 14:19:59
像一個魅力一樣工作,只是重定向輸出的地方,我需要它和我設置。非常感謝。 – sgates3414 2013-04-07 22:17:41