[SOLVED] Output gets directed to the wrong folder!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PAB
Posts: 139
Joined: 12 Aug 2019 13:57

[SOLVED] Output gets directed to the wrong folder!

#1 Post by PAB » 08 Aug 2020 11:21

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"
Last edited by PAB on 11 Aug 2020 14:43, edited 2 times in total.

OJBakker
Expert
Posts: 88
Joined: 12 Aug 2011 13:57

Re: Output gets directed to the wrong folder!

#2 Post by OJBakker » 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"

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Output gets directed to the wrong folder!

#3 Post by PAB » 08 Aug 2020 14:23

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.

Post Reply