0

When it comes to Windows command prompt, I always get confused about path, which seems so easy to grasp in Linux Terminal counterparts.

What I wanna do is, I want to delete everything(both files and folders in one command) in the path (for eg. pub\static directory path) except a file called .htaccess relative from my root directory(C:\wwwroot\Magento2), which is open in Windows command prompt.

How can I do this, I tried the below for loop, but it deletes .htaccess instead. Also how to avoid for loop altogether but get the job done as I described (relative from root directory) ?

for %i in (C:\wwwroot\Magento2\pub\static\*) do if not %i == .htaccess del %i

So I googled a bit and tried below one, but it doesn't do anything:

FOR /D %p IN ("C:\wwwroot\Magento2\pub\static\*.*") DO ( IF NOT %p == .htaccess ( IF EXIST %~sp\NUL ( RMDIR "%p" /S /Q ) ELSE ( DEL /F "%p" ) ) )
Vicky Dev
  • 1,893
  • 2
  • 27
  • 62

1 Answers1

0

Single PowerShell command:

Get-ChildItem C:\inetpub\wwwroot\Magento2\pub\static -Recurse |
  Where-Object { $_.Name -ne ".htaccess" } | Remove-Item -Recurse
Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
  • Well it gives this error `An empty pipe element is not allowed. At line:1 char:4 + | <<<< Where-Object { $_.Name -ne ".htaccess" } | Remove-Item -Recurse + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : EmptyPipeElement` – Vicky Dev Dec 19 '16 at 20:05
  • Even if I don't have administrator access then also I can open and run commands in Powershell ? – Vicky Dev Dec 19 '16 at 20:06
  • You don't have to be administrator. Also, the above two lines are a single command. The newline is for readability. I moved the pipe character to the correct place. – Bill_Stewart Dec 19 '16 at 20:14