如何改变特定字符串在批项目 [英] How to change specific string in a project with batch

查看:114
本文介绍了如何改变特定字符串在批项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个应用程序。有些道路已经对整个项目进行更改。路径是固定的,这些文件可以编辑(这是在 .cshtml )。

因此​​,我认为我可以使用一个批处理文件来改变所有的 http://localhost.com http://domain.com 为例(我知道的相对和绝对路径,但在这里我不得不做出这样的。

我想在多台计算机上使用这个脚本,所以我不想安装的应用程序,并使用该应用程序使用脚本...只是蝙蝠运行,这就是它......

所以,如果你有code,可以使在文件中的变化,也可能是了不起的!

要完成我的问题,在这里它是文件和目录的路径


MyApp的
MyApp的/浏览次数
MyApp的/查看/索引/ page1.cshtml
MyApp的/查看/索引/ page2.cshtml
MyApp的/查看/另一/ ​​page7.cshtml
...


解决方案

您可以使用GNU的Win32 SED 了解这一点:

为/ R的MyApp /视图一%%的(* .cshtml)做SED -ibakS#HTTP ://本地主机\\ .COM#HTTP://domain.com#g%%〜一

为/ R 循环搜索所有文件夹递归和 SED 改变所有的URL的 *。CSHTML 文件。这也使得备份副本 *。BAK


批是少得多安全的,但如果你want-这里笔者建议在批处理:

@echo OFF&安培; SETLOCAL
SETfpath = MyApp的\\视图
SETnewext =。新SETFNAME =
用于/ R%fpath%%%一中(* .cshtml)DO SETFNAME = %%〜一个&放大器; CALL:过程
GOTO:EOF:处理
(FOR / Fdelims =%% B IN('FINDSTR / N^%FNAME%')DO(
    SET行= %% B
    SETLOCAL ENABLEDELAYEDEXPANSION
    SET!行=行:*:!=
    如果行:HTTP://localhost.com= NEQ!行了! (
        SET行=行:HTTP://localhost.com=http://domain.com
    )
    ECHO(!行!
    ENDLOCAL
))>中FNAME%%% newext%。
GOTO:EOF

它是如何工作:


  • 第一个循环:读startfolder的目录递归,把文件名先后在一个变量,并调用一个子程序过程为每个文件。

  • 第二循环:逐行读取文件中的行与使用 FINDSTR 以preserve空行。替换所有 http://localhost.com http://domain.com 如果出现的话,写的行到一个新的文件。

用于变量:


  • %fpath%路径要处理的文件,默认的 MyApp的\\浏览

  • %newext%扩展新的文件,默认的。新

好处:


  • preserve空行与 FINDSTR

  • 在第二循环toggeling 延迟扩展
  • preserve感叹号

  • preserve百分号文件名通过使用全局变量

问题:


  • Windows命令shell语言(的 WCSL 的,又名批),不能把长于行 8191 字符,没有这么多的HTML内容

  • 好,批量不是赛车和相关的内容量这可能是一个名副其实的通宵作业

<子>的祝你好运!

I'm developing one application. Some path have to be changed on the whole project. The path are fixed and the files can be edited (it is in .cshtml ).

So I think I can use a batch file to change all the http://localhost.com to http://domain.com for example (I know relative and absolute path, but here I HAVE to make that.

I want to use this script in many computers, so I don't want to install an application and use that app with a script... Just run .bat and that's it...

So if you have code that can make that changes in files, it could be marvellous!

To complete my question, here it is the path of files and dir

MyApp
MyApp/Views
MyApp/Views/Index/page1.cshtml
MyApp/Views/Index/page2.cshtml
MyApp/Views/Another/page7.cshtml
...

解决方案

You can use GNU Win32 sed for this:

for /r "MyApp/Views" %%a in (*.cshtml) do sed -ibak "s#http://localhost\.com#http://domain.com#g" "%%~a"

The for /r loop searches all folders recursively and sed changes the URL's in all *.cshtml files. It also makes a backup copy *.bak.


Batch is much lesser safe, but if you want- here is my suggestion in batch:

@echo OFF &SETLOCAL
SET "fpath=MyApp\Views"
SET "newext=.new"

SET "fname="
for /r "%fpath%" %%a in (*.cshtml) DO SET "fname=%%~a"&CALL:process
goto:eof

:process
(FOR /f "delims=" %%b IN ('findstr /n "^" "%fname%"') DO (
    SET "line=%%b"
    SETLOCAL ENABLEDELAYEDEXPANSION
    SET "line=!line:*:=!"
    IF "!line:http://localhost.com=!" neq "!line!" (
        SET "line=!line:http://localhost.com=http://domain.com!"
    )
    ECHO(!line!
    ENDLOCAL
))>"%fname%%newext%"
goto:eof

How it works:

  • the first for loop: read the directory of the startfolder recursively, put the file names successively in a variable and call a sub routine process for each file.
  • the second for loop: read the file line by line with the use of findstr to preserve empty lines. Replace all http://localhost.com to http://domain.com if it appears and write the lines to a new file.

Used variables:

  • %fpath% path to files to process, default MyApp\Views
  • %newext% extension for the new file, default .new

Benefits:

  • preserve empty lines with findstr
  • preserve exclamation marks by toggeling delayed expansion in the second for loop
  • preserve percent signs % in file names by use of a global variable

Issues:

  • Windows Command Shell Language (WCSL, aka 'batch') can not treat lines longer than 8191 characters, not so much for HTML content
  • well, batch is not a racing car and dependent of the amount of your content this can be an veritable overnight job

Good luck!

这篇关于如何改变特定字符串在批项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆