0

I am trying to find and replace values of a string within a batch file but having issues. I have the user run the batch file and it asks the user 1)what drive the file is on 2)what is the name of folder in the TEST parent folder 3)what is the name of the new server. I want the batch file to look within a file called importer.config and replace a value called server_name with whatever the input from the user is. Here is what I have:

@echo off
SET drive=
SET /P drive=Please enter the drive:

SET folder=
SET /P folder=Enter name of folder desired:

SET server=
SET /P server=Enter name of new server:

@echo off > newfile.txt
setLocal EnableDelayedExpansion

if exist newfile.txt del newfile.txt

for /f "tokens=* delims= " %%a in (%drive%\test\%folder%\importer.config) do (
set str=%%a
set str=!str:server_name=%server%!
echo !str! >> newfile.txt
)
del importer.config
rename newfile.txt importer.config

pause

Every time I run this, the cmd prompt shows: The system cannot find the file specified c:\test\users_input_they_entered\importer.config. The issue is that file is there so trying to understand what I am missing and why it cant find the file that does exist.

It then also states "Could not find c:\windows\system32\importer.config" which not sure why that happens as well

I have searched on stackoverflow, but cannot figure this out with any assistance.

Robby Johnston
  • 183
  • 3
  • 7
  • 15
  • any help is appreciated so much! – Robby Johnston Oct 24 '12 at 15:13
  • Try to use `usebackq` and quote your filename, else you get trouble with spaces. `for /F "usebackq delims=" %%a in ("%drive%\test\%folder%\importer.config") do ...` – jeb Oct 24 '12 at 15:25
  • nope....didnt work...it did the same thing..thanks though :( – Robby Johnston Oct 24 '12 at 15:28
  • it actually didnt error...I forgot i didnt have the colon in the path....but it still says Could Not Find c:\windows\system32\importer.config ....and it doesnt actually replace server_name with server entered – Robby Johnston Oct 24 '12 at 15:31
  • 2
    As your file in the for-loop can't be cause the error `Could Not Find c:\windows\system32\importer.config`, it could be the line `del importer.config`. It could help if you use `echo on` – jeb Oct 24 '12 at 15:44
  • i added this: cd "%drive%:\test\%folder%" right before: @echo on > newfile.txt setLocal EnableDelayedExpansion and it worked great!! thanks for the help jeb – Robby Johnston Oct 24 '12 at 16:03
  • jeb, one last thing....in that file, the command keeps replacing ! characters....any within the command of replacing to tell it to avoid !s? – Robby Johnston Oct 24 '12 at 20:30
  • I don't think you can. On the line `set str=%%a` they are been evaluated by the DelayedExpansion – William Bettridge-Radford Nov 10 '12 at 18:07

1 Answers1

1

You're pushing your luck using the for loop for that.

A tool like sed would work well.

If you look at this post they have a vbscript implementation that you could use Is there any sed like utility for cmd.exe

set input_file=importer.config
set output_file=temp.config
set new_server_name=server1984

cscript /Nologo sed.vbs s/server_name/%new_server_name%/ < %input_file% > %output_file%
Community
  • 1
  • 1