.Bat = Replace spaces in a variable with _

micromega Posted messages 141 Status Member -  
dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   -
Hello,

I need to replace the spaces in the path returned by a variable with "_" (key 8):

"%Path%\Folder"

Is it possible?

Thank you

5 answers

  1. jordane45 Posted messages 30427 Registration date   Status Moderator Last intervention   4 832
     
    Hello,

    Surely with this:

    Variable Edit/Replace

    Use the syntax below to edit and replace the characters assigned to a string variable.

    Syntax
    %variable:StrToFind=NewStr%

    %~[param_ext]$variable:Param

    Key
    StrToFind : The characters we are looking for
    NewStr : The chars to replace with (if any)
    variable : The environment variable
    param_ext : Any filename Parameter Extension
    Param : A command line parameter (e.g. 1)


    https://ss64.com/nt/syntax-replace.html

    .
    --
    Best regards,
    Jordane
    3
  2. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 660
     
    bonjour
    C:> set chemin=\a a\b b\c c\
    C:> set nc=%chemin: =_%
    C:> echo %nc%
    \a_a\b_b\c_c\

    1
  3. Anonymous user
     
    Hi,

    If you can store this path in a text file afterwards, I can do that in Python or C if you're interested.
    0
  4. micromega Posted messages 141 Status Member 1
     
    Hello,

    Thank you for the reply.
    I have quite limited knowledge of ".Bat", but no idea how to act directly in the ".bat" without having to output the path to an external file?
    Thanks anyway for the suggestion.
    0
  5. micromega Posted messages 141 Status Member 1
     
    Hello,

    The Dubcek solution is working perfectly.

    Thank you for your help.

    Last point: I have a command-line software.
    Every time, I need to specify the file path from the root of the disk where it is located (e.g., "C:\Folder 1\Folder 2\Folder 3\File")

    If I place this command-line software directly in "Folder 2", is it possible to start it from there?
    (e.g., "Folder 2\Folder 2\File")?
    Originally it does not work (for your information, I am using it combined with a ".bat".

    Thank you
    0
    1. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 660
       
      Folder 2\Folder 2\File there is no \ in front, so we need to be in Folder 1
      it's a relative path
      0