Jump to content

How would I create a batch in DOS that does this


Recommended Posts

Hello, Bare with me I am going to give you the jist of what I need.

I am attempting to do the following:

I have a user who has a folder called c:\temp

now in c:\temp there are subfolders

the subfolders are completely random names

what the user wants to do is have me create a batch file that deletes everything in c:\temp BUT always retains the last folder

(the folders are dates, so lets say 02-12-01 then 02-12-02 and so on so forth)

Now I created a script file like this

rmdir c:\temp /s/q
mkdir c:\temp

It does what it is supposed to do it silently deletes everything and then remakes c:\temp

Is there any other switch I can put to make it omit a folder without any user intervention?

As far as I know a batch file can't do that but maybe someone can point me in the right direction?

Maybe another program that does it for you? Or another kind of batch file?

Keep in mind 02-12-01 and so on is completely made up I don't know what the names of the folders will be they are random.

Thanks!

Edited by anthonyaudi
Link to comment
Share on other sites


First thing the folder names are EITHER dates or random.

If the folder names are "random" then you will have to use date attributes.

If you have something like:

C:\temp\

C:\temp\1278trpo\

C:\temp\wetr72pl\

C:\temp\w8rty66zqw39\

What happens if you open a command prompt and enter (you can copy and paste):

DIR C:\temp\ /A:D

And what happens if you enter:

DIR C:\temp\ /A:D /O:-D

You will see how the most recently modified folder is the FIRST "normal" folder, either right after the "." and ".." entries or right before them.

Now, issue:

DIR C:\temp\ /A:D /O:-D /B

You will see how the most recently modified folder is first one.

Now issue:

@ECHO OFF&FOR /F "tokens=* delims=" %A IN ('DIR C:\temp\ /A:D /O:-D /B') DO ECHO rmdir "c:\temp\%A" /s /q

What do you get?

Now issue:

@ECHO OFF&FOR /F "skip=1 tokens=* delims=" %A IN ('DIR C:\temp\ /A:D /O:-D /B') DO ECHO rmdir "c:\temp\%A" /s /q

What do you get?

You have your batch:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
FOR /F "skip=1 tokens=* delims=" %%A IN ('DIR C:\temp\ /A:D /O:-D /B') DO (
ECHO rmdir "c:\temp\%%A" /s /q
)

Of course in order to have it actually working you need to remove the ECHO.

Also mind you that depending on the filesystem used, and on the users who created contents, you may have access permissions issue.

jaclaz

Link to comment
Share on other sites

This should do what you asked - Silently delete all folders and their contents in c:\temp of any arbitrary name, including allowing spaces in the name, but leaving the last modified folder in place with its contents intact.


@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
FOR /F "tokens=* skip=1" %%G IN ('dir c:\temp /b /ad /o-d') DO rd /s /q "c:\temp\%%G"

Cheers and Regards

LOL - figures jaclaz would beat me to it - AND with a full explanation!!

There are many good batch reference sites, but this one is easy to use - http://ss64.com/nt/

Edited by bphlpt
Link to comment
Share on other sites

Bear in mind that we'd need to be talking NTFS for the timestamps to be 'touched' on modification of its content, (DOS suggests pre-NTFS).

If we knew more about the Operating System and what exactly these random directories were there may be a more appropriate solution. I'm not keen on trusting a solution to remove all except one. Are these directories modified on a particular frequency?

Link to comment
Share on other sites

The OP is *NOT* using DOS, no way! All versions of DOS RMDIR would fail when the target directory has any files in it, besides not accepting either /s or /q... he/she must be using the DOS-box of a NT-Family OS for the described script (notice he/she didn't say "batch file", either) to work. Under true DOS, DELTREE would be required.

Link to comment
Share on other sites

It is also important to note that the system whereby NTFS folder timestamps are touched isn't very accurate and it may be better to search for the directory which contains the most recently modified file. Believe me they can be different!

Link to comment
Share on other sites

Well, I'm sure if the OP's needs are not met by the provided script, they'll either modify it using the resources provided or will post again with further questions and more details. Your points are taken, Yzöwl, and the OP should consider them. But since the OP's original "solution" was to erase them ALL, and since they are located in "c:\temp", the simplistic solution might very well suffice.

Cheers and Regards

Edited by bphlpt
Link to comment
Share on other sites

If the person wanted to use VBS Script


Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Folder :Folder = "C:\Temp"
If Not Fso.FolderExists(Folder) Then
Fso.CreateFolder(Folder)
Else
Fso.DeleteFolder(Folder),True
Fso.CreateFolder(Folder)
End If

Link to comment
Share on other sites

jaclaz: you my friend are an absolute genius.

Well, maybe I am a genius :unsure: (possibly also absolute :rolleyes: ), but not because of that three-liner, which is very basic.

That script works like a charm.

Sure it does, BUT take into consideration what Yzöwl said, there are situations where this approach is NOT 100% "foolproof" or "safe".

Depending on the actual value of data, I would implement a slightly more complete check, along the lines suggested.

The posted example simply skips over first result:

@ECHO OFF

SETLOCAL ENABLEEXTENSIONS

FOR /F "skip=1 tokens=* delims=" %%A IN ('DIR C:\temp\ /A:D /O:-D /B') DO (

ECHO rmdir "c:\temp\%%A" /s /q

)

What you could do is check the first result:

@ECHO OFF

SETLOCAL ENABLEEXTENSIONS

SET Counter=1

FOR /F "tokens=* delims=" %%A IN ('DIR C:\temp\ /A:D /O:-D /B') DO (

SET LastDIR=C:\temp\%%A

SET LastDIR

IF %Counter%==1 GOTO :out_of_for1

)

:out_of_for1

FOR /F "tokens=* delims=" %%A IN ('DIR C:\temp\* /A:-D /S /O:-D /B') DO (

SET LastItem=%%A

SET LastItem

IF "%%~dpA"=="C:\temp\%LastDIR%" ECHO OK, it's the same DIR&PAUSE&GOTO :do_rmdir

GOTO :out_of_for2

)

:out_of_for2

ECHO NO MATCH

PAUSE

GOTO :EOF

:do_rmdir

FOR /F "skip=1 tokens=* delims=" %%A IN ('DIR C:\temp\ /A:D /O:-D /B') DO (

ECHO rmdir "c:\temp\%%A" /s /q

)

GOTO :EOF

The above is just an example and can be definitely bettered, it will fail for files inside a subdir (but can be easily adapted to take care of this also).

jaclaz

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...