-1

I'm trying to make a system which turns the contents of a .txt file into a variable. This isn't my problem, though; for some reason, my files are reading characters I didn't enter, and can't use.

Please note: What I'm doing is in no way efficient, and I'm positive there are other ways to go about this, but this is the best way for me. Also, I'm not amazingly intelligent when it comes to coding. Just thought I'd throw that out there.

First, let me show you the system I have in place.

value.txt

4

This file has the contents which I'd like to make into a variable.

Batch Files

setcmdvar.bat

set cmdvar=

I leave this empty so that I can put the contents of value.txt at the end (more on this later).

start.bat

@echo off
call PowerShell.exe cd "C:\Users\%username%\Desktop\Folder"; $PSvar = Get-Content value.txt; $PSvar >> setcmdvar.bat
pause
call setcmdvar.bat
pause
echo The variable equals %cmdvar%.
pause
exit

The second line from start.bat creates this script in PowerShell:

PowerShell script

cd "C:\Users\%username%\Desktop\Folder\"
$PSvar = Get-Content value.txt; $PSvar >> setcmdvar.bat

This creates a variable in PowerShell, $PSvar, which equals the contents of value.txt; in our case, 4. Then, it puts $PSvar (4) at the end of setcmdvar.bat, using >>, which changes it to:

setcmdvar.bat (changed)

set cmdvar=4

Or, at least, it should, to my knowledge. Instead, it changes the file to this:

set Items=桔⁥瑳楲杮椠⁳業獳湩⁧桴⁥整浲湩瑡牯›⸢ †⬠䌠瑡来牯䥹普††††㨠倠牡敳䕲牲牯›㨨
嵛‬慐敲瑮潃瑮楡獮牅潲割捥牯䕤捸灥楴湯 †⬠䘠汵祬畑污晩敩䕤牲牯摉㨠吠牥業慮潴䕲灸捥整䅤䕴摮晏瑓楲杮 

Or some other strange combination of characters. I looked one up, and it was Chinese. There's also some other characters like †, ⬠, ›, ⸢, 
, and ⁳. I have no idea why these are being typed. Along with this, start.bat displays the following:

Press any key to continue . . .
(PowerShell script runs here)
'■s' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . .
The variable equals .
Press any key to continue . . .
(exit)

I did not type "■s," and I assume this may be the problem. Whatever it is, does anyone have any ideas?

P.S. I'm sorry if my code is complicated, or if it looks bad, but I have it this way for a reason. Mostly my incompetence, actually. But I think it's better that way.

Also, I know there are commands like for /f "delims=" %a in ('ver') do @set foobar=%a (I just took this off the internet) but I've tried commands like those, and I suppose I just don't understand them all that well, because they didn't work.

I appreciate the help!

IgroSome
  • 3
  • 4
  • 1
    Powershell adds a byte order marker to text by default. Batch prefers ANSI encoding. https://stackoverflow.com/questions/5596982/using-powershell-to-write-a-file-in-utf-8-without-the-bom – SomethingDark Jun 12 '20 at 20:52
  • `>>` which is more correctly `Out-file -Append` can mix two encodings in the same file, even if the file was originally ASCII or ANSI. I would suggest instead that you use `Add-Content`, which should check the encoding and match that already existing when appending. BTW, the default for `Get-Content` is to use `UTF8NoBOM`, so it may also be necessary for you to use the `-Encoding` parameter, with that, probably as `ASCII`, and obviously the same parameter is available for `Add-Content` whose default is also `UTF8NoBOM` too. As a side note, `Call` is for batch files or internal labels in them! – Compo Jun 12 '20 at 21:37
  • @Compo Yes, start.bat (which includes call) is a batch file. I appreciate the help. However, I'm still getting the same issues. – IgroSome Jun 12 '20 at 21:48
  • `PowerShell.exe` is definitely not a batch file, @IgroSome, hence the reason I told you about `Call`! – Compo Jun 12 '20 at 22:23
  • 1
    You have really over complicated this whole process. Either stick to doing everything in a batch file or powershell. It makes no sense doing what you are doing. In the simplest form to read the first line of a file in a batch file you can do this: `set /p var= – Squashman Jun 12 '20 at 22:47
  • @Compo I know it's not a batch file, I'm not _that_ stupid. – IgroSome Jun 12 '20 at 23:57
  • @Squashman That's true, but I'd rather it automatically sets the variable, without user input. – IgroSome Jun 12 '20 at 23:58
  • If the `Call` command 'Calls one batch program from another', and `PowerShell.exe` isn't a batch program, and if you're not _that_ stupid @IgroSome, why have you used `call PowerShell.exe`? – Compo Jun 13 '20 at 09:16
  • @IgroSome, the code I gave you takes **NO** user input. It takes the input from the file. I thought I made that pretty clear in my comment. – Squashman Jun 13 '20 at 20:50

2 Answers2

0

>> has its problems with mixing encodings. Plus it defaults to utf16. I recommend changing

$PSvar >> setcmdvar.bat

to

add-content setcmdvar.bat $PSvar

I'm not sure how to keep everything on one line. You can make setcmdvar.bat like this:

set cmdvar=^

So that it continues.

js2010
  • 13,551
  • 2
  • 28
  • 40
0

It would probably be better to avoid the static setcmdvar.bat script and write it all in the script. Using -Encoding ascii is what keeps the output from being Unicode and having a BOM (Byte Order Mark) at the beginning. It has nothing to do with Chinese characters.

ECHO>"%USERPROFILE%\value.txt" 4
SET "CMDFILE=%USERPROFILE%\setcmdvar.bat"
powershell -NoLogo -NoProfile -Command ^
    "'SET ""cmdvar=' + (Get-Content $Env:USERPROFILE\value.txt) + '""""' |" ^
    "Out-File -FilePath %CMDFILE% -Encoding ascii"
pause
call "%CMDFILE%"
pause
echo The variable equals %cmdvar%
pause
EXIT /B 0
lit
  • 10,936
  • 7
  • 49
  • 80
  • The ascii encoding can be limiting for certain languages. Note that the standalone versions of PowerShell (v7+ definitely, v6 I believe) have both the UTF8NoBom and UTF8BOM encodings. The former has no BOM while the latter does, if that is not obvious. – Bacon Bits Jun 13 '20 at 01:48