1.

Solve : Using the DIR function after launching a .BAT file from a Javascript?

Answer»

Hi,

I create a Photoshop Javascript that does some stuff and then launches a .BAT file to do the rest of the job. The problem is that the batch file needs to look into it's own folder to find files and do stuff with them, but since I launched the .BAT file from a javascript, it doesn't know what his own folder is. I tried using the DIR function in the .bat to see what folder it was looking into and instead of looking in it's own folder in my documents it looks on the M: drive for some reason.

I tried running the batch file by itself and it works correctly, it's only when I launch it from Javascript that it GETS mixed up. Is there a way to fix that, or is there a way to link to the .bat file's own directory with some code?


I'd also like to know if it's possible to send a variable from Javascript to a Batch file? Right now to do it I create a temp file with the variable and the batch file gets the temp file into ONE of it's own variables, but is there a way to pass a variable directly as we can do in the Run tool in the Start menu?

Thanks,Quote from: FausseFugue on June 25, 2008, 02:13:24 PM

I tried running the batch file by itself and it works correctly, it's only when I launch it from Javascript that it gets mixed up. Is there a way to fix that, or is there a way to link to the .bat file's own directory with some code?

I take it the folder that contains the java script your running is different from the batch file's working directory.

I'd add "CD C:\path\to\batch\file\working\directory" to the top of the batch file. That way when it does the rest of code it's ALREADY in the correct location.

Quote from: FausseFugue on June 25, 2008, 02:13:24 PM
I'd also like to know if it's possible to send a variable from Javascript to a Batch file?

Yeah, when starting the batch file add the variable to the end. "run batch_file.bat variable" (forgive my complete lack of javascript knowage!!)

I believe you can add up to 3 variables this way. The batch file will see these variables as %1, %2 and %3 (in entered order)

You can then use "set variable_name=%1" and so on.

Hope it helps. Every batch file knows the folder where it is located (stored). This is held in a special variable %0 (read that as percent zero). You can use the normal variable modifiers so that %~dp0 is the complete drive letter and path.Quote from: Dias de verano on June 25, 2008, 03:41:48 PM
Every batch file knows the folder where it is located (stored). This is held in a special variable %0

Didn't know that....

Nice one. Quote from: blastman on June 25, 2008, 04:04:15 PM
Quote from: Dias de verano on June 25, 2008, 03:41:48 PM
Every batch file knows the folder where it is located (stored). This is held in a special variable %0

Didn't know that....

Nice one.

I made a slight error.

%0 is the batch file's own FILENAME

%~dpnx0 is its drive, path, filename and extension

%~dp0 is its drive and path
Wow, thanks a lot to you both for this quick reply, this will really help!!!Actually,

I just tested and both solutions don't work.

For:
Quote
"run batch_file.bat variable"
In Javascript we use the execute command, so it normally looks like this:
File("C:/Folder/File.bat").execute()
so I tried:
File("C:/Folder/File.bat variable").execute()
but it doesn't work. It doesn't do anything, it doesn't EVEN execute the batch file.

For:
Quote
%~dp0 is its drive and path
I tried:
ECHO %~dp0
that works fine.
But when I try:
CD %~dp0
it doesn't work.


If anyone would know a solution to those two problems I would really appreciate!

Thanks!Quote from: FausseFugue on June 26, 2008, 06:56:53 AM
File("C:/Folder/File.bat variable").execute()

Sounds like it looking for the file "file.bat varaible" which does exist. I'd try;

File(""C:\folder\file.bat " variable").execute()

Pleae bear in mind I know NO JS at all! this is based on other shell script's ie, vbs

Quote from: FausseFugue link=topic=60062.msg380322#msg380322
But when I try:
CD %~dp0
it doesn't work.

"It doesn't work"... every help forum helper's favourite answer!!! (Not). Just how does it "not work"?

try

cd /d "%~dp0"Thank you both again!

Quote
File(""C:\folder\file.bat " variable").execute()
That doesn't work in Javascript, but now I don't absolutely need that anymore since this works:
Quote
cd /d "%~dp0"
so I can store my variables in a tmp file in the folder where the .bat file is located and use this code Code: [Select]SET /P VARIABLE=<"variable.tmp" to get the variable in the .bat file.

Thank you!


Discussion

No Comment Found