1.

Solve : Remove a directory but save a file within.?

Answer»

Long live DOS \ \\//

This is a post of my findings and to share with the DOS community.

Goal - Delete a tree of content but do not delete any files within named web.config
IIS 7.0 has some funky things going on with required web.config files within static content.

So, typically I would copy out the file and copy it back, but with this assignment, it messes things up with the webpage.
So...moving forward.

First part.... (remove files, copy files and delete all non web.config files)
rmdir /s /q %Static_Server%".\abcd_prev"
xcopy /q /e %Static_Server%".\abcd" %Static_Server%".\abcd_prev\">NUL

rem Programming note - For loop used to display all files within the %Static_Server%".\abcd_prev\" FOLDER.
rem Programming note - Warning - Then the loop will remove all files within the %Static_Server% path! (except for 'web.config")!!!!!
FOR /F "tokens=1-4" %%a in ('dir /a:-d /b /s %Static_Server%.\abcd^|find /v "web.config"') DO (Del /q %%a %%b %%c %%d)

1. First for loop is used to list "dir" all files within the context root folder and delete all files not named "web.config"
This leaves you with a whole lot of empty folders and the web.config files.

2. The next step is to clean up "empty directories"
Originally I had this long drawn out code I will share with you but then I found out how to suppress STDERR output...
The following are my troubleshooting steps...

a. errorlevel - Will not work because this is used for testing errors not testing if value = true.
b. {find " 0 File(s)" path} This tries to find the text within a folder and gives an access error because well its looking for a string value...
c. Looking for a way to set a VARIABLE to true for 0 files found.
test...If exist path|find "0 File(s)" (set true=GOOD) or something else like this...

Attempt 1 -
FOR %%b in ('dir %%a|find "0 File(s)"') DO (set string_input=%%b)
Ok (Problem - This still prints out multiple lines of "The directory is not empty." Issue second for loop...)

Attempt 2 -
SETLOCAL ENABLEDELAYEDEXPANSION
rem 1. Get a reverse order of the empty folders to be removed.
FOR /F "tokens=1-4" %%a in ('dir /a:d /b /s %Static_Server%.\abcd^|sort /R') DO (
rem 2. Find the folders labled with 0 Files.
FOR %%b in ('dir %%a|find "0 File(s)"') DO (set string_input=%%b)
)
rem 3. Compare and execute.
IF !string_input!==0 (echo.rmdir %%a %%b %%c %%d)
)
ENDLOCAL
Good - This will make sure the directory is empty before deleting.
This for loop will run backwards through the directory tree and sort the folders backwards.
But ..... Problem is you will still get some of those nasty error messages "The directory is not empty."

Attempt 3 -
FOR /F "tokens=1-4" %%a in ('dir /a:d /b /s %Static_Server%.\oad^|sort /R') DO (rmdir %%a %%b %%c %%d 2>NUL)
Best - This for loop will run through the directories backwards and it will suppress the evil error message with the "2>NUL" code.

Enjoy. - DarthClear as mud.Quote from: Darth on October 04, 2011, 06:31:44 PM

Long live DOS \ \\//


And yet, half the commands you use wouldn't work on DOS...I used DOS for years before and during the early years of Windows and frankly I think I knew it fairly well. Frankly, I've FORGOTTEN a lot of what I knew about DOS from years of non-use. I do still use it occasionally, but not as a primary operating system anymore. There are still a lot of things you need the command prompt for, but it seems that some of the questions on the DOS forum could be done fairly easily in Windows. If the object is to learn more about DOS then it's a good thing. If the object is to be productive, then there is a better way.Very well stated...


Discussion

No Comment Found