How do I delete a folder that another process has open? - delphi

I created two services. I want to delete a folder, but that folder is used by my first service. When I execute the first service after that I execute the second service it works fine. But when I try to execute both service at the same time it does not work properly.

Actually, the question should have been "What in the application could prevent folder from deletion by other applications".
Possibilities are:
Your service opens some file in that folder and does not close it. Check all files that you are opening in the service, and close the ones which are in that folder.
How files are closed depends on how they were opened. If you used CreateFile(), then close with CloseHandle(). If it was TFileStream, then just Destroy it.
Your service has that folder set as the current directory. Choose other directory as a current with SetCurrentDir.

you cannot delete it unless, you can tell the other service by sending message to stop using the folder(or its content) before deleting it.

If first service is launched before second service then you can delete folder but if both services are launched together then you can't delete folder.
Only difference is you allowed time for first service to finish working with the folder.
Things to consider:
If your first service is using the folder then you won't be able to delete it until your service closes connection with that folder like already mentioned.

Related

SSIS File System Task moving file to another folder using UNC Path

In my new SSIS package I have tried using both File System task and Script task to move a file to child ("DONE") folder once the file has been processed and I get an error saying "Could not find part of the path" I set the path to variable 100% the path exists so what I am doing wrong.
The file is being processed by a batchfile that loads the file thru a 3rd party system into an SQL server database. If the file was locked it would surely say locking error and the 3rd party system would not show successful import.
I just fiddled around with my package tried to run it locally but disabled the batch call, (this was running the script so one line call File.Move (Source, Destination) where I changed the destination to not include the filename so the path only) and initially it was circling in an infinite loop. So I stopped it and ran a few more times and every time post the first run it was throwing a Target of Invocation error whatever that is. Then I changed it back to File System Task and now it worked locally. I deployed to the server it worked also. I reenabled the batch call redeployed and it worked on the server go figure. So I have no idea what I really did for it not to work. Moral of the story step away and come back don't trust yourself and reconfigure.
Now that I can put it down to mostly likely a human error or maybe a bug in SSIS that you do something else as in reconfigure it now starts to work or was it simply removing the filename from the destination path works for File System task only I will never know but I will probably delete this post as I don't think it adds anything useful.

How to deploy client-side service WSDLs

I have code using the SysOperationFrameworkService, and after a model-deploy or some other set of circumstances, many users get Function SysOperationDataContractInfo::newParameterInfo has been incorrectly called..
It's unable to be resolved with full-compile, sync, full CIL, deleting XPPIL files, deleting AUC/KTI, usage data, security (they're admin), refreshing caches (server &client), etc. and I have a ticket open with Microsoft and they're struggling.
I've narrowed the issue down to when the service group AxClient located in the AOT at \Service Groups\AxClient WSDL files get deployed to C:\Users\[User]\AppData\Local. Either those files existing there, or something happening when those files get deployed is needed.
Any idea how/what causes these files (AxClient.wsdl, etc) to be created? With a user who happens to be "working", I can close the client, delete all the files, open the client, and no files will exist...then I perform a SysOp process and those files get created during that process. With the non-working user, with same security on the same machine does it, the files don't get created.
I have found that the
"Function SysOperationDataContractInfo::newParameterInfo has been incorrectly called" has been throwed because of a possible bug in the kernel when calling the "dictMethod = new DictMethod(UtilElementType::ClassInstanceMethod, dictClass.id(), methodName);" in the SysOperationServiceController.getServiceOperation() method. In my case the new DictMethod was sometimes unable to get the method parameter from a superclass, causing the _parentMethodParameterName check fail in the SysOperationDataContractInfo.newParameterInfo.
So, I solved the issue just by doing an override of the super method where the parameter could not be retrieved and then just calling super(_parameter), then everything went well.
Have you tried axclicfg.exe -> connections -> refresh

AttachFile locks attachment for deleting on Windows XP

I'd been working on a plugin when I discovered this. I can't say for sure if this behavior happened before or not on my machine (it doesn't on our test server, a Linux box), but after attaching a file, I can't delete it until the server restarts. I can't delete through the UI or by manually navigating to the server directory and trying to delete from there.
Has anyone ever encountered this before? Could it be something environmental on my box??
Most probably it's a permission issue in that folder, which allows your JIRA user (a user under which privileges JIRA instance runs) to create files, but not to delete them (or something even more fun) :) Try deleting the temp folder (where your uploaded attachments reside) and recreate it again, adding your JIRA web user to the access list for that folder.
The workaround to delete files, when some other process is holding a lock on the file, without having to terminate that process, is to use Unlocker. But be warned, when Unlocker unlocks a file it does that in a way which does not notify the lock holder that the file has been unlocked by force. That means that the lock holder still thinks it holds the lock on the open file which it doesn't (the file handle is invalid). That means that some applications might crash due to unexpected state of the supposedly open file. Btw, I've been using Unlocker since forever and it rarely caused any crashes, but it's better to be warned.

Updating EXE file from server…

I need to update my application from a central server.
The application checks always if it is a correct version, against the server installation.
So when it is not, I need it to update itself.
So how can I copy the EXE if it is running? What solution do I have?
I rename the current running exe to MyTempExe.exe, copy the new exe to the correct location (request elevated privileges if necessary) and then run a separate app to restart the main app. On start up I check for MyTempExe.exe delete it if it's there.
The reason I use a separate app for the restart is I don't have a set time frame for the app to close down and need to wait for it to finish whatever it's doing, on shutdown it writes information to disk about its current state that the updated app will use to resume where the old one left off.
I don't know if it's the best solution but it's the one I use.
As you can see by all the answers there is no set way to do this, so I thought I would add the way we have successfully done this.
We never run an application directly from the network.
We run the application from the local machine and have it copy from the network on startup.
We do this using an application launcher. It downloads an XML file that contains CRC and Version Resource Values for the application files. The XML File is created during the deployment process, in a FinalBuilder Script.
The application then compares the XML File to local content, and copies down needed files. Finally we then launch the application in question. This has worked well for deploying an application that serves around 300 local users. Recently we switch from a file copy to an HTTP download as we found problems with remote user disconnecting drives.
We still still build installations (With Innosetup) to get the basic required files deployed.
Package your app with an installer such as Inno. Download and execute the installer. Have the installer search for and kill your app, or instruct the user to close it. The setup will replace your .exe, and if the app can't be killed or the user is non-cooperative, it'll issue a re-start notice.
Download new EXE to TEMP
Create Batch from EXE, content:
taskkill /PID %process id of running EXE%
copy %new EXE% %running EXE%
%EXE%
all values in %...% are placeholders
execute batch from the running EXE
delete batch
I use TMS TWebUpdate myself, for software updates. The advantage is that there a bunch of extra actions you can put into the script, if you need anything other than plain EXE updates.
I have two components at work the application executable itself and a web-service (SOAP) which provides version details and file downloads.
The application calls a method on the SOAP service to ask for the number of files in the project (project is identified by using the application.exename usually).
The soap service gets its info from an INI file, which has entries like:
[ProjectName]
NumberOfFiles=2
File1=myapp.exe;1.0.0.1
File2=mydll.dll;1.0.0.2
You just update this file at the same time as uploading your new files.
The process of updating the application this:
Get number of files available on the web service
For each file, the application asks for the name and version number from the SOAP server.
The application compares this information to its own version info and decides if the file needs updating, building a local list of files that need updating.
For each file that needs updating the application downloads the file to filename.ext.new
Finally, the application renames all filename.ext to filename.ext.old and renames filename.ext.new to filename.ext and then restarts itself. (No real need for an external app to restart your own program).
Note 1, that you may have to ask for elevation to replace files, depending on where you install your files.
Note 2: be kind to your users, think carefully before you force updates on users.
Note 3: You cannot delete a running exe, but you can rename it and then restart the new version.
Edit===
For some reference data files which cannot contain version information resources, you can have entires like File99=MyDataFile;1.1.2011 the 3 elements to the version number indicates to the client that it should check against the file date/stamp.
You could have a separate update executable whose task is to check the server version, download an updated executable if necessary, and then run the local executable.
Or you could have one executable running in two different modes: 1. on startup, check for an update, if there is one, download the executable to a download directory, run it and quit.
2. The new executable would check if it's running from the installation directory, if not, it would copy itself there, overwriting the old version, start the copy from there, and quit.
My way is the other way round: If a new version is online, promt the user to update. If he want's to (or is forced to...) I end the app and start a new exe (updater). this updater loads the update and replaces the old exe (not running). then it starts the new exe. ready. (You can of course replace other files too.) BUT: Using an Installer like InnoSetup gives you more possibilities and doesn't mix up with the regular uninstaller, so it is really better...
You can do this without running another application. Push the updates to the client from the server while running, storing in a temporary directory on the client. When you want to upgrade move all your running files to another temporary directory, move the new files into the original application directory, and just restart the application using the standard executable name on shutdown.
I upgrade client applications running on unattended machines automatically this way.

(SC) DeleteService FAILED 1072

Last time I create WAS profile and WASService then I try to config and run many script for learn how to config WAS, Finally it crash so i use wasprofile delete this profile and forgot delete WASService.
Now I found IBM Webphere Application Server service display in services.msc list, so I tried to delete it with WASService.exe -remove command and windows SC command but I got message
C:\Program Files\IBM\WebSphere\AppServer\bin>sc delete "IBMWAS61Service - DEV"
[SC] DeleteService FAILED 1072:
The specified service has been marked for deletion.
make sure the service is stopped, the services control panel is closed, and no open file handles are open by the service.
Also make sure ProcessExplorer is not running.
I had a similar problem and what I did to overcome it was the following:
Stop the service: net stop "ServiceName"
Ensure: the "mmc.exe" process does not exist (The "Services" list window): taskkill /F /IM mmc.exe
Delete the service: sc delete "ServiceName"
C:\server>sc delete "ServiceName"
[SC] DeleteService SUCCESS
Now, if I execute another sc command, what I get is the following:
C:\server>sc delete "ServiceName"
[SC] OpenService FAILED 1060:
The specified service does not exist as an installed service.
But not the 1072 error message
What I've done is go to this location in regedit:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
From here, you will see a folder for every service on your machine. Simply delete the folder for the service you wish, and you're done.
N.B: Stop the service before you try this.
For some buggy reason both Event Viewer and/or Services.msc won't do a proper refresh when you tell them to!
Close them and restart, and the service would have been deleted anyway.
I had the same issue. After I closing and re-opening the Computer Management window the service was removed from the list. I'm running windows 7
In Windows 7, make sure Event Viewer closed before deleting.
I had this error also, make sure the exe the service is pointing to is stopped. Also make sure you don't have any Windows dialog boxes behind your other windows. That is why mine wasn't deleting. There was a windows message behind it saying this service has been deleted or something similar.. just had to click ok, there it went.
I had the same error due to a typo in the service name, i was trying to delete the service display name instead of the service name.
Once I used the right service name it worked fine
Logging-out and logging-in again close all blocking apps thus resolves the problem.
The 3rd party application uninstaller had removed the files for the service and then left the service in this pending deletion state.
After trying to close all applications, identifing PID of service(couldn't) for kill, logging off all other users and logging off and on, rebooting was the only fix that worked for me.
One situation where this can also happen is if there is some other service or application that is holding open a service handle obtained with OpenService. For example, a monitoring service that starts and stops services based on some external event can keep open handles to each of the services it monitors. In this case, uninstalling the service would leave it in the "marked for deletion" state until all handles obtained with OpenService are closed.

Resources