Computer Hope
Microsoft => Microsoft DOS => Topic started 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
set /p out=<d:\number.txt
TIMEOUT /T %out%
and this is what I get
-
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?
-
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
-
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?
-
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)
-
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.
-
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%
-
Try chcp 850 instead.
-
Did you change font to Lucida Console?
-
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?
-
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)
-
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
when I generate number.txt with powershell it doesn't work
Why Powershell?
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.
-
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
-
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.
-
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
-
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.
-
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