TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: dubb on October 20, 2017, 06:56:41 PM

Title: Executing bat files in current drawing location
Post by: dubb 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?
Title: Re: Executing bat files in current drawing location
Post by: ChrisCarlson on October 23, 2017, 02:42:05 PM
Does the batch file run independently from AutoCAD?
Title: Re: Executing bat files in current drawing location
Post by: JohnK 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
Title: Re: Executing bat files in current drawing location
Post by: dubb 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?")
Title: Re: Executing bat files in current drawing location
Post by: ronjonp 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 (https://www.theswamp.org/index.php?topic=53550.msg582650#msg582650).
Title: Re: Executing bat files in current drawing location
Post by: dubb 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"))
Title: Re: Executing bat files in current drawing location
Post by: dubb 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")
Title: Re: Executing bat files in current drawing location
Post by: ronjonp on October 24, 2017, 09:19:56 PM
I'll have to take a look in the morning.
Title: Re: Executing bat files in current drawing location
Post by: ronjonp 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 (https://www.theswamp.org/index.php?topic=39415.msg446782#msg446782) 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
Title: Re: Executing bat files in current drawing location
Post by: dubb 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 (https://www.theswamp.org/index.php?topic=39415.msg446782#msg446782) 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
Title: Re: Executing bat files in current drawing location
Post by: Lee Mac on October 25, 2017, 06:51:26 PM
Try:
Code: [Select]
(_run "\"C:\\Test Test\\sync.bat\"")
Title: Re: Executing bat files in current drawing location
Post by: dubb 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\"")
Title: Re: Executing bat files in current drawing location
Post by: Lee Mac on October 25, 2017, 07:47:21 PM
Ok, one more:
Code: [Select]
(_run "\"C:/Test Test/sync.bat\"")
Title: Re: Executing bat files in current drawing location
Post by: dubb 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

Title: Re: Executing bat files in current drawing location
Post by: roy_043 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. )
Title: Re: Executing bat files in current drawing location
Post by: Grrr1337 on October 26, 2017, 05:16:09 AM
If left with no options, perhaps try to copy the file with fso and then run it with shell ?
BTW vlax-release-object.
Title: Re: Executing bat files in current drawing location
Post by: ronjonp on October 26, 2017, 09:13:22 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. )
This works for me too. Updated my code HERE (https://www.theswamp.org/index.php?topic=53550.msg582650#msg582650).
Title: Re: Executing bat files in current drawing location
Post by: JohnK on October 26, 2017, 11:04:45 AM
I don't see the difference between the two "sync.bat" scripts. Is this "robocopy" program your problem or is AutoLisp (...why would you use robocopy to begin with you may as well be using xcopy?)? I would suggest looking into Unison and simplifying a bit.

Also, wouldn't just setting up a "scheduled task" in windows be easier?

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
Title: Re: Executing bat files in current drawing location
Post by: MP on October 26, 2017, 11:15:03 AM
There's lot's of compelling reasons to use robocopy John.

Agree that task scheduling (or folder watching triggered events) may be a good strategy.
Title: Re: Executing bat files in current drawing location
Post by: dubb on October 26, 2017, 11:16:20 AM
Thanks! I really need to learn more about vlax functions. This worked perfectly. Thanks to all who helped.
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. )
Title: Re: Executing bat files in current drawing location
Post by: dubb on October 26, 2017, 11:21:41 AM
@John, I was kinda set with using Robocopy because I liked how easy it was to use. Though Ron's xcopy solution might work. If I find the time to tweak it a little to fit my needs. I will be giving it a try.
Title: Re: Executing bat files in current drawing location
Post by: JohnK on October 26, 2017, 11:32:43 AM
There's lot's of compelling reasons to use robocopy John.

Agree that task scheduling (or folder watching triggered events) may be a good strategy.

Ok. Would like to help but don't really know how; because it sounds like the problem, to me, is over complication or this robocopy program (arguments or something). I looked at the Wikipedia page for it and it seems like a pretty basic copy program. ...I mean the only reason I ask `why use robocopy' is because it sounds like the OP wants to preform a sync type operation and I just don't understand why you wouldn't want to use a sync type program (why make syncing more difficult then it already is). -i.e. something like Unison or Rsync will give you all of the features xcopy/robocopy will give you, in addition, better detection and better efficiently (both result in faster syncs) and you can create a simple script which can be called with scheduled tasks.

Title: Re: Executing bat files in current drawing location
Post by: JohnK on October 26, 2017, 11:36:07 AM
@John, I was kinda set with using Robocopy because I liked how easy it was to use. Though Ron's xcopy solution might work. If I find the time to tweak it a little to fit my needs. I will be giving it a try.

That doesn't look simple at all!
"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's really no big deal to me what you use but personally I keep things simple as possible. I don't like strings like that because I forget what all of those switches mean and cannot fix problems later.
Title: Re: Executing bat files in current drawing location
Post by: dubb on October 26, 2017, 11:47:27 AM
I would love to use Rsync, I haven't used it for a while since I don't have my linux server. How I decided to use robocopy instead of xcopy; I searched how to synchronize files using dos and autolisp and robocopy seemed easy and had some of the features that seemed to fit my needs however, its not perfect and I am receiving feedback on how it performs. Though I would like to use Rsync or Unison, I also want to limit the amount of third party application distributed to other workstations. Not sure if I have to install Rsync or unison on each work station individually so I am hesitant to take the responsibility in ensuring every third party application is installed. I create several tools with autolisp for my office as a head designer for my office. To deploy these tools a new workstation would require an installation of autocad and the customized profile.
Title: Re: Executing bat files in current drawing location
Post by: dubb on October 26, 2017, 11:53:46 AM
Thanks I value your input.

Quote

That doesn't look simple at all!
"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's really no big deal to me what you use but personally I keep things simple as possible. I don't like strings like that because I forget what all of those switches mean and cannot fix problems later.
Title: Re: Executing bat files in current drawing location
Post by: JohnK on October 26, 2017, 12:08:15 PM
Alright, humor me. Try Unison. Rsync will require you to get cygin dlls and stuff (too complicated for this post). I jotted down some steps for you to take/try out unison with. I really don't have a lot of time (I have a 90% going out in a few weeks and I need to do a lot of things before then) but these instructions should get you going.

Unison doesn't require an install; its just a binary that would need to be copied to everyone's hard drive (that would be a good job for xcopy or robocopy). You can create an installer for it if you wish but not really necessary.

Steps:
1. Get the windows binary.

[ https://www.irif.fr/~vouillon/unison/ ]

2. Unpack and place the binary somewhere.
3. Update your windows PATH enviorment variable to include where that binary is. (optional; hard-code paths in step 8 if no env variable)
4. Open text editor and create a file in the following directory:
 [ C:\Users\<username>\.unison\ ]
5. Name the file something usefull like: "sync_Documents-folder.prf"
6. Populate the file with the following (adjust per your needs):

Code: [Select]
# Unison preferences file
# This file will preform a sync on "my documents" folder to my USB stick
#
# Last Updated: 01.01.99

#---[ Root ]------------------------------------------------------------
root = C:/Users/<username>/
root = D:/

#---[ Paths ]-----------------------------------------------------------
# Paths to synchronize
path = Documents

#---[ Options ]---------------------------------------------------------
batch = true
# confirmbigdel = false

#---[ Ignore ]----------------------------------------------------------

ignore = Path */Temp

ignore = Name *.tmp
ignore = Name *.example
ignore = Name *.zip

7. Create another file somewhere on your desktop called "Sync Documents to USB.cmd"
8. Populate it with the following code:

Code: [Select]
@echo off
call <path>\<to>\<unison>\unison.exe sync_Documents-folder

pause

9. Plug in USB stick.
10. Double click the "Sync Documents to USB.cmd" on your desktop.
Title: Re: Executing bat files in current drawing location
Post by: dubb on October 26, 2017, 12:16:24 PM
Got it, will report on this within the next few business days. Thanks!
Title: Re: Executing bat files in current drawing location
Post by: JohnK on October 26, 2017, 12:21:38 PM
Sounds good. I'd recommend trying out different folders on your hard drive with different size files. -i.e. Try out a bunch of small files and see how long it takes to sync a few folders like that. You'll be surprised at how fast it is and how you can't really foul anything up. 

Good luck.
Title: Re: Executing bat files in current drawing location
Post by: ronjonp on October 26, 2017, 04:20:28 PM
... Though Ron's xcopy solution might work. If I find the time to tweak it a little to fit my needs. I will be giving it a try.
I added some code HERE (https://www.theswamp.org/index.php?topic=39415.msg582702#msg582702) to run RoboCopy out of AutoCAD .. give it a try! :) 

Use like so:
Code - Auto/Visual Lisp: [Select]
  1. (rjp-robocopy
  2.   "C:\\Test"
  3.   "C:\\Test Test"
  4.   nil
  5.   "/MIR /XF *.dwl /XF UpdateLog.txt /XF toolsversion.txt /R:10 /W:5 /ETA /TEE /LOG+:ToolsUpdateLog.txt"
  6. )
Title: Re: Executing bat files in current drawing location
Post by: dubb on October 26, 2017, 07:34:43 PM
Sweet! Looks great! Thanks Ron.

... Though Ron's xcopy solution might work. If I find the time to tweak it a little to fit my needs. I will be giving it a try.
I added some code HERE (https://www.theswamp.org/index.php?topic=39415.msg582702#msg582702) to run RoboCopy out of AutoCAD .. give it a try! :) 

Use like so:
Code - Auto/Visual Lisp: [Select]
  1. (rjp-robocopy
  2.   "C:\\Test"
  3.   "C:\\Test Test"
  4.   nil
  5.   "/MIR /XF *.dwl /XF UpdateLog.txt /XF toolsversion.txt /R:10 /W:5 /ETA /TEE /LOG+:ToolsUpdateLog.txt"
  6. )