64

I am trying to delete files in my $(TargetDir) within visual studio before building a project.

How do you have to format command line to get around this problem I am getting below? alt text

Community
  • 1
  • 1
dance2die
  • 35,807
  • 39
  • 131
  • 194

7 Answers7

88

Try

cd $(TargetDir)
del *.tif

As jvenema pointed out, your $(TargetDir) is expanding into a path containing spaces in the folder names which is breaking the delete command.

Eoin Campbell
  • 43,500
  • 17
  • 101
  • 157
  • 2
    I observed that even if no spaces are in the dirname, separating into a 'cd' and a wildcard makes the difference between working and busted :/ – Dean Radcliffe Dec 12 '12 at 01:41
  • @Eoin Could you please tell me where can I find the document of these command ? Or could you please give me the search key in the google ? thanks a lot. – Joe.wang Jul 10 '13 at 01:48
  • Just to clarify, TargetDir is probably your bin directory. – Ben Power Nov 22 '18 at 03:07
32

I ended up using rd /s /q "$(TargetDir)" to clean out the directory. As far as I know it is working.

tuck
  • 603
  • 5
  • 13
  • This removes even there are subdirectories also by /q option it removes quietly. It means "remove directory command"(rd) never ask for deletion(R U Sure?). After any build if there is an existing file this way become better,thanks – Davut Gürbüz Jun 28 '12 at 10:43
  • 1
    This quietly removes the build target directory, $(TargetDir), and its contents. Visual Studio recreates the build directory when necessary. – George Mar 17 '13 at 15:17
  • I also found this approach useful, as 'del' will fail with the same error when the directory you're looking to delete files from doesn't exist, which is a problem when you're looking to remove all files from a directory deeper in the build output structure. – OcularProgrammer May 18 '15 at 23:44
17

Try adding quotes around the directory.

Jerod Venema
  • 44,124
  • 5
  • 66
  • 109
  • Thanks jvenema: It seems like "quote" actually does what I thought it should do, but unfortunately I was not delete all images using a wide-card. Eoin's version worked though. – dance2die Apr 20 '09 at 13:49
14

You have to write del "$(TargetDir)*.tif" because of spaces in directory path.

Václav Dajbych
  • 2,584
  • 2
  • 30
  • 45
10

Old question but a couple of things:

del "$(TargetDir)*.tif" /q

1) /q is for quiet. Otherwise, del cmd prompts "... Are you sure (Y/N)?" which the build does not like.

2) As many have pointed out, "" around the targetDir for possible space in the target directory.

Sudip Shrestha
  • 441
  • 4
  • 12
0

For DOTNET Core, your quotes need to be escaped, like this:

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
  <Exec Command="del &quot;$(ProjectDir)wwwroot\_framework\*.*&quot; /q" />
</Target>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
  <Exec Command="copy &quot;$(ProjectDir)..\Client\bin\Debug\net5.0\wwwroot\_framework\*.*&quot; &quot;$(ProjectDir)wwwroot\_framework\&quot;" />
</Target>
Serj Sagan
  • 28,927
  • 17
  • 154
  • 183
-2

wmic process where name='chromedriver.exe' delete

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Meg-90
  • 259
  • 2
  • 8