Grab powershell variable from .bat file in same directory

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bearwires
Posts: 7
Joined: 06 Jun 2017 06:40

Grab powershell variable from .bat file in same directory

#1 Post by bearwires » 15 Jul 2017 11:15

Hey guys, I am a complete newb with bash and powershell scripting and have been trying to figure out how to define a variable in powershell with a variable defined in a batch script stored within the same directory as the powershell script.

Basically, I want to get the Dropbox Path from config.bat and set variable $dropbox for use in other variables within the powershell script. This is what I have so far....


config.bat:

Code: Select all

    
    @Echo OFF
    set Dropbox_Path=C:\Users\User\Documents\Dropbox


Extract of test.ps1:

Code: Select all

    #Grab Dropbox Path from config.bat and set $dropbox variable

    $dropbox = Get-Content C:\Users\User\Documents\Dropbox\test\Batch-Files\config.bat `
              | ? { $_ -match "Dropbox_Path=\s*"} `
              | select -First 1 `
              | % { ($_ -split "=", 2)[1] }

    # Set other Variables using $dropbox

    $input_csv = "$dropbox\test\Test.csv"
    $master_csv = "$dropbox\test\Test-MASTER.csv"
    $output_file = "$dropbox\test\Test.txt"
 


Everything works fine if I specify the absolute path for the config.bat file (as shown) but if I try using relative paths $PSScriptRoot\config.bat or .\config.bat, I get errors and powerhsell cannot find the config.bat file showing error...

"Get-Content : Cannot find path 'C:\config.bat' because it does not exist."

I have also tried an alternative way and used write-host to see if the variables show the correct directories....

Code: Select all

    $configFullPath = "$($pwd)\config.bat"
    Write-Host "Config file full path = $configFullPath"

    Import-Module -Force $configFullPath

    $dropbox = Get-Content _config.bat `
              | ? { $_ -match "Dropbox_Path=\s*"} `
              | select -First 1 `
              | % { ($_ -split "=", 2)[1] }

    Write-Host "Config file full path = $configFullPath"
    Write-Host "Dropbox Path = $dropbox"
    PAUSE

With the above code, I get this PS error....

"Import-Module : The specified module 'C:\Users\User\config.bat' was not loaded because no valid module file was found in any module directory."

The output shows:

Code: Select all

    Config file full path = C:\Users\User\config.bat    # WRONG
    Dropbox Path =

The path needs to be relative for my purposes and its probably something embarrassingly super simple that the coding gurus here will make me look like a complete numpty :)

Please help!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Grab powershell variable from .bat file in same directory

#2 Post by aGerman » 15 Jul 2017 15:21

I'm not familiar with Powershell but this should work:

Code: Select all

    $dropbox = Get-Content (Join-Path $PSScriptRoot 'config.bat') `
              | ? { $_ -match "Dropbox_Path=\s*"} `
              | select -First 1 `
              | % { ($_ -split "=", 2)[1] }

FWIW the \s* is most likely not what you intended. It works because it's useless.

Steffen

bearwires
Posts: 7
Joined: 06 Jun 2017 06:40

Re: Grab powershell variable from .bat file in same directory

#3 Post by bearwires » 15 Jul 2017 16:05

I get this error Steffan:

Code: Select all

Join-Path : Cannot bind argument to parameter 'Path' because it is an empty string.
At line:1 char:35
+ $dropbox = Get-Content (Join-Path $PSScriptRoot 'config.bat') `
+                                                     ~~~~~~~~~


I thought the \s* selected anything but white space after the =
So I can just omit it from the code if it is useless?
This is why I'm a newb :oops: I wish I learned coding when I was younger instead of electrical engineering lol
I'm sure this will start to make more sense as the years go by :D

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Grab powershell variable from .bat file in same directory

#4 Post by aGerman » 15 Jul 2017 16:48

Sounds like $PSScriptRoot isn't pre-defined. Older PS versions may work with that:

Code: Select all

    $dropbox = Get-Content (Join-Path (Split-Path -parent $MyInvocation.MyCommand.Definition) 'config.bat') `
              | ? { $_ -match "Dropbox_Path="} `
              | select -First 1 `
              | % { ($_ -split "=", 2)[1] }


bearwires wrote:I thought the \s* selected anything but white space

No. The \s (lower case s) matches white space characters. The * quantifier is for "zero or more times" and this "zero" is the reason why it even works. To answer your question - yes omit the \s*.

Steffen

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Grab powershell variable from .bat file in same directory

#5 Post by penpen » 15 Jul 2017 17:08

@bearwires
I tried your first script with "$PSScriptRoot\config.bat" and ".\config.bat" instead of the absolute path "C:\Users\User\Documents\Dropbox\test\Batch-Files\config.bat".
I also appended these lines (just for testing):

Code: Select all

echo $PSScriptRoot
echo $input_csv
echo $master_csv
echo $output_file


It is working flawlessly under my 32 bit win10 (using the command shell "cmd.exe"):

Code: Select all

Z:\with space>powershell -ExecutionPolicy RemoteSigned -File "test.ps1"
Z:\with space
C:\Users\User\Documents\Dropbox\test\Test.csv
C:\Users\User\Documents\Dropbox\test\Test-MASTER.csv
C:\Users\User\Documents\Dropbox\test\Test.txt

Z:\with space>


penpen

bearwires
Posts: 7
Joined: 06 Jun 2017 06:40

Re: Grab powershell variable from .bat file in same directory

#6 Post by bearwires » 15 Jul 2017 18:35

@penpen
You are correct, the problem wasn't with any of the code, it was newbie user error :oops:
I was copying the code from notepad++ to powershell window instead of running the actual .ps1 script file from the folder :roll: :shock: Doh!!

Thanks Steffen and penpen for your assistance

Post Reply