tags:

views:

72

answers:

4

Hi

I am facing error when i try to delete a file using a batch file. For example say the file i want to delete is "C:\test\a.dll"

i get the folder "c:\test" from registry and then i try to append the file name using and delete it using the following command

del /s %WPINSTDIR%\a.dll

where i get WPINSTDIR from registry and it would be "C:\test"

however when i try to run the batch file i get a error saying network path found and this is the command that is executed. del /s "c:\test"\a.dll

By setting a environment path variable i found that the problem is with the 2 slashes in "c:\test" and the quotes. Anyway to get around this problem.

Thanks

A: 

This might do:

set current=%CD%
CD /d %WPINSTDIR%
DEL /s a.dll
CD /d %current%

EDIT
Edited to use CD /d and the "%CD%-trick".

Thorsten Dittmar
Works only if the current drive is equal to the drive in %WPINSTDIR%.
Frank Bollack
Hm. True. Didn't consider that...
Thorsten Dittmar
Frank: You can use `cd /d` then.
Joey
well i tried that and it does work but since my batch file is a part of a patch after deleting the file i need to go back to the original drive where the files were temporarily copied for the patch to execute and this path is selected by user i cant go ahead with this solution. ANy other means?
viswanathan
You can use `pushd` then to change the directory instead and after deleting `popd` to restore the original directory.
Joey
Or you can use the %CD% trick above.
Thorsten Dittmar
Meh, poor man's clone of `pushd`/`popd`. I'd use those since it's clearer what they do. And they work even without command extensions.
Joey
Didn't know whether `pushd/popd` actually supported changing the drive then `cd`ing. Now I know ;-)
Thorsten Dittmar
You can even `pushd` UNC paths, when command extensions are enabled (which is the default anyway) :-)
Joey
A: 

You can remove quotes around your environment variable with the following:

%WPINSTDIR:"=%

So the following might work:

del %WPINSTDIR:"=%\a.dll

It will fail, though, if the path contains spaces.

You can also use the following:

call :del_file %WPINSTDIR% a.dll
goto :eof
:del_file
del "%~1\%~2"
goto :eof

which should work even with paths containing spaces. The ~ in %~1 removes surrounding quotes.

Joey
+3  A: 

Try using

pushd %WPINSTDIR%
del /s a.dll
popd

This restores the former directory.

Frank Bollack
i just tried this and it seems to work. Need to check all scenarios. Thanks a lot all.
viswanathan
A: 

Thanks the push and pop method seems to work. need to check for all scenarios.

Thanks all

viswanathan
don't use answers to thank other users or say their solution work. Use comments instead
klez