1

I'm a bit of a newbie with batch files, but am looking for a script to delete .txt log files that are older than 30 days... (FWIW, I am on XP SP3...)

I tried to follow the example found here, (including downloading the forfiles.exe from here) and ended up with this:

forfiles -p"D:\SQL Database Back Ups\Update_FmlaManagement_Log" -s -m *.txt -d-30 -c "cmd /c del @FILE"

But I get this error:

Error path not found (3rd argument) line 545

I did a lot of googling, but can't seem to figure out what this error is talking about. Somebody suggested using forfiles.exe in order to get the @variables to work, but I tried that (along with the full forfiles path), and nothing seems to be working...

Can someone at least point me in the correct direction as to what this error message is telling me?

Community
  • 1
  • 1
sǝɯɐſ
  • 2,470
  • 4
  • 33
  • 47
  • maybe you just miss the space after -p ? – Loïc MICHEL Feb 07 '13 at 19:07
  • @Kayasax I thought it needed one there at first too, but when I ran it without the space there, I get `Invalid directory :`. I messed with it some more, and realized that the space isn't needed. If I run it without the space and enter an invalid filepath, I get `Invalid directory : D:\Blah`... So I'm pretty sure that isn't the issue, but I appreciate the suggestion! – sǝɯɐſ Feb 07 '13 at 19:47
  • What do you get for this `forfiles -c "cmd /c echo hello"` from the command line and from a batch? – jeb Feb 07 '13 at 21:14
  • @jeb, sorry, I didn't see this until after I had replaced my `forfiles.exe` (See my answer below)... Now I get a bunch of "hello"s :) – sǝɯɐſ Feb 07 '13 at 21:47

4 Answers4

1

Just to see whether I could, I cobbled a little sumn sumn together to delete files older than 30 days old without using forfiles. Make sure the pushd line is set to go to the appropriate directory. I got the 2592000 by subtracting 01/01/2013 from 01/31/2013.

@echo off
setlocal enabledelayedexpansion

pushd "D:\SQL Database Back Ups\Update_FmlaManagement_Log"

set today=%date:~-10%
call :toEpoch %today%
set /a thirtyDaysAgo=%epoch% - 2592000

for /f "tokens=1,5*" %%I in ('dir /o:d *.txt ^| findstr "txt$"') do (
    call :toEpoch %%I
    if "%%K"=="" (set file=%%J) else (set file=%%J %%K)
    if /i %thirtyDaysAgo% GTR !epoch! del /q "!file!"
)
popd
goto :EOF

:toEpoch date
echo wscript.echo datediff("s", "01/01/1970 00:00:00", "%1")>epoch.vbs
for /f %%x in ('cscript /nologo epoch.vbs') do set epoch=%%x
del /q epoch.vbs
goto :EOF

(VBscript borrowed from Cameron Oakenleaf.)

Admittedly, this won't work obsessively accurately. For instance, if a log file was last modified at noon 30 days ago and you're running this at 8:00 am, the file will probably be deleted even though it still has another 4 hours to go to reach 30 days old. And it's probably not nearly as efficient as forfiles.exe.

*shrug* It was a road less traveled and I traveled it.

EDIT: HerbCSO has another, probably better implementation that makes mine look like a Rube Goldberg-ian solution.

Community
  • 1
  • 1
rojo
  • 24,000
  • 5
  • 55
  • 101
  • I see I have a lot to learn about batch scripting, that all looks chinese to me :P LOL... I appreciate your effort, if I can't get my error resolved I may try using something like this... but I would prefer getting my one-liner working :) – sǝɯɐſ Feb 07 '13 at 20:45
1

@file is only the File name. It does not include the path. If your batch file is not being executed within your path it will not see the file to delete it because you are in a different working directory.

Squashman
  • 13,649
  • 5
  • 27
  • 36
  • That is probably a good point, but I also tried replacing `@FILE` with `@PATH\@FILE`, which is what one other SO user suggested, and this gives me the same error... I've tried `@FILE`, `@PATH`, and `@PATH\@FILE`, and none of these resolve the error... – sǝɯɐſ Feb 07 '13 at 20:41
  • @sǝɯɐſ `@path` is the full path with the file name. `@file` is the full filename with extension. – Squashman Oct 25 '18 at 22:04
1

I can't believe that the error comes from your sample line.
My forfiles (Win7) throws an error for the missing space between -p and the path.

And the text of the error message isn't normal for batch commands.
I don't know any batch command that tells you the line where an error occours, nor which parameter is wrong.

So I believe you got the error message from a completly different source.
Or you have a second forfiles command that do some totally other thing.
Or do you named one of your tests forfiles.bat?

jeb
  • 78,592
  • 17
  • 171
  • 225
  • Well I don't know exactly what the problem was, but your answer led me in the right direction... Apparently the issue was the `forfile.exe` file, as I tried one from a different site, and it worked perfectly. Thanks for your help! – sǝɯɐſ Feb 07 '13 at 21:42
1

Well, I'm not positive, but it seems the issue might have been a bad forfiles.exe from Microsoft's FTP server. I ended up downloading a second forfiles.exe (for Windows Server 2003) from this guy's site (direct download).

After replacing the original exe in C:\WINDOWS\system32 and getting rid of the copy in my downloads folder, I changed the syntax a little from the dashes to forward slashes as follows:

forfiles /p "D:\SQL Database Back Ups\Update_FmlaManagement_Log" /s /m *.txt /d -30 /c "cmd /c del @FILE"

and this worked perfectly first try!

So I guess the answer is, don't download forfile.exe from Microsoft's FTP server? :P

EDIT:

I would be very interested to know if other people have successfully used Microsoft's .exe in the link above? Otherwise maybe it's just something with my particular setup...

sǝɯɐſ
  • 2,470
  • 4
  • 33
  • 47