• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Deleting a file in use

A

Ashish

#1
Is there any way in powershell to delete a file which is in use?
I tried move and delete and that didnt work.. Am looking at something in Win
API like MOVEFILE_DELAY_UNTIL_REBOOT
 

My Computer

R

RichS

#2
If the file is locked open you won't be able to delete it from PowerShell
during that session
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Ashish" wrote:

> Is there any way in powershell to delete a file which is in use?
> I tried move and delete and that didnt work.. Am looking at something in Win
> API like MOVEFILE_DELAY_UNTIL_REBOOT
 

My Computer

J

Jason

#3

My Computer

A

Ashish

#5
i created a Pinvoke code which calls MoveFileEx API for the same...seems like
it isnt happy taking values of long type. any ideas what may have gone wrong?


$domain = [AppDomain]::CurrentDomain
$name = New-Object Reflection.AssemblyName 'TestAssembly'
$assembly = $domain.DefineDynamicAssembly($name, 'Run')
$module = $assembly.DefineDynamicModule('TestModule')
$type = $module.DefineType('TestType')


[Type[]]$parameterTypes = [string], [string], [int64]
$method = $type.DefineMethod("MoveFileeX", 'Public,Static,PinvokeImpl',
[bool], $parameterTypes)

$ctor = [Runtime.InteropServices.DllImportAttribute].GetConstructor([string])
$attr = New-Object Reflection.Emit.CustomAttributeBuilder $ctor, 'kernel32'
$method.SetCustomAttribute($attr)


$realType = $type.CreateType()
[object[]]$args = [string]'F:\\dll\\abc.dll', [string]$null, [int64]4
$realType.InvokeMember('MoveFileeX', 'Public,Static,InvokeMethod',
$null,$null, $args)

"RichS" wrote:

> If the file is locked open you won't be able to delete it from PowerShell
> during that session
> --
> Richard Siddaway
> Please note that all scripts are supplied "as is" and with no warranty
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
>
>
> "Ashish" wrote:
>
> > Is there any way in powershell to delete a file which is in use?
> > I tried move and delete and that didnt work.. Am looking at something in Win
> > API like MOVEFILE_DELAY_UNTIL_REBOOT
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)