开发者

Windows batch file FOR /F Loop - how to get whole row when tokens and delims are used?

Hello, dear colleagues...

I have following problem. I need to change one value in text file pref.js (belongs to FireFox)

If this file conatins row:

user_pref("network.proxy.type", 1);

I should change it to:

user_pref("network.proxy.type", 0);

The only way that seems to work (in batch file) is to copy this file to a new one "line by line" and if I find the "network.proxy.type" row I do not copy it, but instead I write to the new file the desired row.

I do it like this:

cls
@echo off
set searchedString=user_pref("network.proxy.type"
set replaceSearchedWith=user_pref("network.proxy.type",0^^);

for /F "tokens=1,* delims=," %%j in (FileIn.txt) do (
  if %%j==%searchedString% ( 
    echo %replaceSearchedWith% >> FileOut.txt

  )
  if not %%j==%searchedString% ( 
    echo %%j,%%k >> FileOut.txt
  ) 
)

This works almost well. The only problem is, that the source file looks like this:

# Mozilla User Preferences

/* Do not edit this file.
 *
 * If you make changes to this file while the application is running,
 * the changes will be overwritten when the application exits.
 *
 * To make a manual change to preferences, you can visit the URL about:config
 * For more information, see http://www.mozilla.org/unix/customizing.html#prefs
 */

user_pref("accessibility.typeaheadfind.flashBar", 0);
user_pref("app.update.enabled", false);
...

As you can see, in the beginning of this file there are comments. And my algorithm adds comma (,) to the end of the last comment line. It then looks like this:

# Mozilla User Preferences, 
/* Do not edit this file., 
 *, 
 * If you make changes to this file while the application is running, 
 * the changes will be overwritten when the application exits., 
 *, 
 * To make a manual change to preferences, you can visit the URL about:config 
 * For more information, see h开发者_如何学运维ttp://www.mozilla.org/unix/customizing.html#prefs 
 */, 
user_pref("accessibility.typeaheadfind.flashBar", 0); 
user_pref("app.update.enabled", false); 
...

It is because my program joins tokens like this:

echo %%j,%%k >> FileOut.txt

And now the final question

Is there a way how to get the whole text line in my for loop, so that I would not have to join strings?

Or is there a better way to do my job?

I think I could skip first 12 lines, but I'm not sure that on each computer this comments will take exactly 12 rows. This script should be used on hundreds of computers in our network.

Thank you in advance


You could simply check whether %%k is an empty string, before echoing the output:

...
  if not %%j==%searchedString% (
    if "%%k"=="" (
      echo %%j>> FileOut.txt
    ) else (
      echo %%j,%%k>> FileOut.txt
    )
  ) 
...

By the way, instead of comparing to %searchedString% twice you could only do it once by employing a single if (...) else (...) structure. Here:

@echo off
set searchedString=user_pref("network.proxy.type"
set replaceSearchedWith=user_pref("network.proxy.type",0^^);

for /F "tokens=1,* delims=," %%j in (FileIn.txt) do (
  if %%j==%searchedString% ( 
    echo %replaceSearchedWith% >> FileOut.txt
  ) else ( 
    if "%%k"=="" (
      echo %%j>> FileOut.txt
    ) else (
      echo %%j,%%k>> FileOut.txt
    )
  ) 
)


@grawity seconded. Perl is portable. Why use anything else? Anyway, just to satisfy your masochistic tendencies:

for /F "tokens=*" %%j in (FileIn.txt) do call :testformatch %%j& if errorlevel 1 (echo %%j >>FileOut.txt) else echo need something else >>FileOut.txt
goto :eof

:testformatch
echo %*| find "network.proxy.type" >nul
goto :eof

Ugh, makes my flesh crawl!


First of all, do not use cmd scripts for this. Calculating π with only integer math is one thing, but trying to parse JavaScript code (even computer-generated) is road to madness (not to mention being really slow).


Second, mucking with user's prefs.js is not the Right Way.

You can set up distributed configuration in Mozilla software, as described in Mission Control.

  1. Create a <programdir>\defaults\pref\all.js with contents:

    pref("general.config.obscure_value", 0);
    pref("general.config.filename", "firefox-config.js");
    
  2. Create a <programdir>\firefox-config.js with contents:

    lockPref("autoadmin.global_config_url", "https://configserver/firefox-config.js");
    
  3. On configserver, create a firefox-config.js and make sure it is available on the aforementioned URL.

    pref("network.proxy.type", 0);
    

    If you want to enforce the proxy type, use lockPref() instead. (This can be overriden, though.)

You could skip step 3 and just use network.proxy.type in step 2, but would you rather update hundreds of config files once or every time?

Also, it would be easier to tell Firefox to use system's proxy settings (network.proxy.type=5), the same way IE and Chrome do.


If you want to stick to your methods, consider a different programming language. Perl, Python, JScript or C# are all better than cmd.

JScript and VBScript come with Windows. For C# and VB.NET, these days most Windows boxen have .NET Runtime installed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜