anthonyaudi Posted February 1, 2012 Share Posted February 1, 2012 (edited) 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:\tempnow in c:\temp there are subfoldersthe 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 thisrmdir c:\temp /s/qmkdir c:\tempIt does what it is supposed to do it silently deletes everything and then remakes c:\tempIs 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 February 1, 2012 by anthonyaudi Link to comment Share on other sites More sharing options...
jaclaz Posted February 1, 2012 Share Posted February 1, 2012 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:DAnd what happens if you enter:DIR C:\temp\ /A:D /O:-DYou 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 /BYou 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 /qWhat 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 /qWhat do you get?You have your batch:@ECHO OFFSETLOCAL ENABLEEXTENSIONSFOR /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 More sharing options...
bphlpt Posted February 1, 2012 Share Posted February 1, 2012 (edited) 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 OFFSETLOCAL ENABLEEXTENSIONSFOR /F "tokens=* skip=1" %%G IN ('dir c:\temp /b /ad /o-d') DO rd /s /q "c:\temp\%%G"Cheers and RegardsLOL - 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 February 1, 2012 by bphlpt Link to comment Share on other sites More sharing options...
Yzöwl Posted February 1, 2012 Share Posted February 1, 2012 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 More sharing options...
dencorso Posted February 1, 2012 Share Posted February 1, 2012 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 More sharing options...
Yzöwl Posted February 1, 2012 Share Posted February 1, 2012 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 More sharing options...
bphlpt Posted February 1, 2012 Share Posted February 1, 2012 (edited) 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 February 1, 2012 by bphlpt Link to comment Share on other sites More sharing options...
gunsmokingman Posted February 2, 2012 Share Posted February 2, 2012 If the person wanted to use VBS ScriptDim 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 More sharing options...
anthonyaudi Posted February 6, 2012 Author Share Posted February 6, 2012 jaclaz: you my friend are an absolute genius. That script works like a charm. Link to comment Share on other sites More sharing options...
jaclaz Posted February 6, 2012 Share Posted February 6, 2012 jaclaz: you my friend are an absolute genius. Well, maybe I am a genius (possibly also absolute ), 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 OFFSETLOCAL ENABLEEXTENSIONSFOR /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 OFFSETLOCAL ENABLEEXTENSIONSSET Counter=1FOR /F "tokens=* delims=" %%A IN ('DIR C:\temp\ /A:D /O:-D /B') DO (SET LastDIR=C:\temp\%%ASET LastDIRIF %Counter%==1 GOTO :out_of_for1):out_of_for1FOR /F "tokens=* delims=" %%A IN ('DIR C:\temp\* /A:-D /S /O:-D /B') DO (SET LastItem=%%ASET LastItemIF "%%~dpA"=="C:\temp\%LastDIR%" ECHO OK, it's the same DIR&PAUSE&GOTO :do_rmdirGOTO :out_of_for2):out_of_for2ECHO NO MATCHPAUSEGOTO :EOF:do_rmdirFOR /F "skip=1 tokens=* delims=" %%A IN ('DIR C:\temp\ /A:D /O:-D /B') DO (ECHO rmdir "c:\temp\%%A" /s /q)GOTO :EOFThe 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now