Computer Hope

Microsoft => Microsoft DOS => Topic started by: Blisk on April 06, 2019, 04:24:11 PM

Title: deleting space from result
Post by: Blisk on April 06, 2019, 04:24:11 PM
I have tested a little script to take a number from txt file and use it in batch, but problem is I get that litle square and script doesnt work.
How to get just a number so script will work. In file d:\number.txt is only a number for timeout delay

Code: [Select]
set /p out=<d:\number.txt
TIMEOUT /T %out%

and this is what I get
Title: Re: deleting space from result
Post by: Geek-9pm on April 06, 2019, 06:24:34 PM
Simple answer: Just do not take a number from a file.
Put the number in the batch file itself.
Why is that hard to do?

Title: Re: deleting space from result
Post by: Blisk on April 07, 2019, 12:11:05 AM
Simple answer: Just do not take a number from a file.
Put the number in the batch file itself.
Why is that hard to do?

I know that.
Thing is I want to learn how to do it
Title: Re: deleting space from result
Post by: Salmon Trout on April 07, 2019, 02:33:45 AM
Blisk, how is the file number.txt made? What is that leading character before the '4'? It is not just a simple space; the timeout command doesn't mind spaces before the number.

Why is the number being got from a file?



Title: Re: deleting space from result
Post by: Salmon Trout on April 07, 2019, 04:41:04 AM
You can get exactly this problem if number.txt has Unicode encoding. If you are creating the text in Notepad or some other editor, save with ANSI encoding.

If you doing it that way (with an editor), why?

(https://images2.imgbox.com/dc/66/tR5WeIoR_o.jpg)

(https://images2.imgbox.com/99/1d/rSeRI0Xl_o.jpg)
Title: Re: deleting space from result
Post by: Salmon Trout on April 07, 2019, 05:15:07 AM
Alternative:

Do 1 and 2 and maybe also 3

1. Save text with UTF-8 encoding.

2. Add this line at the top of your batch file underneath @echo off

chcp 65001 >nul

3. As well as these things, also try a Unicode font for console such as Lucida Console.



Title: Re: deleting space from result
Post by: Blisk on April 07, 2019, 08:55:16 AM
I have created file with batch
copy /y nul d:\number.txt
echo 5 >> d:\number.txt

This also didn't help
@echo off
chcp 65001 >nul
also if I save number.txt with UTF8 or ANSI, I get the same result.

What is than right way to do that?

So this is whole script now. and doesn't work
@echo off
chcp 65001 >nul
echo 5 > d:\number.txt
set /p out=<d:\number.txt
TIMEOUT /T %out%
Title: Re: deleting space from result
Post by: Salmon Trout on April 07, 2019, 09:17:04 AM
Try chcp 850 instead.

Title: Re: deleting space from result
Post by: Salmon Trout on April 07, 2019, 09:31:40 AM
Did you change font to Lucida Console?
Title: Re: deleting space from result
Post by: Blisk on April 07, 2019, 09:42:06 AM
When I edit with notepad and add chcp 850 in batch it works
but when I generate number.txt with powershell it doesn't work
Is there more celan way to do this like ignore first charter?
Title: Re: deleting space from result
Post by: Sidewinder on April 07, 2019, 09:49:59 AM
Not to *censored* in but: I suspect it sees the 5 as a stream number as in 2>nul. Try escaping the 5, echo ^5 > d:\number.txt

Just a thought. 8)
Title: Re: deleting space from result
Post by: Salmon Trout on April 07, 2019, 11:28:04 AM
To avoid the stream number thing, you need a space after the number, before the > symbol.

See here, the first try leaves number.txt blank

C:\>echo 5> number.txt
ECHO is on.

C:\>type number.txt

C:\>dir number.txt

07/04/2019  18:20                 0 number.txt

C:\>echo 5 > number.txt

C:\>type number.txt
5


Quote from: Blisk
when I generate number.txt with powershell it doesn't work

Why Powershell?

Quote from: Blisk
copy /y nul d:\number.txt
echo 5 >> d:\number.txt

Why are you doing this?????

This works every time for me....

C:\>echo 5 > d:\number.txt

C:\>set /p out=<d:\number.txt

C:\>timeout /T %out%

Waiting for 0 seconds, press a key to continue ...

C:\>chcp
Active code page: 850


Timeout waits correctly for 5 seconds, no CHCP change needed.





Title: Re: deleting space from result
Post by: Blisk on April 07, 2019, 11:48:35 AM
I am trying to learn some stuff which doesn't work.
Can you please try with my file, which doesn't work with me.

When I put in quotes I see there are tvo charters before number!
Somehov I should skip that
Title: Re: deleting space from result
Post by: Salmon Trout on April 07, 2019, 12:46:20 PM
Your file says 148, and it is encoded as Unicode. How did you make it?

Anyhow, convert Unicode to ANSI with the TYPE command:

TYPE number.txt > number.txt

It works, I just tried it.




Title: Re: deleting space from result
Post by: Blisk on April 07, 2019, 12:56:12 PM
Yes this works now, thank you.
TYPE number.txt > number1.txt
set /p out=<d:\number1.txt
timeout /T %out%

I created number file with this powershell
1| % {Get-Random -Minimum 2 -Maximum 222 } | Out-File -FilePath D:\number.txt
Title: Re: deleting space from result
Post by: Salmon Trout on April 07, 2019, 01:11:56 PM
Powershell writes Unicode to files by default; if you add the switch -Encoding ASCII then you can forget all the other tricks (I think)

1| % {Get-Random -Minimum 2 -Maximum 222 } | Out-File -FilePath D:\number.txt -Encoding ASCII

It worked for me. SET /P read the value to %out% and TIMEOUT /T %out% read the value properly. Please try it.

Title: Re: deleting space from result
Post by: Blisk on April 07, 2019, 01:32:29 PM
yes you are right as usually
ths works for me too.
thank you again
I didn't know I need to watch in charterset in files too