Page 1 of 1

[SOLVED] Output gets directed to the wrong folder!

Posted: 08 Aug 2020 11:21
by PAB
Good evening,

Part 1:

I have a .bat file named "Report.bat".
If I run "Report.bat" the output goes to the desktop . . .

Code: Select all

set "Output_File=%DesktopFolder%\Report.txt"
I then use this for the "Report.txt" file output . . .

Code: Select all

> "%Output_File%"
That all works great.

Part 2:

Now I have a folder named "PBU" with sub folders "bin" and "Reports".
Within the root of "PBU" I have a .bat file named "PBU_Program.bat".
I have put the "Report.bat" file in the "bin" folder.
From "PBU_Program.bat" I call the "Report.bat".

In the "Report.bat" to get it to output to the "PBU\Reports" folder instead of the desktop I changed . . .

Code: Select all

set "Output_File=%DesktopFolder%\Report.txt"
. . . to . . .

Code: Select all

cd /d "%~dp0"
set "Output_File=%~dp0Reports\Report.txt"
Whatever I seem to do I can NOT get it to output to the "PBU\Reports" folder.
On the numerous attempts I have tried it mainly gets saved to the "PBU\bin" folder.
What am I doing wrong please?

Thanks in advance.

EDIT:

I did find one way that regardless of where the "PBU_Program.bat" was run from it would still save in the "PBU\Reports" folder as long as it's run from the folder named "PBU" with sub folders "bin" and "Reports" . . .

Code: Select all

set "Output_File=%cd%\Reports\Report.txt"
The only down side is obviously that it will no longer run as a standalone .bat file from the desktop for example because there is no "Reports" folder to output to, but I can live with that. I can just comment out . . .

Code: Select all

set "Output_File=%cd%\Reports\Report.txt"
. . . and replace it with . . .

Code: Select all

set "Output_File=%DesktopFolder%\Report.txt"

Re: Output gets directed to the wrong folder!

Posted: 08 Aug 2020 13:08
by OJBakker
Your "Report.bat" is located in PBU\bin
So your code expands to PBU\bin\Reports\Reports.txt
The code below will set the Output_File to PBU\Reports\Reports.txt

Code: Select all

cd /d "%~dp0"
set "Output_File=..\Reports\Report.txt"

Re: Output gets directed to the wrong folder!

Posted: 08 Aug 2020 14:23
by PAB
Hello OJBakker,
OJBakker wrote:
08 Aug 2020 13:08
Your "Report.bat" is located in PBU\bin
So your code expands to PBU\bin\Reports\Reports.txt
The code below will set the Output_File to PBU\Reports\Reports.txt

Code: Select all

cd /d "%~dp0"
set "Output_File=..\Reports\Report.txt"
You code also works as expected!
Thank you, it is appreciated.