Page 1 of 1

"REM"ing a command in another file

Posted: 19 Oct 2011 19:22
by Rileyh
Hi all,
As a result of "Dave_b"'s post on making additions to the global "path" variable, I am making a "substitute" command.
Quote of Dave_b's post:
There are variables that are pre-defined before your command session starts. PATH is one of them. You can change the values of these pre-defined variables as long as you have admin privileges.

There are multiple methods to get to the window that allows modification of the values, and the details can vary depending on your version of Windows. On Vista you can get to it from the Control Panel: Select System, then select Advanced system settings, then select Environment Variables. From this point on I think the various Windows versions are the same.

There are two sections: User variables only apply to you, and System variables apply to all users of the machine. If you modify the PATH in System variables then anybody that logs in to that machine will have access to your "command". Alternatively I think you can create a PATH variable in your User section, copying the existing value from System section and then modify it as you want. I believe the PATH definition in your User section will over-ride the System value, but I'm not 100% sure.


I want to place a REM statement before a user-defined piece of code (they state the file path as well).
Is there a way to place code in another text document/batch file remotely?

Any help would be appreciated,
Rileyh

Re: "REM"ing a command in another file

Posted: 23 Oct 2011 01:08
by Ed Dyreen
'
You can replace lines in a file with for /f.
You can replace lines in a file with the set /p technique

New technic: set /p can read multiple lines from a file :arrow:
viewtopic.php?f=3&t=2128

You can connect remotely using net use /?

Re: "REM"ing a command in another file

Posted: 23 Oct 2011 04:00
by aGerman
Rileyh, I read this post before but to be honest I have no clue what you are looking for.
You can redirect a REM line to another file like each other line. But if you want to insert a line in the middle of an existing code then you will have to write the file completely new and you will have to know the line number before. So far no problem. Read the file line by line and write it to a temporary new file. If you reached the right number of lines then you have to redirect your REM line. Redirect the rest. Move the temporary file and overwrite the origin file.
You've lost me when you wrote "(they state the file path as well)". What does it mean?

Regards
aGerman

Re: "REM"ing a command in another file

Posted: 24 Oct 2011 01:50
by Rileyh
@aGerman,
Sorry for the poor explanation.
Here is what I want to do: (an attempt at a good explanation :) )
1. The user previously types "substart" which allows for the "substitute" command to be run.
2. The user opens cmd and types: substitute + "(string they want to find)" + "(string they want to replace the found string with)" "(file path of the file that contains the string to substitute)". In short, substitute "string1" "string2" "filepath"
3. The command needs to change directory to the given file path, then locate the string in the text/batch file and "comment out"/REM the line.
4. The command then types the "string2"/user defined string into the selected text/batch file on the line after the "string1".
5. The line remains REMed until the user types "subend", which will return the line to it's previous state.

Hopefully there is a solution to my problem.


Regards,
Rileyh

Re: "REM"ing a command in another file

Posted: 28 Oct 2011 11:00
by aGerman
I don't understand the reason for that. I guess nobody changes a source code via batch, much less if you want to undo that later :?
Call your batch file with an option as parameter and check in the called batch what parameter came in.

bat1.bat

Code: Select all

@echo off
call "bat2.bat" /1 "string A"
call "bat2.bat" /2 "string B"
pause


bat2.bat

Code: Select all

@echo off
if "%~1"=="/1" (call :sub1 %2&goto :eof)
if "%~1"=="/2" (call :sub2 %2&goto :eof)
echo neither /1 nor /2 was passed
goto :eof

:sub1
echo you passed /1
echo the 2nd parameter was %1
goto :eof

:sub2
echo you passed /2
echo the 2nd parameter was %1
goto :eof

Regards
aGerman