MSFN Forum: How would I create a batch in DOS that does this - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How would I create a batch in DOS that does this Rate Topic: -----

#1 User is offline   anthonyaudi 

  • Member
  • PipPip
  • Group: Members
  • Posts: 122
  • Joined: 10-June 08

Posted 01 February 2012 - 02:50 PM

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!

This post has been edited by anthonyaudi: 01 February 2012 - 02:52 PM



#2 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 9,112
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 01 February 2012 - 03:32 PM

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:

Quote

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

#3 User is offline   bphlpt 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 467
  • Joined: 12-May 07
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 01 February 2012 - 03:52 PM

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/

This post has been edited by bphlpt: 01 February 2012 - 03:57 PM


#4 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,195
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 01 February 2012 - 03:58 PM

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?

#5 User is offline   dencorso 

  • Adiuvat plus qui nihil obstat
  • Group: Super Moderator
  • Posts: 3,966
  • Joined: 07-April 07
  • OS:98SE
  • Country: Country Flag

Posted 01 February 2012 - 04:28 PM

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.

#6 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,195
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 01 February 2012 - 04:45 PM

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!

#7 User is offline   bphlpt 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 467
  • Joined: 12-May 07
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 01 February 2012 - 05:52 PM

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

This post has been edited by bphlpt: 01 February 2012 - 05:55 PM


#8 User is online   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,020
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 01 February 2012 - 07:26 PM

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 



#9 User is offline   anthonyaudi 

  • Member
  • PipPip
  • Group: Members
  • Posts: 122
  • Joined: 10-June 08

Posted 06 February 2012 - 09:30 AM

jaclaz: you my friend are an absolute genius.
That script works like a charm.

#10 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 9,112
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 06 February 2012 - 11:45 AM

View Postanthonyaudi, on 06 February 2012 - 09:30 AM, said:

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.

View Postanthonyaudi, on 06 February 2012 - 09:30 AM, said:

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:

Quote

@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:

Quote

@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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy