0

I want to read a text file where every line has a number. Negative numbers need to be replaced by 0 and written to a new file along with the rest of the positive numbers.

The problem is that I want to save the value of a line, i.e. %%a into a new variable. Then I'll check if the first character of that variable is '-' if so, I'll set the value to 0 for that line in the final file if not, it will remain as is.

But I cannot save the line value into anything. Below is my code. 3.txt is the original file, tempfile.txt is the final file.

set filem=3.txt
set tempfile=tempfile.txt
for /f "tokens=*" %%a in (%filem%) do (
    set linevalue=%%a
    IF %linevalue:~0,1% EQU - (
    echo 0>>%tempfile%
    ) ELSE (
    echo %%a>>%tempfile%
    )
)
pause
AAB
  • 1,594
  • 2
  • 25
  • 40
  • Thank you for including a description of your problem and your attempts to solve it. Please also include a description of the problem - that is why the code that you show does not work. As part of a Minimal Example we expect that you tell us what you expected to happen, and what actually happened. – bendl Feb 12 '20 at 14:56
  • I have closed your question as a duplicate, (as the issue is that you're are trying to both define a variable and use its expanded value within the same parsed code block). _I have additionally included an answer to assist your understanding of the information in the linked question and answers._ – Compo Feb 12 '20 at 15:29

2 Answers2

0

I am assuming that you will run the / in the same directory/folder as your code files, otherwise, edit the cd /d "%~dp0" adding the full/complete folder of your files.


@echo off 

cd /d "%~dp0"
set "filem=.\3.txt"
type nul >".\tempfile.txt"
set "tempfile=.\tempfile.txt"

for /f "tokens=1*" %%a in ('type "%filem%"')do (
 echo["%%~a"|%__APPDIR__%findstr.exe \-[0-9] >nul && (
   echo[0>>"%tempfile%" ) || ( echo/%%~a%%b>>"%tempfile%" )
)

goto :EOF

Io-oI
  • 2,514
  • 3
  • 22
  • 29
0

If your numbers are really integers, you could give the following method a try:

@Echo Off
Set "filem=3.txt"
Set "tempfile=tempfile.txt"
If Exist "%filem%" (
    (   For /F "Tokens=1* Delims=]" %%G In (
            '""%__AppDir__%find.exe" /N /V ""<"%filem%""'
        ) Do If "%%H" == "" (
            Echo=%%H
        ) Else (
            If %%H Lss 0 (
                Echo 0
            ) Else Echo %%H
        )
    )>"%tempfile%"
)
Pause

If they're floating point numbers, you'd be both setting and using variables within the same code block, so you would need to use delayed expansion, (which was missing from your code):

@Echo Off
SetLocal DisableDelayedExpansion
Set "filem=3.txt"
Set "tempfile=tempfile.txt"
If Exist "%filem%" (
    (   For /F "Tokens=1* Delims=]" %%G In (
            '""%__AppDir__%find.exe" /N /V ""<"%filem%""'
        ) Do If "%%H" == "" (
            Echo=%%H
        ) Else (
            Set "LineVal=%%H"
            SetLocal EnableDelayedExpansion
            If "!LineVal:~,1!" == "-" (
                Echo 0
            ) Else Echo %%H
            EndLocal
        )
    )>"%tempfile%"
)
Pause
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thanks a lot, it works, when number is negative, it assigns 0, unfortunately when number is positive, it adds a blank space in front of the number, therefore I cannot use. In short, if you have: -1|-6|8|7, I get 0|0| 8| 7. As long as I don't know how to enter an "intro", I typed | instead... – user12886101 Feb 13 '20 at 07:45
  • You must not have copied it correctly, my code adds nothing to any line, it only replaces lines with a minimum of two characters, _(the first being a `-`, with a single one)_. The most likely reason is that you've added a space character yourself, after the `H` on line `12` with the first example, line `15` with the second example. In fact, there should be no trailing spaces on any line of my script, _(any decent text/programming editor should have a trim whitespace ootion)_. – Compo Feb 13 '20 at 08:13
  • True, you're right, so I have 2 more problems, first, my file has a space at the begining of every line, I don't know how to get rid of it. On the other hand, my numbers are in exponential notation, for example 2.3e-2, I need the numbers in number notation, in the example 0.023, is it possible to do it? I'm sorry but I'm a physicist, not a programmer, that's why I do not know about coding. Thanks again! – user12886101 Feb 13 '20 at 09:35
  • Thanks again. I understand. Thanks for your time anyway. I just give up. – user12886101 Feb 13 '20 at 12:33