0

I have a file (let's call it version.txt) that contains a version number and some text:

v5.02

Some text explaining
where and how this
number is used

Based on this answer, I use

set /p version=<version.txt

to store the first line of the file in the version variable. Now I'm trying to write a batch script that operates on folders that contain this version number in their name. However, I get unexpected results because something seems to go wrong when I insert the variable in a path. For example, this script

@set /p version=<version.txt
@echo C:\some\folder\%version%\some\file.exe

prints

C:\some\folder\v5.02

instead of

C:\some\folder\v5.02\some\file.exe

What's going on? I have a feeling there are hidden characters of some sort at the end of the text in the variable, because setting the variable by hand to a constant in the script works.

Edit: I'm using Windows 10 with Notepad++ as my editor, if it helps.

optical
  • 161
  • 7
  • 2
    Please [edit] your post to include the code you're actually using, rather than linking to another answer. Some change you made to the code could be causing the issue, and we can't tell if that's the case if we can't see your actual code in the form of a [mre]. – Ken White Jul 07 '21 at 00:28
  • 2
    Your code works as-is in plain ASCII, so it could be content, as you suspect. Open the file with PSPad or similar. It has a hex mode so you can see exactly what's in the file. As a stab in the dark, you might want to put `chcp 65001` on a line before your `set` command. It enables Unicode for the script. Maybe consider rewriting in PowerShell, It will give you better control of picking off and scrubbing the content. – Tony Jul 07 '21 at 01:01
  • This is strange, because I typed exactly what I wrote in my question in a new text file and a new batch file, and I get the truncated output. I tried to include `chcp 65001` before the `set` command, but it didn't change anything (apart from `Active code page: 65001` being printed before `C:\some\folder\v5.02`). – optical Jul 07 '21 at 11:30
  • The described issue occurs, when `version.txt` has Unix line endings (`LF`) instead of Windows line endings (`CRLF`), so it's not "hidden characters", but "missing characters". – Stephan Jul 07 '21 at 14:20

2 Answers2

0

Since everything I tried didn't seem to work, the solution I found in the end is to call the batch script from a Python script. The Python script reads the first line of the version file and passes it as an argument to the batch script. Out of context, it is a bit of an inelegant solution, but in my case the batch script was already called by a Python script, so it's not that terrible.

Here is a minimal example:

version.txt

v5.02

Some text explaining
where and how this
number is used

script.bat

@echo C:\some\folder\release\%1\some\file.exe

script.py

import os
with open("version.txt") as f:
    version = f.readline().rstrip()
os.system("cmd /c script.bat %s" % version)

Edit: Following Stephan's comment, I tried to change the line ending in the text file from LF to CRLF and it indeed solves the problem. However, since I don't really have control over everything that writes in that file, the solution above remains the most feasible in my case.


Edit 2: Stephan's answer (with the for loop) is actually a better solution than this one since it avoids having to transfer part of the work to the calling Python script.

optical
  • 161
  • 7
0

I can only replicate your issue, when version.txt uses Unix line endings (LF) instead of Windows (CRLF). for /f is immune to this issue:

for /f "delims=" %%a in (version.txt) do set "verion=%%a" & goto :skip
:skip
echo C:\some\folder\%version%\some\file.exe

goto :skip breaks the loop after reading the first line.

Stephan
  • 53,940
  • 10
  • 58
  • 91