Batch file to edit another batch not work correctly

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Padd007
Posts: 16
Joined: 20 Jun 2016 06:36

Batch file to edit another batch not work correctly

#1 Post by Padd007 » 21 Jun 2016 05:05

Hey guys

Forgive me for the second post but I am having another problem I cant resolve.

I have the following in a batch file:

Code: Select all

@echo off
cls
color 0a

echo "Contacting XXXXXXX APN"
echo "Please wait >>>>>"

ping localhost –n 25 >NUL

rasdial XXXXX XXXXX\YYYYY XXXXXX


I have another batch which prompts for a username and uses it for domain settings but I also need to change "YYYYYY" with a new user. I am using the following to do this:

Code: Select all

set /p ID="Enter TB"
wmic path win32_computersystem where Name="%computername%" CALL rename Name="TB%ID%"

set txtfile=%CD%\test.bat
set newfile=%CD%\new_test.bat
if exist "%newfile%" del /f /q "%newfile%"
set "search=TB009786"
set "replace=TB%ID%"
for /f "tokens=*" %%a in (%txtfile%) do (
   set newline=%%a
   call set newline=%%newline:%search%=%replace%%%
   call echo %%newline%% >>%newfile%
)


This creates a new file with the amendment but with one error:

Code: Select all

@echo off
cls
color 0a

echo "Contacting XXXXX APN"
echo "Please wait >>>>>"

ping localhost –n 25

rasdial XXXXX XXXXX\YYYYY XXXXXX


The ">NUL" gets lost during the process and I am not sure why.

Could anyone help??

Regards

Padd
Last edited by Padd007 on 22 Jun 2016 02:12, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch file to edit another batch not work correctly

#2 Post by aGerman » 21 Jun 2016 14:22

To answer your question: Replace the FOR /F loop with that code ...

Code: Select all

setlocal EnableDelayedExpansion
<"!txtfile!" >"!newfile!" (
  for /f %%i in ('type "!txtfile!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
    set "line=" &set /p "line="
    if not defined line (
      echo(
    ) else (
      echo(!line:%search%=%replace%!
    )
  )
)
endlocal


But may I ask you what that odd PING line is good for? Of course I know it's used for a delay of ~25 seconds. So actually my question is why you even included any delay? I guess the only purpose is wasting the users time ...

Regards
aGerman

Padd007
Posts: 16
Joined: 20 Jun 2016 06:36

Re: Batch file to edit another batch not work correctly

#3 Post by Padd007 » 22 Jun 2016 03:42

Thankyou i will give that a go.

TBH I am not sure what the purpose of that is, the script was provided by the client and I presumed it was there for a reason. I might try and remove it if it continues to be a problem. :)

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Batch file to edit another batch not work correctly

#4 Post by thefeduke » 22 Jun 2016 15:08

Padd007 wrote:

Code: Select all

:: . . . 
   call echo %%newline%% >>%newfile%
:: . . .
The ">NUL" gets lost during the process and I am not sure why.
To see what happened, be aware of the of the contents of the newline variable and experiment with something like this on a Command Line:

Code: Select all

Echo anything >here_it_is.not >>here.Not >>Here.itis
type here*.*
It seems that only the last redirection using the same handle takes effect and the previous are overridden, but are not treated as data by ECHO.
John A.

Padd007
Posts: 16
Joined: 20 Jun 2016 06:36

Re: Batch file to edit another batch not work correctly

#5 Post by Padd007 » 23 Jun 2016 03:20

thefeduke wrote:
Padd007 wrote:

Code: Select all

:: . . . 
   call echo %%newline%% >>%newfile%
:: . . .
The ">NUL" gets lost during the process and I am not sure why.
To see what happened, be aware of the of the contents of the newline variable and experiment with something like this on a Command Line:

Code: Select all

Echo anything >here_it_is.not >>here.Not >>Here.itis
type here*.*
It seems that only the last redirection using the same handle takes effect and the previous are overridden, but are not treated as data by ECHO.
John A.


I shall have a play, thank you :)

douglas.swehla
Posts: 75
Joined: 01 Jun 2016 09:25

Re: Batch file to edit another batch not work correctly

#6 Post by douglas.swehla » 25 Jun 2016 01:46

Replace

Code: Select all

echo "Please wait >>>>>"

ping localhost –n 25 >NUL

with

Code: Select all

timeout 25

or, if you want them to wait the entire 25 seconds,

Code: Select all

timeout /t 25 /nobreak


The TIMEOUT command was made to do exactly what the PING hack is for, and is included in Windows 7 onward. It was not native in XP, but is available for download. Not sure about Vista. Documentation, examples, screenshots, and alternatives are available at SS64 and HowToGeek. You can also just run "TIMEOUT /?".

The TIMEOUT command does a countdown ("Waiting for N seconds, press a key to continue..."), where N is the number of seconds remaining, so, while you can redirect its output to NUL, there's really no need, and it's probably better if you don't. That gets rid of your ">" problem.

As aGerman noted, there is no obvious benefit to making the user wait, so this solution only applies if the client insists on keeping the delay.

You could just pass the XXXX and YYYY values as arguments to the first batch file. Is there a reason to use this elaborate "find and replace" approach instead? It's a neat trick, and solves a problem (no native F&R tool), but I think it's not a problem that you actually have.

Padd007
Posts: 16
Joined: 20 Jun 2016 06:36

Re: Batch file to edit another batch not work correctly

#7 Post by Padd007 » 28 Jun 2016 08:09

I have this working now, turns out the delay served no purpose as was suggested so I removed it. I went the route I did because I am not familiar with batch files and I have basically copied and pasted bits from around the internet. Didn't know how to do it any other way :)

Post Reply