3

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
...
clement
  • 4,204
  • 10
  • 65
  • 133
  • Excuse me. A _bounty_ is enabled by the OP in order to request more attention to his question by the community. However, the OP must also pay attention to give the appropriate feedback in a timely way in order to complete the bounty in the right way... – Aacini Jul 06 '13 at 14:50
  • Dear Moderator: I posted an answer to this question after the bounty was started. At first place, my answer was reported as "doesn't work" by the OP. However, he had not provided yet the feedback that allows me to identify and correct the (possible) error. In my humble opinion, my answer addresses all question concerns as requested. Of course, I am interested in receive the reputation award, but if my answer not receive any bounty points, then I'll really appreciate it that I could know the reasons of such result. Thanks... – Aacini Jul 07 '13 at 05:14

2 Answers2

5

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!

Endoro
  • 37,015
  • 8
  • 50
  • 63
  • No, I have to run that command on a lot of computer so install sed everywhere isn't a good idea but I appreciate the answer :-) – clement Jun 30 '13 at 15:09
  • @clement I've added a few things that hopefully match your requirements. – Endoro Jul 01 '13 at 17:02
  • +1 for handling blank lines. I think my solution will replace blank lines with the ever-helpful "Echo is ..." – mojo Jul 02 '13 at 13:29
  • it didn't do the work, it creates .new file, set the text ar the wrong place and didn't delete the oldest :-/ – clement Jul 03 '13 at 14:10
  • I'm removing my answer as improving it only makes it more like @Endoro's. – mojo Jul 03 '13 at 17:52
1

The hybrid Batch-JScript file below can solve your problem in a way much faster than any pure Batch solution. Don't worry! This is a Batch .BAT solution, so you don't need to install anything in order to run it in any computer with Windows XP or posterior version.

@if (@CodeSection == @Batch) @then

@echo off
for /R "\MyApp\Views" %%a in (*.cshtml) do (
   < "%%a" CScript //nologo //E:JScript "%~F0" > "%%a.repl"
)
goto :EOF

@end

// JScript section

WScript.Stdout.Write(WScript.StdIn.ReadAll().replace(/http:\/\/localhost\.com/g,"http://domain.com"));

Previous program generate output files with same name of input ones plus an additional .repl extension. Of course, the original files can be deleted so the new ones replaces them if you wish. Also, any modification to the form of use of this file can be achieved; for example, to give the original and new strings for replacement in the Batch file parameters.

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • @clement: What exactly happens? What appear if you enter `cscript` in a command-line? What is your Windows version? – Aacini Jul 03 '13 at 18:41
  • @clement: See this link for further details: http://technet.microsoft.com/en-us/library/bb490816.aspx – Aacini Jul 06 '13 at 14:42
  • it doesn't work == it is not working at all. Nothing appears, Nothing is done in the filesystem. – clement Jul 08 '13 at 08:56