3

On a computer I'm not an administrator on, there is a startup file (.cmd file in C:\Documents and Settings\All Users\Start Menu\Programs\Startup) for all users. It does several things I don't like, so I have created my own .cmd file which I have placed in C:\Documents and Settings\[user]\Start Menu\Programs\Startup.

I want my code to run after the system wide, because my program first undoes the network mappings the system wide file did, and then replaces it with my own network mappings. How can I make my program wait and start as soon as the system wide one is done? (I cannot remove or edit the system wide file).

Thanks

EDIT: The time that the system wide file takes to runs varies and I would like my file to run right after. The "If Exists" method seems a little to contingent since the system wide script can change or a file could be moved. I am hoping to give my script to a few of my coworkers, so hoping to have it work without me having to update anything. Also, being a linux guy, I don't know cmd code, so please write out any coding suggestions.

6 Answers 6

2
+100

What your trying to do is a little difficult if you don't have access to the "All Users" start up .bat file if you did all you would have to do is edit it so that after all commands were executed you would

"call Mycoolbatchfile.bat" 

As this is not an option my solution would be to code the following in AutoIT

Global $SystemWideScript = "Admin.bat"

Func MountNetworkDrv($drv,$path)
    ShellExecute(@SystemDir & "\net.exe ","use " & $drv & " " & $path,"","",@SW_HIDE)
EndFunc

While 1
    For $i = 1 To 100

        ;Wait for the Batch script to finish executing
If ProcessExists($SystemWideScript) = 0 Then

    ;The system wide "All Users" batch script is not running so we can execute
MsgBox(0,"Custom Network Drives","I Cannot detect " & $SystemWideScript & " running so i'm executing")

MountNetworkDrv("G:","\\192.168.1.2\logs")


MountNetworkDrv("Y:","\\Server\CoolStuff")

Exit
EndIf
Next
Sleep(2000)
Wend

This is just my view on the issue (one of many).

5

I would use the Task Scheduler (Start -> All Programs -> Accessories -> System Tools) if you have Windows Vista / Windows 7. You can tell Windows to run your .cmd a specified amount of time after login, such as 30 seconds.

2
  • 1
    I definitely recommend this approach as well.
    – Joshua
    Jun 7, 2010 at 14:36
  • @Dan If you need it to run immediately after the global script, the only way to know when it exits is to know the process name and watch for it's exit (which you can do from the task scheduler, trigger / on event / application / process exit), otherwise simply waiting a maximal amount of time will work regardless of the global scripts's name. Jun 8, 2010 at 9:08
1

as you mentioned that you want to wait until the network mappings. @Mike Fitzpatrick is correct.

:start
REM check if the filesystem has been mounted
IF EXIST L:\somefile GOTO okay
PING -n 2 127.0.0.1 > nul
goto start
:okay
REM DO EVIL STUFF HERE

Note that ping -n 2 127.0.0.1 > nul is for waiting some time in windows.

3
  • 1
    note: the waiting time is to prevent constant looping of batch file
    – bubu
    Jun 7, 2010 at 14:29
  • I'm still worried about the system startup file changing, which could nullify my script... but if there isn't a better alternative... Is there a way to make this have a catch that >15 seconds will automatically go to :okay? Also, can I do this on a folder instead of a file?
    – Jarvin
    Jun 7, 2010 at 14:45
  • just change -n 2 to -n 15, then remove goto start i think you need to do that on a file
    – bubu
    Jun 7, 2010 at 15:23
1

I realize you're looking for a technical solution, and there are several great recommendations above, but first, you might want to consider just talking to your IT department and seeing if they could adjust things to not annoy you in the first place, particularly if other coworkers want this changed, too. I imagine they might not take lightly to you undoing on your own the settings they're implementing.

If you do however decide to forge ahead with this, one possibility would be to put a shortcut to it in the start menu's startup folder. Items in that folder do not run until Explorer starts, and usually explorer doesn't start until after startup scripts have run.

Another method would be to just have sleep 10 at the beginning of your batch file. The sleep command isn't built into windows, but can be downloaded as part of the windows server administration tools.

1

Do an if exist on some file you know exists on the last mapped drive that you don't want mapped, loop (with a few seconds pause) until it does exist, then do the rest of your script.

Edit:

IF EXIST works fine with just a root folder so you can see if a drive letter is mounted without having to worry about a particular file being on that drive. (Less contingent)

I like the @bubu's use of ping to introduce the delay but @nhinkle's suggestion to install sleep is cleaner.

Based on @bubu's code:

REM optional initial sleep
sleep 15
:start
REM check if the filesystem has been mounted
IF EXIST L:\ GOTO okay
sleep 5
goto start
:okay
REM DO EVIL STUFF HERE
1
  • You can see that i am one of the bastard operators from hell. I agree though, that using "sleep" is neater.
    – bubu
    Jun 8, 2010 at 17:30
0

You can create batch file like this:

@echo off
echo Press X to continue, just waiting 20 seconds finish loading up of your Pc
choice /c XZ /n /t 20 /d Z
if %errorlevel% equ 1 goto evil
if %errorlevel% equ 2 goto evil
:evil
REM EVIL STUFF HERE

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .