Monitoring File Changes - command-line

Call CreateObject("WScript.Shell").Run(some.exe,0,False)
I'm using that line to call a .exe that returns some text, but can also write it to a file.
I would use .Exec instead of .Run to get the results directly but then the script hangs.
I really don't want a timer checking if the output file is created or modified.
What I need is a way to catch an event somehow. Any Ideas?

Since you mention that it might either return it or write it to a file, does that mean that after the process has written to the file it'll exit?
If so, you could just call the Shell.Run method with the bWaitOnReturn parameter set to True and your script would wait for the process to finish before it continued.
Otherwise, if the process might write a file and then still continue to run, then I think you will have to poll to check if it exists, or possibly you could create a C# or VB.Net exe that uses FileSystemWatcher (or a normal Win32 exe that uses the API FindFirstChangeNotification) to look for the creation of the file and when it finds one it immediately exists and then you could run that process with bWaitOnReturn set to True, but that's probably just overcomplicating things.

Related

How to add parameters to a running process or exe by default?

Objective:
I'm trying to use a program called Texmod.exe to start another program (exe) with certain parameters.
Background:
Texmod is program that opens up another exe and extracts/replaces textures that are pulled up in the second program while it is running. I'm trying to start the second exe with the parameters: -AlwaysFocus -ControllerOffset=1 -SaveDataId=2. Starting the second exe with shortcuts/.bat that have these parameters in a command line DOES work. However, I would like to do this in conjunction with Texmod.
Complications:
I'm using Texmod v0.9 b which doesn't support starting an exe with command line parameters. Newer versions, such as the open source reboot known as uMod, does support but it doesn't replace textures as well and crashes often compared to the original version.
Texmod.exe must start the second exe directly. Texmod can't select a shortcut to start the exe. I've tried making Texmod start another exe/.bat that then starts the second desired exe with parameters but this causes Texmod not to function.
Methods I have thought about but not sure if they work or even possible:
Somehow forcing all processes started by Texmod.exe to start with certain parameters
Somehow force the desired exe to always start with certain parameters regardless of start method (via Texmod, shortcuts, .bat, etc)
Add parameters to the process started by the desired exe after it is already running
Obviously I'm open to any ideas. Is what I'm asking even possible? Sorry if it seems unclear or I sound irrational; my knowledge on this is limited.
Edit: #Toby Speight I'm not sure what you mean by an example of the code I'm having trouble with as I'm just wondering if it's possible to perform the scenario I described. This is where I got the Texmod program from (it's the oldest release texmod.zip - Original TexMod 0.9 beta). I've searched the internet for possible solutions but I've found very few so some of the methods I've tried are:
Used Texmod.exe to start a desktop shortcut with target: "directory to exe" -AlwaysFocus -ControllerOffset=1 -SaveDataId=2. The result was Texmod couldn't use the shortcut and just opened the exe without the parameters.
Used Texmod to start an exe named caller.exe which started the exe I desired using the method described here. In command.txt, described by the instructions, I put ""name of exe.exe" -AlwaysFocus -ControllerOffset=1 -SaveDataId=2." The result was the exe started with the parameters but Texmod failed to modify the textures because it was modifying textures pulled up by caller.exe instead.
Used Texmod to open a .bat file with the lines: ""name of exe.exe" -AlwaysFocus -ControllerOffset=1 -SaveDataId=2." The result was the same as attempt described in #2.
Edit2: I also stumbled on this page where someone else claims they managed to achieve what sounds like the objective I desire. However, I do not know enough about code to understand it. If someone could look over this to see if it would work or see if they can modify it to fit the arguments that I desire.
You can use windows registry to force Windows run another app when specific .exe is called. Let me show an example: i'm trying to use TexMod with Remember Me.
Required setup:
Create a copy of game .exe, place it near original with different name, eg RememberMe_copy.exe
Go to windows registry location HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options
create new key named as original .exe, in my case it's RememberMe.exe. Registry key looks like a folder.
In this key, create new string value named Debugger with full path to copy of .exe with required commandline args. For example: "C:\Program Files (x86)\Steam\steamapps\common\Remember Me\Binaries\Win32\RememberMe_copy.exe" -ReadPoolSizeFromIni. Note the path to .exe is quoted, commandline arguments are not. String value has type REG_SZ.
Now when you try to run anything called RememberMe.exe, Windows will instead run the RememberMe_copy.exe with specified parameters (and something else but we don't care). This feature is left in Windows for debugging purposes.
Finally, open TexMod, select RememberMe.exe. It will think it's running original file, but instead Windows will silently run another file with another parameters. As there is nothing else in between, TexMod is happy.
Why can't we use single file? Well, Windows will run original .exe instead of original .exe, then instead of it will run original .exe... causing infinite loop.

How to have Autohotkey 'listen' for a change to a file?

I have a text file, lets call it C:\to_run.txt. I'd like my Autohotkey script to 'listen' to this file in such a way that when it detects a change, it performs an action immediately based on the contents of the file, and then make the file blank again.
I can handle the last parts, so really I'm asking for an efficient way to detect file changes? I say efficient because my Autohotkey script is getting rather long and I don't want this listening function to hang up the rest of the script in any way.
Assuming we are really talking of only one file to check on:
Surely not as beautiful as Sidola's answer, but without the need for external libraries:
#persistent
lastFileContent := ""
setTimer, checkFile, 20
return
checkFile:
fileread newFileContent, changingDocument.txt
if(newFileContent != lastFileContent) {
lastFileContent := newFileContent
msgbox, content changed to: %newFileContent%
}
return
In this case, for checking on larger files, it might be better to compare MD5-checksums instead of the whole file content.
Note: I have not tested the performance implications on this. This script opens up the file 50 times per second, could be pretty hard drive consuming.
Check out WatchDirectory()
Just make sure you're running the latest version of AHK.
To get it up and running, first download these three scripts and save them to your /lib folder.
WatchDirectory.ahk
_Struct.ahk
sizeof.ahk
After that, simply point to to wherever you want to look, provide a callback function and if you want, a third param to watch for specific changes. Refer to this forum post for full documentation.
If you drop this script in its own folder, save it and run it, then save it again, it should detect changes to that script file.
#Persistent
WatchDirectory(A_ScriptDir "\|.ahk\", "Callback", 0x10)
return
Callback(param1, param2) {
msgBox % param1 "`n" param2
}
Note however, it will fire twice whenever the file is changed. This seems to be a Windows behaviour from what I can gather.

Matlab Script Called Before Each Run

I have some init commands in a m-file on disk. Depending on where the user is executing from, the contents of that init file will will change.
I'm wondering if there is a way to have Matlab automatically call this init script every time a script begins execution (either via the Run key or by requesting a call to a function in the interpreter).
I've looked at the startup.m script and that won't work as it is only executed when Matlab is first started but I want something that runs each time a script begins execution.
I've also thought about placing some boilerplate code at the top of the script file, but, that doesn't work in cell mode as the script may start execution part way through the script which would skip the boilerplate code.

How to run multiple Scripts one by one in a powershell script

I have 8 scripts in Powershell which I run one by one. Let's call the scripts: script1.bat, script2.bat, .., script8.bat.
Now I need a script which runs all scripts.bat one by one, but not simultaneously.
And is there a way to check, if each script was successful?
./script1.bat
./script2.bat
./script3.bat
...
You'll get the picture, I guess. This will run them in sequence. To determine whether they were sucessful or not that depends very much on how those batch files signal errors or sucessful completion. If you exit with exit /b 234 or something similar on an error then you can use $LastExitCode or also $? to determine that. You could also look whether the changes made by those batch files are actually done, of there is no other way of figuring out whether they were sucessful.

.bat script only executes 1 line

I'm writing a script that performs the same function several times, but when I run the script only one of commands executes leaving the rest not executed after the .bat file has run.
Does this have to do with the long time it takes for my commands to run (15-20 sec)? I've written plenty of bat files and I've never run into this. Do I need to have a sleep function between each command?
I've been trying to figure this one out on google, but my available search terms makes my search results vague and difficult.
Any help is definitely appreciated.
the bat file looks something like the following
IF input1 == "search term" goto location
do something
do something
do something
etc
goto end of file
:location
do something else
do something else
do something else
...
Does one of your "do something else" lines involve calling another batch file? If so, do you use the CALL command?
If you want to call another batch file recursively, you need to use CALL. Otherwise, when the called batch file exits, it does not return to the calling batch file and simply exits. This is a relic from the MS-DOS days; since memory was at a premium, the MS developers decided that the batch interpreter shouldn't keep a call stack by default -- so if you wanted one, you had to use CALL.
See call /? for more information.