1.

Solve : Batch File copying text from one file to another cuts off at first space?

Answer»

Hi. I have a batch file to remove the last line of my .TXT file. In this case it copies all rows (except the last one) from d177920101id.txt
and into docinfo.txt

It copies the lines great, but each line is cut off at the first space.

So in d177920101id.txt I have the lines
BIG|CONSTRUCTION|INC
HANSEN|TOWING AND REPAIR|INC
EOF 2

In docinfo.txt I end up with
BIG|CONSTRUCTION|INC
HANSEN|TOWING

Any idea how I can change my batch file to copy the entire lines including the spaces?

Code: [SELECT]SETLOCAL ENABLEDELAYEDEXPANSION

rem Count the lines in file
set /a count=-1
for /F %%a in (d177920101id.txt) DO set /a count=!count!+1

rem Create empty temp file
copy /y NUL tempfile.txt >NUL

rem Copy all but last line
for /F %%a in (d177920101id.txt) DO (
IF /I !count! GTR 0 (
echo %%a >> tempfile.txt
set /a count=!count!-1
)
)

rem overwrite original file, DELETE temp file
copy /y tempfile.txt docinfo.txt >NUL
del tempfile.txt
Change: 'for /F %%a in ...' to 'for /F "delims=" %%a in ...'
Quote from: bmacbmac on July 19, 2012, 02:29:39 PM

Hi. I have a batch file to remove the last line of my .txt file. In this case it copies all rows (except the last one) from d177920101id.txt
and into docinfo.txt

It copies the lines great, but each line is cut off at the first space.

So in d177920101id.txt I have the lines
BIG|CONSTRUCTION|INC
HANSEN|TOWING AND REPAIR|INC
EOF 2

In docinfo.txt I end up with
BIG|CONSTRUCTION|INC
HANSEN|TOWING

Any idea how I can change my batch file to copy the entire lines including the spaces?

Code: [Select]SETLOCAL ENABLEDELAYEDEXPANSION

rem Count the lines in file
set /a count=-1
for /F %%a in (d177920101id.txt) DO set /a count=!count!+1

rem Create empty temp file
copy /y NUL tempfile.txt >NUL

rem Copy all but last line
for /F %%a in (d177920101id.txt) DO (
IF /I !count! GTR 0 (
echo %%a >> tempfile.txt
set /a count=!count!-1
)
)

rem overwrite original file, delete temp file
copy /y tempfile.txt docinfo.txt >NUL
del tempfile.txt
If you read the help file for the FOR command you would have read this.
Code: [Select]delims=xxx - specifies a delimiter set. This replaces the
default delimiter set of space and tab.Thank you, that was extremely helpful. Adding "delims=" worked perfectly!


Discussion

No Comment Found