Author Topic: Executing bat files in current drawing location  (Read 5467 times)

0 Members and 1 Guest are viewing this topic.

dubb

  • Swamp Rat
  • Posts: 1105
Executing bat files in current drawing location
« on: October 20, 2017, 06:56:41 PM »
I noticed when I ran a bat file from autolisp they failed to run when my current drawing is from our server. When I ran a bat file with my current drawing located on my local hard drive it worked. Could this be a trustedlocations issue or a networking issue?

ChrisCarlson

  • Guest
Re: Executing bat files in current drawing location
« Reply #1 on: October 23, 2017, 02:42:05 PM »
Does the batch file run independently from AutoCAD?

JohnK

  • Administrator
  • Seagull
  • Posts: 10640
Re: Executing bat files in current drawing location
« Reply #2 on: October 23, 2017, 04:07:32 PM »
It's a windows and path issue. Follow the following template for your bat file.

Code - Text: [Select]
  1. cls
  2. @pushd %~dp0
  3.  
  4. ::: SCRIPT HERE :::
  5.  
  6. @popd
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

dubb

  • Swamp Rat
  • Posts: 1105
Re: Executing bat files in current drawing location
« Reply #3 on: October 24, 2017, 11:38:01 AM »
Thanks! sorry for the late reply. But it seems to work with your code.

@master_shake. I'm not sure what you mean. The bat file is called with (startapp "c:/tools/sync.bat?")

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Executing bat files in current drawing location
« Reply #4 on: October 24, 2017, 12:26:01 PM »
You could also use something like this to suppress the box from popping up:

Refer to code HERE.
« Last Edit: October 26, 2017, 09:12:33 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

dubb

  • Swamp Rat
  • Posts: 1105
Re: Executing bat files in current drawing location
« Reply #5 on: October 24, 2017, 02:59:51 PM »
Oh Yes! I have been looking all over the web on how to achieve this. On stackexchange I keep reading on who to make a .vbs script but I don't want to create another external file. Thanks Ron!

You could also use something like this to suppress the box from popping up:

Code - Auto/Visual Lisp: [Select]
  1. (defun _run (file / ws)
  2.   (if (and (findfile file) (setq ws (vlax-get-or-create-object "wscript.shell")))
  3.     (progn (vlax-invoke ws 'run file 0 -1) (vlax-release-object ws) file)
  4.   )
  5. )
  6. ;; (_run (strcat (getenv "userprofile")"\\Desktop\\Test.bat"))

dubb

  • Swamp Rat
  • Posts: 1105
Re: Executing bat files in current drawing location
« Reply #6 on: October 24, 2017, 03:20:03 PM »
For some reason I can't execute the function like this
Code: [Select]
(_run "c:/tools/sync.bat")

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Executing bat files in current drawing location
« Reply #7 on: October 24, 2017, 09:19:56 PM »
I'll have to take a look in the morning.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Executing bat files in current drawing location
« Reply #8 on: October 25, 2017, 09:20:07 AM »
For some reason I can't execute the function like this
Code: [Select]
(_run "c:/tools/sync.bat")
You're right. I can't get it to work either  :oops:  .. perhaps you could use THIS if you're just updating files. I know this one works as I've been using it for years.

It's working here, both types of paths:

Code - Auto/Visual Lisp: [Select]
  1. (defun _run (file / ws)
  2.   (if (and (findfile file) (setq ws (vlax-get-or-create-object "wscript.shell")))
  3.     (progn (vlax-invoke-method ws 'run (strcat "\"" file "\"") 0) (vlax-release-object ws) file)
  4.   )
  5. )
  6. (_run (setq path (strcat (getenv "userprofile") "\\Desktop\\test with spaces\\Test.bat"))) ;;<-Works here
  7. (_run (vl-string-translate "\\" "/" path)) ;;<-Works here
*Added quotes like Roy & vlax-release-object <- got lost from first post
« Last Edit: October 26, 2017, 09:11:49 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

dubb

  • Swamp Rat
  • Posts: 1105
Re: Executing bat files in current drawing location
« Reply #9 on: October 25, 2017, 06:41:27 PM »
Good morning/afternoon! I got really busy, started working on a reply. I need to correct my initial response.

Code: [Select]
(_run "C:\\tools\\sync.bat") It worked! Yay!

But in my real code I have spaces in the naming of directories so tested to find the culprit.
Code: [Select]
(_run "C:\\Test Test\\sync.bat") Doesn't work

So I tried chr(34) to add double quotes.
Code: [Select]
(_run "chr(34) & c:\\Test Test\\sync.bat & chr(34)") Returns nil, doesn't work

Tried using ascii code for space
Code: [Select]
(_run (strcat "c:\\test" "& chr(32) &""test\\sync.bat")) Returns nil.

Any ideas?

For some reason I can't execute the function like this
Code: [Select]
(_run "c:/tools/sync.bat")
You're right. I can't get it to work either  :oops:  .. perhaps you could use THIS if you're just updating files. I know this one works as I've been using it for years.


It's working here, both types of paths:
Code - Auto/Visual Lisp: [Select]
  1. (defun _run (file / ws)
  2.   (if (and (findfile file) (setq ws (vlax-get-or-create-object "wscript.shell")))
  3.     (progn (vlax-invoke-method ws 'run file 0) file)
  4.   )
  5. )
  6. ;; (_run (setq path (strcat (getenv "userprofile") "\\Desktop\\Test.bat"))) ;;<-Works here
  7. ;; (_run (vl-string-translate "\\" "/" path)) ;;<-Works here

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Executing bat files in current drawing location
« Reply #10 on: October 25, 2017, 06:51:26 PM »
Try:
Code: [Select]
(_run "\"C:\\Test Test\\sync.bat\"")

dubb

  • Swamp Rat
  • Posts: 1105
Re: Executing bat files in current drawing location
« Reply #11 on: October 25, 2017, 07:32:45 PM »
Hmm..didn't think of using slash quotes. However, it didn't work.

Try:
Code: [Select]
(_run "\"C:\\Test Test\\sync.bat\"")

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Executing bat files in current drawing location
« Reply #12 on: October 25, 2017, 07:47:21 PM »
Ok, one more:
Code: [Select]
(_run "\"C:/Test Test/sync.bat\"")

dubb

  • Swamp Rat
  • Posts: 1105
Re: Executing bat files in current drawing location
« Reply #13 on: October 25, 2017, 08:01:51 PM »
I see what you did there..LoL, nope didn't work.

Ok, one more:
Code: [Select]
(_run "\"C:/Test Test/sync.bat\"")

here is my sync.bat file
This works well together
Code: [Select]
(_run "c:\\Test\\mirsup.bat")
Code: [Select]
@pushd %~dp0
cls
robocopy "C:\Test" "C:\Test Test" /MIR /XF *.dwl /XF UpdateLog.txt /XF toolsversion.txt /R:10 /W:5 /ETA /TEE /LOG+:ToolsUpdateLog.txt
@popd

However if I change the sync.bat file to this.
Code: [Select]
robocopy "C:\Test" "C:\Test Test" /MIR /XF *.dwl /XF UpdateLog.txt /XF toolsversion.txt /R:10 /W:5 /ETA /TEE /LOG+:ToolsUpdateLog.txt
It won't work


roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Executing bat files in current drawing location
« Reply #14 on: October 26, 2017, 04:21:14 AM »
Note: If you want to enclose the file name in double quotes you should modify ronjonp's code.
Code - Auto/Visual Lisp: [Select]
  1. ; (_run "C:\\Test Test\\sync.bat") => OK
  2. ; (_run "C:/Test Test/sync.bat") => OK
  3. (defun _run (file / ws)
  4.   (if (and (findfile file) (setq ws (vlax-get-or-create-object "wscript.shell")))
  5.     (progn (vlax-invoke-method ws 'run (strcat "\"" file "\"") 0) file)
  6.   )
  7. )