How to make .bat file open a file from any location?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BatchEr2
Posts: 6
Joined: 04 May 2013 03:25

How to make .bat file open a file from any location?

#1 Post by BatchEr2 » 04 May 2013 03:33

I've been doing this batch file since yesterday, and I'd like some help on this one;
If my friend downloads the .zip, extracts it somewhere and runs the .bat that opens a html file, how can the bat locate it?
On my own computer it is: C:\Users\käyttäjä1\Desktop\Download, but ofcourse the other guy doesn't have the same username.
I'd like to send this to few of my friends (not a spambot), so finding out their usernames is not an option.
Is this possible?
Vista, 7 (and 8) sample please?

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

Re: How to make .bat file open a file from any location?

#2 Post by aGerman » 04 May 2013 05:54

The path (including the trailing backslash) where the script is located can be found in %~dp0. This parameter can be used if you append the file names or pathes located relative to your script. Say there is a test.html in the same directory, see what

Code: Select all

echo "%~dp0test.html"

outputs.

Regards
aGerman

BatchEr2
Posts: 6
Joined: 04 May 2013 03:25

Re: How to make .bat file open a file from any location?

#3 Post by BatchEr2 » 04 May 2013 09:11

aGerman wrote:The path (including the trailing backslash) where the script is located can be found in %~dp0. This parameter can be used if you append the file names or pathes located relative to your script. Say there is a test.html in the same directory, see what

Code: Select all

echo "%~dp0test.html"

outputs.

Regards
aGerman


Thank you so much!

BatchEr2
Posts: 6
Joined: 04 May 2013 03:25

Re: How to make .bat file open a file from any location?

#4 Post by BatchEr2 » 05 May 2013 05:05

Well now, that works well, but how I do make it read from subfolders?
The bat is in a .zip file, with a folder with the html files etc.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to make .bat file open a file from any location?

#5 Post by foxidrive » 05 May 2013 05:15

You would extract the files and folders and then run the bat file, right?

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: How to make .bat file open a file from any location?

#6 Post by Endoro » 05 May 2013 05:19

Code: Select all

for /r "%~dp0" %%i in (*.html) do echo %%~fi

BatchEr2
Posts: 6
Joined: 04 May 2013 03:25

Re: How to make .bat file open a file from any location?

#7 Post by BatchEr2 » 05 May 2013 06:36

Endoro wrote:

Code: Select all

for /r "%~dp0" %%i in (*.html) do echo %%~fi

Could you explain this please? Do I just replace the star with the file name?
foxidrive wrote:You would extract the files and folders and then run the bat file, right?

Yes, but if possible, no.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to make .bat file open a file from any location?

#8 Post by foxidrive » 05 May 2013 06:48

BatchEr2 wrote:
foxidrive wrote:You would extract the files and folders and then run the bat file, right?

Yes, but if possible, no.


That's what I thought - you cannot execute a batch file and have it use related files from an archive.

You'd need to unzip the archive with folders, and then in the batch file you would use relative paths like this:

Code: Select all

@echo off
start "" "folder_one\internet.html"


where the file internet.html is inside "folder_one" that was inside the ZIP archive and assuming the batch file was not in a folder in the ZIP archive.

Am I sufficiently clear in the description?

BatchEr2
Posts: 6
Joined: 04 May 2013 03:25

Re: How to make .bat file open a file from any location?

#9 Post by BatchEr2 » 05 May 2013 06:55

foxidrive wrote:
BatchEr2 wrote:
foxidrive wrote:You would extract the files and folders and then run the bat file, right?

Yes, but if possible, no.


That's what I thought - you cannot execute a batch file and have it use related files from an archive.

You'd need to unzip the archive with folders, and then in the batch file you would use relative paths like this:

Code: Select all

@echo off
start "" "folder_one\internet.html"


where the file internet.html is inside "folder_one" that was inside the ZIP archive and assuming the batch file was not in a folder in the ZIP archive.

Am I sufficiently clear in the description?

Quite so. But now with that code, Chrome tries to open 'source/page.html' ("source" is my folder inside the .zip, the bat file comes with the .zip, but is not in the subfolder)
and ofcourse it won't just work.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to make .bat file open a file from any location?

#10 Post by foxidrive » 05 May 2013 07:46

I don't know where the bat is.

Explain the paths of the bat file and the folder with the HTML


If the paths of the extracted files are:

current folder\go.bat
current folder\source\page.html

then the code I gave before will work, with source\page.html

BatchEr2
Posts: 6
Joined: 04 May 2013 03:25

Re: How to make .bat file open a file from any location?

#11 Post by BatchEr2 » 05 May 2013 10:02

Yeah, thanks, got it all working.
My friends will kill me after opening this.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to make .bat file open a file from any location?

#12 Post by foxidrive » 05 May 2013 10:28

I am hoping your joke is trivial and totally non-destructive.

If it's at all damaging then don't distribute it, k?

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: How to make .bat file open a file from any location?

#13 Post by Endoro » 05 May 2013 11:45

It is our responsibility to answer questions here. :mrgreen:

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to make .bat file open a file from any location?

#14 Post by Squashman » 05 May 2013 12:17

Endoro wrote:It is our responsibility to answer questions here. :mrgreen:

But those of us who are moderators have to keep a close eye on kids who are just trying to be malicious or who have no idea what they are doing because their scripts could destroy someones data. Those threads will be closed.

Post Reply