Tech Support > Computers & Technology > Programming > Batch file and reading it
Batch file and reading it
Posted by JediKnight2 on January 15th, 2005


Is there a way to search for a batch file and append a batch file with
the drive letter the file is located in...for instance...I have an
update to 1.bat sometimes 1.bat may be on C or D drive. I want 2.bat
to find 1.bat and insert the drive parameter into itself. I dont think
I can do that, BUT if that cant be done I should be able to use 2.bat
to read and find 1.bat and build 3.bat from the parameters it found and
the parameters I enter to finish the build...correct?

Posted by mensanator@aol.com on January 15th, 2005



JediKnight2 wrote:
I wasn't sure what you meant by updating 1.bat. Did you want to
completely replace it with a new file? Or did you want to append
a new file to the old one? The following batch file can do either
by switching the REM on the first "for /F" command.

REM
REM locate the file and write it to a file
REM error messages such as "file not found"
REM don't get written to file
REM
REM the /b option returns just the filename
REM the /s option searches subdirectories and
REM will add the path to the file when found
REM
REM it would be bad if you have to search from
REM the root directory or if more than one copy
REM of 1.bat is found
REM
REM for this test, I've got 1.bat hidden in
REM c:\test\target
REM
dir c:\test\1.bat /b/s > foundit.txt
dir d:\test\1.bat /b/s >> foundit.txt
REM
REM create a batch file that appends the update (2.bat) to
REM to 1.bat and saves it as temp.bat
REM
REM if 2.bat is to replace 1.bat switch REM first line
REM and un-REM second
for /F %%I in (foundit.txt) do echo copy %%I+2.bat temp.bat > doit.bat
REM for /F %%I in (foundit.txt) do echo copy 2.bat temp.bat > doit.bat
REM
REM execute the append
REM
call doit.bat
REM
REM create a batch file that replaces 1.bat with
REM the updated 1.bat
REM
for /F %%I in (foundit.txt) do echo copy temp.bat %%I > doit.bat
REM
REM execute the replacement
REM
call doit.bat
REM
REM end of batch file

I named the above find1.bat

Intial contents of 1.bat:

echo this is 1.bat


The update (2.bat):

echo this is 2.bat


Running find1.bat:

C:\test>find1

C:\test>REM
C:\test>REM locate the file and write it to a file
C:\test>REM error messages such as "file not found"
C:\test>REM don't get written
C:\test>REM
C:\test>REM the /b option returns just the filename
C:\test>REM the /s option searches subdirectories and
C:\test>REM will add the path to the file when found
C:\test>REM
C:\test>REM it would be bad if you have to search from
C:\test>REM the root directory or if more than one copy
C:\test>REM of 1.bat is found
C:\test>REM
C:\test>dir c:\test\1.bat /b/s 1>foundit.txt
C:\test>dir d:\test\1.bat /b/s 1>>foundit.txt
The device is not ready.
C:\test>REM
C:\test>REM create a batch file that appends the update (2.bat) to
C:\test>REM to 1.bat and saves it as temp.bat
C:\test>REM
C:\test>REM if 2.bat is to replace 1.bat switch REM first line
C:\test>REM and un-REM second
C:\test>for /F %I in (foundit.txt) do echo copy %I+2.bat temp.bat
1>doit.bat
C:\test>echo copy c:\test\target\1.bat+2.bat temp.bat 1>doit.bat
C:\test>REM for /F %I in (foundit.txt) do echo copy 2.bat temp.bat >
doit.bat
C:\test>REM
C:\test>REM execute the append
C:\test>REM
C:\test>call doit.bat
C:\test>copy c:\test\target\1.bat+2.bat temp.bat
c:\test\target\1.bat
2.bat
1 file(s) copied.
C:\test>REM
C:\test>REM create a batch file that replaces 1.bat with
C:\test>REM the updated 1.bat
C:\test>REM
C:\test>for /F %I in (foundit.txt) do echo copy temp.bat %I 1>doit.bat
C:\test>echo copy temp.bat c:\test\target\1.bat 1>doit.bat
C:\test>REM
C:\test>REM execute the replacement
C:\test>REM
C:\test>call doit.bat
C:\test>copy temp.bat c:\test\target\1.bat
1 file(s) copied.

Contents of 1.bat after update:
echo this is 1.bat
echo this is 2.bat



Posted by antonioadb@click21.com.br on January 17th, 2005



JediKnight2 wrote:


Similar Posts