win7 batch convert string to number

January 5, 2011 at 23:58:59
Specs: Windows 7

I have a folder within which there are multiple subfolders recursively. all these sub folders are named with a timestamp e.g. c:\test\2010112414254234 i.e year(4 chars) month (2 chars) date (2 chars) hour (2 chars) minute (2 chars) second (2chars) millisecond (2 chars). now my batch script needs to go into the root i.e. "test" and delete all folders 1 day (24hrs) older than todays date time, so i need to convert that foldername into an int and do a number comaprision, or is there a better way... thanks for your help...

See More: win7 batch convert string to number

Report •

January 6, 2011 at 00:20:06
@echo off & setLocal EnableDELAYedeXpansion

pushd c:\files

for /f "tokens=* delims= " %%a in ('dir/b/s/ad ^| find /v "20110106"') do (
echo rd /q %%a

Life is too important to be taken seriously.


Report •

January 6, 2011 at 01:29:11
Hi Mechanix2Go,

i get the following error...
'find' is not recognized as an internal or external command, operable program or batch file.

Also, this will be a batch which will be run every midnight and will go in and cleanup the specified folder of any subfolders which are 1 day or older (based on the filename which is a timestamp), we could use the folder last modified or created date too right, might be easier...

Report •

January 6, 2011 at 02:32:01
I wonder if FIND was left out of win7.

Explore the drive where windows is installed, usually c:, and search for FIND.

Then paste in your %PATH%. [Into your next reply.]

Life is too important to be taken seriously.


Report •

Related Solutions

January 6, 2011 at 05:55:28
Mechanix2Go: I wonder if FIND was left out of win7.
Thu 01.06.2011  8.24.58+

Microsoft Windows [Version 6.1.7600]

Thu 01.06.2011  8.25.00+
c:\Windows\System32>dir find.exe /b
Nope, still there. I'm not sure why vybz is getting an error. (Not shown: The same results for the x64 version)

As for integer conversion, don't bother. CMD uses signed 32-bit variables, but that number requires a 64-bit variable.

Personally, I'd stick with VBScript for date/time operations; it's just easier, especially if you can use the date metadata already provided by the file system.

Set fso = CreateObject("Scripting.FileSystemObject")
For Each d In fso.GetFolder(".").SubFolders
  If d.DateCreated < (Now - 1) Then _
Next 'd

How To Ask Questions The Smart Way

Report •

January 6, 2011 at 08:40:43
@echo off
setLocal EnableDELAYedExpansion

::Get Today's Date
Set Day=%Date:~-7,2%
Set Month=%Date:~-10,2%
Set Year=%Date:~-4,4%

:: Set the compair date to the Julian of today's date.
Call :jdate %Year% %Month% %Day%
Set /a CompairDate=%jdate%

:: Set the compair date minus the number of days.
Set /a CompairDate=!CompairDate!-1

Set FileDate=1

for /f "tokens=*" %%a in ('dir c:\test\ /b /s /ad') do (
  set /a FileDate=%%a:~-16,8%
  set FileDay=!FileDate:~3,2!
  set FileMonth=!FileDate:~0,2!
  set FileYear=!FileDate:~6,4!
  ::Convert Filedate into a Julian Date
  call :jdate !FileYear! !FileMonth! !FileDay!
  set /a FileDate=!Jdate!
  if "!FileDate!" lss "!CompairDate!" (
    del "%%a" /s /q
    echo *Deleted* %%a
Goto :EOF

:: Convert date to Julian

IF %MM:~0,1% EQU 0 SET MM=%MM:~1%
IF %DD:~0,1% EQU 0 SET DD=%DD:~1%

SET /A Month1 = ( %MM% - 14 ) / 12
SET /A Year1  = %1 + 4800
SET /A JDate  = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( %MM% - 2 -12 * %Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + %DD% - 32075
SET Month1=
SET Year1=
Goto :EOF

You have to convert the dates to Julian dates first because if Today's date was 01/01/2011 and you just subtracted 1 from it then the date would be 00/01/2011. Julian dates allow you to subtract 1 and still get the result you wanted which would have been 12/31/2010.

The above code is untested so you may want to remove the DEL command until you see how it works.

P.S. I would just use RAZOR's code because the NOW command is already converted to a Julian Date. The only problem with RAZOR's code is that it look at the creation date of the folder and not the name of the folder.

Report •

January 7, 2011 at 01:39:59
Hi R2,

Thanks for FINGing. I can't imagine why it would be left out. But if the Redmond WA brain trust sees fit to leave out debug, ya never know.

You're exactly right about cmd math and that vbs is much better for date math.

In this case if the situation is as described, no math needed. Just del everything not containing NNNNN.

That is, once he gets on the sunny side of FIND.

Any side bets on whether FIND is gone or the path is hosed?

Life is too important to be taken seriously.


Report •

Ask Question