Reputation: 5
As I am not an expert in script command and I am getting stuck on this script, I am asking the community for some help.
As for my problem I tried the following (see code below ) in order to change a character in a textfile on a specific location. By using For /F
I was hoping to readout the entire file to a backup copy, and just change the wanted location to the new value by using an IF
. However I noticed that when I put the token parameter to tokens=6
it wouldn't copy the entire file to the new location. so I changed this to tokens=*
. However with this the entire row on the 74th line is changed. :s and with this I also found out that ENTERS and specific characters as "!" are not copied to the backup file.
Now i was hoping that you could help me change this character on location line 74 , token 6 without further resulting in any change of the specific text file.
thanks for any advise.
@echo off
CLS
setlocal enabledelayedexpansion
set ORIGINALFILE=test.file.txt
set MODIFIEDFILE=temp1.txt
set MODIFIEDFILE2=temp_backup.txt
set SEARCHVALUE=0
set REPLACEVALUE=1
set OUTPUTLINE=
set linenumber=0
::for demo purpose make cop of original file as original will be overwritten
copy %ORIGINALFILE% %MODIFIEDFILE2%
del temp1.txt
echo "all set"
timeout /t 2 /nobreak >nul
::everything is set now change the 6th item on the line 74 to a "1"
FOR /f "tokens=* delims=," %%G in ('"type %ORIGINALFILE%"') do (
set /a linenumber=!linenumber!+1
SET string=%%G
if !linenumber! EQU 61 (
SET modified=!string:%SEARCHVALUE%=%REPLACEVALUE%!
echo !linenumber!
) else (
echo !linenumber!
SET modified=!string!
)
echo !modified! >> %MODIFIEDFILE%
)
Upvotes: 0
Views: 43
Reputation: 80113
@ECHO OFF
SETLOCAL
set ORIGINALFILE=q34614930.txt
set MODIFIEDFILE=u:\temp1.txt
set MODIFIEDFILE2=u:\temp_backup.txt
set SEARCHVALUE=0
set REPLACEVALUE=1
::for demo purpose make cop of original file as original will be overwritten
copy %ORIGINALFILE% %MODIFIEDFILE2% >NUL 2>nul
REM echo "all set"
REM timeout /t 2 /nobreak >nul
::everything is set now change the 6th item on the line 5 to a "1"
(
FOR /f "tokens=1*delims=:" %%a in ('findstr /n /r ".*" "%ORIGINALFILE%"') do (
IF "%%a"=="5" (
FOR /f "tokens=1-6*delims=," %%A IN ("%%b") DO echo(%%A,%%B,%%C,%%D,%%E,%replacevalue%,%%G
) ELSE ECHO(%%b
)
)>"%MODIFIEDFILE%"
GOTO :EOF
I used a file named q34614930.txt
containing some test data for my testing. I also changed the filenames to suit mys system.
In the absence of representative data, I use the following and replaced column 6 in line 5.
line 1,col2,col3,col4,col5,col6,col7,col8
line 2,col2,col3,col4,col5,col6,col7,col8
line 4,col2,col3,col4,col5,col6,col7,col8
line 5,col2,col3,col4,col5,col6,col7,col8
line 6,col2,col3,col4,col5,col6,col7,col8
Result
line 1,col2,col3,col4,col5,col6,col7,col8
line 2,col2,col3,col4,col5,col6,col7,col8
line 4,col2,col3,col4,col5,col6,col7,col8
line 5,col2,col3,col4,col5,1,col7,col8
line 6,col2,col3,col4,col5,col6,col7,col8
Without representative data, we're guessing.
Upvotes: 1