Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch File to read a file and substring a portion of the record

Status
Not open for further replies.

ChrisHallJJ

Programmer
Jul 25, 2003
27
US
I want to write a batch file to read a file and substring a portion of the data to write to another file. The file looks like this 000004045
000005025
I can assign a value to a variable and substring it:
SET _string=1234567890
SET _substring=%_string:~5,3%
echo %_substring%
Result is: 678

I can read from a file:
FOR /F %%i IN (c:\a\test.txt) DO @echo %%i
Result is: 000004045
000005025

I can not read from a file and subtring the line:
FOR /F %%i IN (c:\a\test.txt) DO (SET _substring=%%i:~5,4%,@echo %_substring%)
Result is: (SET _substring=000004045:~5,4_substring )
(SET _substring=000005025:~5,4_substring )

How would I do this? Thanks so much, Chris
 
Try this:

FOR /F %%i IN (c:\a\test.txt) DO SET string=%%i

SET substring=%STRING:~-4,4%

 
test.cmd
Code:
FOR /F %%i IN (c:\a\test.txt) DO @echo %%i
FOR /F %%i IN (c:\a\test.txt) DO call test2 %%i

test2.cmd
Code:
echo %1
set junk=%1
SET _substring=%junk:~5,4%
@echo %_substring%
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top