Opening a file in a different folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
sp11k3t3ht3rd
Posts: 26
Joined: 20 May 2010 15:39

Opening a file in a different folder

#1 Post by sp11k3t3ht3rd » 25 Jul 2010 11:53

Okay, with my previous question about the Java. Now I made it so when you compile it puts the files needed to run in a folder called bin. I need to be able to open a file and then delete it after its closed.

Code: Select all

if /i "%classname%"=="BeerSong" (
  >%classname%.txt 2>&1 java %classname%
start %classname%.txt
) else (
echo @echo off >>run.bat
echo java %classname% >>run.bat
echo pause >>run.bat
echo exit >>run.bat
start run.bat
)


start "bin\run.bat" < That doesn't work but I don't want to type the whole directory out all the time because often times it will be different and I don't want to have to change it every time.

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

Re: Opening a file in a different folder

#2 Post by aGerman » 25 Jul 2010 12:44

start "bin\run.bat" will not work because the first argument enclosed in quotes is interpretet as window title. Try
start "Run Batch" "bin\run.bat"

BTW: A batch file will close if it is on the end of code. The EXIT command is useless in this case.

Instead of redirect "exit" to the new run.bat you could make that the run.bat will delete itself.
If the current batch file is placed in the java folder, you could write

Code: Select all

if /i "%classname%"=="BeerSong" (
  >%classname%.txt 2>&1 java bin\%classname%
  start %classname%.txt
) else (
  >bin\run.bat echo @echo off
  >>bin\run.bat echo java %classname%
  >>bin\run.bat echo pause
  >>bin\run.bat echo del /s /q "%%~0"
  start "Run Batch" "bin\run.bat"
)

sp11k3t3ht3rd
Posts: 26
Joined: 20 May 2010 15:39

Re: Opening a file in a different folder

#3 Post by sp11k3t3ht3rd » 25 Jul 2010 13:10

That code isn't working:

Code: Select all

if /i "%classname%"=="BeerSong" (
   >%classname%.txt 2>&1 java bin\%classname%
   start %classname%.txt
) else (
   >bin\run.bat echo @echo off
   >>bin\run.bat echo title Run
   >>bin\run.bat echo java %classname%
   >>bin\run.bat echo pause
   >>bin\run.bat echo del /s /q "%%~0"
   start "Run" "bin\run.bat"
)


In the actual file after you enter the classname is says:
Access is denied.
Access is denied.
Access is denied.
Access is denied.
Access is denied.
Press any key to continue . . .


The file made says:
Exception in thread "main" java.lang.NoClassDefFoundError: boy
Caused by: java.lang.ClassNotFoundException: boy
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: boy. Program will exit.
Press any key to continue . . .


That means that it couldn't find the class and didn't run it.

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

Re: Opening a file in a different folder

#4 Post by aGerman » 25 Jul 2010 13:28

I wrote
If the current batch file is placed in the java folder,...

Where is it placed?

Regards
aGerman

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

Re: Opening a file in a different folder

#5 Post by aGerman » 25 Jul 2010 13:45

[edit]
Maybe I found my bad. Could it be java needs the entire path?
Where are the classes placed?
[/edit]

sp11k3t3ht3rd
Posts: 26
Joined: 20 May 2010 15:39

Re: Opening a file in a different folder

#6 Post by sp11k3t3ht3rd » 25 Jul 2010 13:50

This is the folder structure. Java > bin
> other project folders

The original batch file is at C:\Users\Mike.Mike-PC\Desktop\Programming\Java

The classes along with the run.bat created go to C:\Users\Mike.Mike-PC\Desktop\Programming\Java\bin

When the original file it run it is run from outside the bin folder and puts the classes in it along with the run file. Then I'm trying to call the run file from within

C:\Users\Mike.Mike-PC\Desktop\Programming\Java\bin

Still having the original file at C:\Users\Mike.Mike-PC\Desktop\Programming\Java

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

Re: Opening a file in a different folder

#7 Post by aGerman » 25 Jul 2010 14:06

The output "Access is denied." bothers me. Can you create a file in bin by hand?

Ok. Drive and path of the current batch file can you find in %~dp0. So lets work with it.

Code: Select all

if /i "%classname%"=="BeerSong" (
   >%classname%.txt 2>&1 java "%~dp0bin\%classname%"
   start %classname%.txt
) else (
   >"%~dp0bin\run.bat" echo @echo off
   >>"%~dp0bin\run.bat" echo title Run
   >>"%~dp0bin\run.bat" echo java %classname%
   >>"%~dp0bin\run.bat" echo pause
   >>"%~dp0bin\run.bat" echo del /s /q "%%~0"
   start "Run" "%~dp0bin\run.bat"
)


Regards
aGerman

sp11k3t3ht3rd
Posts: 26
Joined: 20 May 2010 15:39

Re: Opening a file in a different folder

#8 Post by sp11k3t3ht3rd » 25 Jul 2010 19:34

Its still not working. I know that you can change drives from the command line. Is there something similar that we can do here? The reason I don't want to make the run file every time is for ease of use.

sp11k3t3ht3rd
Posts: 26
Joined: 20 May 2010 15:39

Re: Opening a file in a different folder

#9 Post by sp11k3t3ht3rd » 25 Jul 2010 19:55

Just so you know, I'm only 15 years old. This week I'm going to be at a summer camp, so I will not be able to get your response because it's a sleep away camp.

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

Re: Opening a file in a different folder

#10 Post by aGerman » 25 Jul 2010 20:40

sp11k3t3ht3rd wrote:This week I'm going to be at a summer camp, ...

Have fun! :wink:

You can change the working directory by CD. But in this case I would prefer PUSHD, because you can easy change back by POPD.
Try:

Code: Select all

pushd bin
if /i "%classname%"=="BeerSong" (
  >"%dp~0%classname%.txt" 2>&1 java %classname%
  start "" "%dp~0%classname%.txt"
) else (
  java %classname%
  pause
)
popd


Regards
aGerman

sp11k3t3ht3rd
Posts: 26
Joined: 20 May 2010 15:39

Re: Opening a file in a different folder

#11 Post by sp11k3t3ht3rd » 30 Jul 2010 11:43

Have fun! :wink:


Thanks! I just got back. I tried that but when I run it and get to that point in the code it says "The syntax of the command is incorrect." and then the window closes really fast.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Opening a file in a different folder

#12 Post by miskox » 30 Jul 2010 13:01

I would suggest you do this:

Code: Select all

echo on
pushd bin
if /i "%classname%"=="BeerSong" (
  >"%dp~0%classname%.txt" 2>&1 java %classname%
  start "" "%dp~0%classname%.txt"
  pause
) else (
  java %classname%
  pause
)
popd
pause


This should wait everywhere. And when you get an error just post it here. Also you could open a Command prompt window and start the batch from there (so the window will not close at all).

Saso

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

Re: Opening a file in a different folder

#13 Post by aGerman » 30 Jul 2010 14:43

My suggestion to find the bug:

1st Remove @echo off and write cd /d "%~dp0" instead. Now save the code.
2nd Open a command prompt ([Windows]+[R] > cmd > OK)
3rd Drop your batchfile to the command prompt and hit Enter
4th Post the last executed lines here

Regards
aGerman

sp11k3t3ht3rd
Posts: 26
Joined: 20 May 2010 15:39

Re: Opening a file in a different folder

#14 Post by sp11k3t3ht3rd » 30 Jul 2010 17:18

I went through the program and entered the classname and then got this.
C:\Users\Mike.Mike-PC\Desktop\Programming\Java>pushd bin
The syntax of the command is incorrect.

C:\Users\Mike.Mike-PC\Desktop\Programming\Java\bin> >"classnameclassname

C:\Users\Mike.Mike-PC\Desktop\Programming\Java\bin>

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

Re: Opening a file in a different folder

#15 Post by aGerman » 30 Jul 2010 17:53

My bad, sorry.
%dp~0 must be %~dp0

Code: Select all

pushd bin
if /i "%classname%"=="BeerSong" (
  >"%~dp0%classname%.txt" 2>&1 java %classname%
  start "" "%~dp0%classname%.txt"
) else (
  java %classname%
  pause
)
popd


Please try again.

Regards
aGerman

Post Reply