-2

It's my first time to creating the batch script file. My question can be duplicate but I didn't find any question or solution which fulfils my requirement.

I need to create a batch script file on windows for changing the URL in min.js multiple files and one index.html file from the config.json file values.

For example, I have a JSON file:

{
   "APIUrl": "http:\\service.test.com",   
   "BaseUrl": "http:\\www.test.com"
}

Now, I need to change "http:\service2.test.com" with APIUrl and "http:\www.test2.com" with BaseURL.

I found many solutions to find and replace the script for the batch file. But when I run the batch file it's removing the whole script from the min.js without replacing the URLs string.

Maybe the script logic was checking the end line and the min.js file script always written in a single line so replacing the text removing the whole script, it's just my thinking because of the end line script was written in the script.

I google and found below code from the StackOverflow and pasted it to batchscript.bat, but on run the batchscript.bat it is removing the whole script and just left the last line comment "//# sourceMappingURL=app.min.map" in the file. That's why I'm not posting this code.

@echo off 
setlocal EnableExtensions DisableDelayedExpansion

set "search=<http://server/application/>0"
set "replace=<http://someurl.test.com>"

set "textFile=*.*.js"
set "rootDir=./js"

for /R "%rootDir%" %%j in ("%textFile%") do (
    for /f "delims=" %%i in ('type "%%~j" ^& break ^> "%%~j"') do (
        set "line=%%i"
        setlocal EnableDelayedExpansion
        set "line=!line:%search%=%replace%!"
        >>"%%~j" echo(!line!
        endlocal
    )
)

endlocal
pause

For executing this you can take any ".min.js" file and find and replace the text from that file.

  • 1
    If you have a batch file which is exhibiting an issue, and you want us to help you fix it, you're going to have to let us see it. [Edit the question](https://stackoverflow.com/posts/55158877/edit), to include it, remembering to format it as code, so that we can replicate it and your issue. Can you please also clarify whether you need this for a Windows system, because generally, the [tag:shell] tag is for 'nix based systems. – Compo Mar 14 '19 at 10:09
  • @Compo I updated the question you can check it. I'm looking script for the windows I want to replace URLs in my web project for the deployment purpose. – Adnan Ahmed Ansari Mar 14 '19 at 10:53
  • @Compo I know that site isn't a free code writing service but I'm looking for the help. Please don't devote it before understanding the question. – Adnan Ahmed Ansari Mar 18 '19 at 11:40
  • A long list of programs than can solve this here: https://stackoverflow.com/a/49011455/2440 – Sire Oct 08 '21 at 11:27

1 Answers1

-1

If the file name is test.json then we can do the below one.

cat test.json | jq '.APIUrl="test123"'
Gerhard
  • 22,678
  • 7
  • 27
  • 43
error404
  • 2,684
  • 2
  • 13
  • 21
  • what is the purpose of this command?? Is it getting the value from the JSON file? – Adnan Ahmed Ansari Mar 14 '19 at 10:55
  • Before downvoting my answer check for the comments above. The tag was for bash so I assume bash is needed. It a mistake of the user @MallikKumar – error404 Mar 14 '19 at 14:50
  • yash, technically the tag was [tag:shell] not [tag:bash]. Shell scripting is scripting in any shell, whereas Bash scripting is scripting specifically for Bourne Again SHell. The person who downvoted you, wasn't necessarily Mallik, _as that information is not available_, but whomever did so, probably incorrectly assumed that the OP was not relative to a non `cmd.exe` shell in Windows. – Compo Mar 14 '19 at 15:03
  • @Compo yes I am aware that not necessarily the person who has downvoted is mallik but my point of the comment was that it was not batch so I used bash which is one variant of shell :) – error404 Mar 14 '19 at 15:06