Page 1 of 1

Trying to create a data cleaning batch file

Posted: 08 Jun 2011 14:50
by Wenin
I have a server that accumulates a number of files everyday. The file's names can vary wildly, and I've found that they include ! points.

I took a batch file I found online and modified it to work for my purposes. Now I'm running into a problem with how it is handling ! since I'm using the ENABLEDELAYEDEXPANSION setting.

It is setup so that it won't delete itself and another batch file.

Oh and when writing this post, I noticed the folder contains files with filenames that include chinese characters! LOL

When the batch file encounters a file with an exclaimation point, it throws a:
"Could Not Find C:\uploads\20100826154948 - same error.gif"

File's name 20100826154948 - same error!.gif

Code: Select all

@echo off
::------------------------------------------------------------------------------------
::-- This batch file is designed to delete any file within it's folder that is older
::-- than 1 day.  This file is intended to run within C:\Uploads folder.
::------------------------------------------------------------------------------------
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION


call:jdate tnow "%date%"

for %%F in (*.*) do (
    call:ftime tfile "%%F"
    set /a diff=tnow-tfile
   if !diff! GTR 1 if "%%F" NEQ "RenameIt.bat" if "%%F" NEQ "Filechecker.bat" (del "%%~nxF")
)

GOTO:EOF

::-----------------------------------------------------------------------------------
::-- Functions start below here
::-----------------------------------------------------------------------------------


:ftime JD filename attr -- returns the file time in julian days
::                      -- JD    [out]    - valref file time in julian days
::                      -- attr  [in,opt] - time field to be used, creation/last-access/last-write, see 'dir /?', i.e. /tc, /ta, /tw, default is /tw
:$created 20060101 :$changed 20090322 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL
set file="%~2"
set attr=%~3
if not defined attr (call:jdate JD "- %~t2"
) ELSE (for /f %%a in ('"dir %attr% /-c "%file%"|findstr "^^[0-9]""') do call:jdate JD "%%a")
( ENDLOCAL & REM RETURN VALUES
    IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
)
EXIT /b


:jdate JD DateStr -- converts a date string to julian day number with respect to regional date format
::                -- JD      [out,opt] - julian days
::                -- DateStr [in,opt]  - date string, e.g. "03/31/2006" or "Fri 03/31/2006" or "31.3.2006"
:$reference http://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/a0c34d593e782e94/50ed3430b6446af8#50ed3430b6446af8
:$created 20060101 :$changed 20080219
:$source http://www.dostips.com
SETLOCAL
set DateStr=%~2&if "%~2"=="" set DateStr=%date%
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
    for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
        set %%a=%%A&set %%b=%%B&set %%c=%%C))
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b


Re: Trying to create a data cleaning batch file

Posted: 08 Jun 2011 15:14
by Cleptography

Re: Trying to create a data cleaning batch file

Posted: 09 Jun 2011 07:14
by Wenin
Thank you =)

Re: Trying to create a data cleaning batch file

Posted: 09 Jun 2011 07:21
by Wenin
I believe that batch file has the same issue as the one I presented. I ran it in my test directory:

20100826154948 - same error.gif = 20100826154948 - same error!.gif

C:\test>fileages morethan 1000 Del c:\test
The system cannot find the path specified.
20100826154948 - same error.gif is 734694 days old
deleteme.txt is 1 days old
FileAges.bat is 0 days old
Filechecker.bat is 2 days old
RenameIt.bat is 3 days old
test.bat is 1 days old
test.ps1 is 1 days old

C:\test>fileages c:\test
20100826154948 - same error.gif is 734694 days old
deleteme.txt is 1 days old
FileAges.bat is 0 days old
Filechecker.bat is 2 days old
RenameIt.bat is 3 days old
test.bat is 1 days old
test.ps1 is 1 days old

Edit: I just noticed that the "same error" file isn't that old either. =)

Re: Trying to create a data cleaning batch file

Posted: 09 Jun 2011 09:41
by dbenham
Wenin wrote:Now I'm running into a problem with how it is handling ! since I'm using the ENABLEDELAYEDEXPANSION setting.
You can also run into problems if the name contains ^. The solution is to transfer the FOR loop variable to a normal variable with delayed expansion disabled, and then do the remainder of the loop with delayed expansion enabled, making sure to endlocal before loop end. This is also a common technique when trying to process the contents of a file with a FOR loop.

Note: You can provide both SETLOCAL arguments to one command.

Try this (untested) - Note I did not bother reposting the functions at the bottom:

Code: Select all

@echo off
::------------------------------------------------------------------------------------
::-- This batch file is designed to delete any file within it's folder that is older
::-- than 1 day.  This file is intended to run within C:\Uploads folder.
::------------------------------------------------------------------------------------
setlocal EnableExtensions DisableDelayedExpansion

call:jdate tnow "%date%"

for %%F in (*.*) do (
  call:ftime tfile "%%F"
  set /a diff=tnow-tfile
  set "file=%%F"
  setlocal EnableDelayedExpansion
  if !diff! GTR 1 if "!file!" NEQ "RenameIt.bat" if "!file!" NEQ "Filechecker.bat" (del "!file!")
  endlocal
)

GOTO:EOF

Edit Note: Fixed the references to file in IF statement to use delayed expansion

Dave Benham