Page 1 of 1
Batch that automatically opens a file
Posted: 20 Aug 2010 02:58
by dally165
Hi guys,
I am in need of a batch script to run in the background that automatically opens a file in that particular folder as soon as it shows up. e.g. when I copy/move a file in that folder, a 3rd party opens that file automatically.. I want a batch script to do that for me..
Constraints:
-I want the script to open whatever files that gets put in in that one folder
-I dont want the script to open the file the way it is but rather I want a 3rd party program to open that for me.. (I want Borland C++ to debug that file so the constraint will be 'Open file with borland C++' ?????)
-I dont want the script to end once executed unless if I choose to close it
Re: Batch that automatically opens a file
Posted: 20 Aug 2010 07:29
by aGerman
Hmm. I don't use Borland, so I don't know if this would work. You should search the internet for the command line options. The line I posted below is found
here.
Code: Select all
@echo off &setlocal
:mainloop
for %%a in (*.cpp) do (
set "file=%%~fa"
call :borland
ping -n 2 localhost>nul
)
goto mainloop
:borland
call "C:\Borland\bcc55\Bin\bcc32.exe" -w-par -tW -tWM -v -q -lv "%file%"
:loop2
set "choice="
set /p "choice=Loop again / Close [L/C]: "
if /i "%choice%"=="C" exit
if /i "%choice%"=="L" goto :eof
goto loop2
Place the batch file into the same folder where you plan to copy/move the .cpp files.
Regards
aGerman
Re: Batch that automatically opens a file
Posted: 22 Aug 2010 01:43
by dally165
Hi aGerman, thank you so much for that.. it's so close to what I'm looking for.. the code as it is always prompts me to either go back to the loop or close the batch file.. I wanted it to keep looping so what I did was I deleted that prompting bit so now it just keeps looping..
I'm just testing it using notepad++ to open the file and basically the script just keeps opening the file and reopening it and opening it.. (due to the loop)
I was wondering if you could add a constraint to it such that the script will ignore the file that it has already opened?
Re: Batch that automatically opens a file
Posted: 22 Aug 2010 01:47
by dally165
Also it will be cool if I can work the script outside the target folder..
Re: Batch that automatically opens a file
Posted: 22 Aug 2010 03:16
by orange_batch
I started to write a solution for you a few days ago but I didn't feel like it given the moderate complexity and my time.
I'll tell you an example of what the best solution likely is though. As expected, this will require more and more CPU for processing every second (or however many seconds, easily changable), the larger the contents of the folder.
-Initialize a variable notnew=false, and a blank "array" by creating variables previouslength=1 and previous1="nul".
:loop label goes here
-Wait 1 second (or however many seconds you want between each check).
-Process the target folder and store the paths of all it's contents into an "array" called current. (Make sure to include a counter called currentlength (set /a currentlength+=1).)
-(main for /l loop using %currentlength%) For each entry in the current array,
(nested for /l loop using %previouslength%) compare it to each entry in the previous array. If a match is found, set notnew=true.
(end nested loop)
-If notnew=false, open current file, else set notnew=false.
(end main loop)
-Overwrite the previous array and length with the current array and length. goto loop
Re: Batch that automatically opens a file
Posted: 22 Aug 2010 04:15
by dally165
Thx for that.. I'll give it a try but will most likely come back for help.. im quite new to batch scripting...
Re: Batch that automatically opens a file
Posted: 22 Aug 2010 07:04
by orange_batch
Perhaps I'll just write it for you after all.
It's not difficult, just required that thinking which I did above, so what the heck.
Btw I changed a couple things for convenience. Shouldn't have any glaring bugs. It also has nice status messages.
Code: Select all
@echo off&echo:&color B&title New File Opener by orange_batch&setlocal enabledelayedexpansion
set "monitor=C:\Your Folder Path"
set prevlength=1
set "prev1=nul"
set "new=true"
echo: ^> Monitoring: ... %monitor:~-60%
echo:
:loop
:: -n 2 is wait 1 second, -n 3 is wait 2 seconds, -n 4 is ... etc.
ping -n 3 -i 1 127.0.0.1>nul
:: Gather current files' paths.
for /r "%monitor%" %%x in (*) do (
set /a currlength+=1
set "curr!currlength!=%%x"
)
:: Compare each current file's path to each previous file's path. If a match is not found, then open the file.
for /l %%x in (1,1,%currlength%) do (
for /l %%y in (1,1,%prevlength%) do (
if "!curr%%x!"=="!prev%%y!" set "new=false"
)
if "!new!"=="true" (start "" notepad "!curr%%x!"&echo: Opened: ... !curr%%x:~-64!&echo:) else set "new=true"
)
:: Copy current file paths over previous file paths and loop.
for /l %%x in (1,1,%currlength%) do (
set "prev%%x=!curr%%x!"
)
set prevlength=%currlength%
set currlength=
goto loop
Options are:
set "monitor=C:\Your Folder Path" <- Self-explanatory.
ping -n 3 -i 1 127.0.0.1>nul <- As in the comment, -n 2 is check every second, -n 3 is check every 2 seconds, etc...
(start "" notepad "!curr%%x!"&echo: Opened: ... !curr%%x:~-64!&echo:)
The "" sets a title (in this case, blank, which is fine). It prevents some kind of problem according to
http://www.ss64.com. I'm not sure what, but whatever. Following that, it's just like regular command line, you can type the path to Borland or whatever in quotes with arguments etc, and the file to open ( !curr%%x! ), however you wish. The rest of the line is the "Opened:" status message.
Re: Batch that automatically opens a file
Posted: 26 Aug 2010 13:09
by alan_b
I think a much simpler solution would be that after the file has been processed, it should then be moved to a different folder named, for example "processed", before looping back and looking for something to process.
Alan