-2

I need to use variable from batch file within a text file. Not sure what's the best and easiest way to achieve that.

Batch file code:

set back=%cd%
for /d %%i in (C:\input\*) do (
cd "%%i"
set x=%%~nxi
CALL E:\FileMoving_run.bat --context=Default --context_param prop_file_move=C:\file_move.txt
cd %back%
pause
)
cd %back%

Content of text file -C:\file_move.txt specified below. The way I am trying to pass here is definitely not working.

path=C:\Metadata_input\%x%\
elec_path=C:\OHD\%x%\
vsingh
  • 125
  • 1
  • 2
  • 16
  • I'm not sure what you're showing. Is the text file content you show the content of `E:\FileMoving_run.bat`? Didn't you try `%1`, `%2`, etc for the command line arguments? It would have no way of knowing what `%x%` is (`x` isn't defined anywhere). `%0` `%1`, ... are predefined. See [Get list of passed arguments in Windows Bash script .bat](https://stackoverflow.com/questions/357315/get-list-of-passed-arguments-in-windows-batch-script-bat) – lurker Oct 02 '19 at 17:21
  • text file is C:\file_move.txt and that has been passed as param to E:\FileMoving_run.bat. so those contents belong to this file_move.txt fie. – vsingh Oct 02 '19 at 17:29
  • You'll need to show the contents of `FileMoving_run.bat`, or at least relevant portions of it. – lurker Oct 02 '19 at 17:35
  • I don't have the access to that file yet. but I am not sure if the content of the file matters. Because my question over here is about being able to use the variable 'x' which I have setup in my primary bat file for which code is in the problem statement, and then try to use that variable in my txt file. FileMoving_run.bat is just consuming that txt file and my control is not even reaching there yet. – vsingh Oct 02 '19 at 18:15

1 Answers1

1

Based upon my understanding of your question, you could try:

@For /D %%A In ("C:\input\*")Do @(
    (Echo path=C:\Metadata_input\%%~nxA\
    Echo elec_path=C:\OHD\%%~nxA\)>"C:\file_move.txt"
    Call "E:\FileMoving_run.bat" --context=Default --context_param prop_file_move="C:\file_move.txt"
)
@Del "C:\file_move.txt"
Compo
  • 36,585
  • 5
  • 27
  • 39
  • 2
    `Echo path=%%A\ ` and `Echo elec_path=C:\OHD\%%~nxA\ ` – Stephan Oct 02 '19 at 20:05
  • I did try this code. Somehow, when I used %%A, path in the text file was coming weird , for example: path=C:\Metadata_input\C:\Metadata_input\fo\ So I tried setting up a variable as set x=%%~nxa. And then tried passing it on to "Echo path=C:\Metadata_input\%x%\" but value is getting passed as empty. – vsingh Oct 02 '19 at 20:20
  • @Stephan When I tried using Echo elec_path=C:\OHD\%%~nxA\, Value of elec_path in text file appearing as C:\OHD\%~nxA\, – vsingh Oct 02 '19 at 20:24
  • I was using %%~nxA instead of %%~nxa. working fine now. – vsingh Oct 02 '19 at 20:30
  • 1
    @vsingh: just for info: your `%x%` didn't work because of not using [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) – Stephan Oct 02 '19 at 20:33
  • is it the correct way to use the statement where I am calling "E:\FileMoving_run.bat" --context=Default --context_param prop_file_move="C:\file_move.txt". When I run this whole statement via cmd directly, it works fine. but it doesn't seem to be getting executed as part of this batch script. text file is getting generated correctly. – vsingh Oct 02 '19 at 20:46