[译]清理Cookies文件夹
By robot-v1.0
本文链接 https://www.kyfws.com/applications/clean-out-the-cookies-folder-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 8 分钟阅读 - 3688 个词[译]清理Cookies文件夹
原文地址:https://www.codeproject.com/Articles/8629/Clean-out-the-Cookies-folder
原文作者:David Crow
译文由本站 robot-v1.0 翻译
前言
A command-line utility for cleaning out the Cookies folder.
用于清除Cookies文件夹的命令行实用程序.
介绍(Introduction)
前几天,当我偶然进入Cookies文件夹时,正在浏览某些文件夹.至少可以说,我震惊地看到其中有数千个文件,其中一些文件已经使用了将近三年.考虑到IE的cookie相关设置一定有问题,我检查了IE的属性,一切看起来都应该正确.我点击了(I was browsing through some folders the other day when I stumbled into the Cookies folder. I was shocked, to say the least, to see several thousand files in there, some of which were nearly three years old. Thinking that something must be wrong with IE’s cookie-related settings, I checked IE’s properties and everything looked as it should. I clicked on the)**删除厨师(Delete Cook)一世(i)es(es)**按钮,并显示"(button and was presented with a “)删除Internet临时文件文件夹中的所有cookie?(Delete all cookies in the Temporary Internet Files folder?)“消息.我的回答是肯定的,然后去检查Cookies文件夹.它没有被更改!但是,所有其他临时文件都已删除.此时,我不确定Cookies文件夹是否是特殊情况,或者是否我只是错过了一些明显的东西(发生),我在其他几台机器上尝试了同样的事情,但结果却好坏参半,其中有一部分Cookies文件夹被清空,而在另一些文件夹上则保持不变.(” message. I responded in the affirmative and then went to check the Cookies folder. It was untouched! All other temporary files had been deleted, however. At this point, I was not sure if the Cookies folder was a special case, or if I was just missing something obvious (it happens). I tried this same thing on a few other machines and got mixed results. On some of them, the Cookies folder was emptied. On others, the folder remained untouched.)
回到我进行Web开发时,我似乎记得(Back when I was doing Web development, I seem to remember an) Expires
的属性(attribute of the) Cookies
用于设置Cookie过期日期的集合.我离开那家公司之前,我们一直没有去验证cookie是否满足了他们的所有要求.也许我是错误的,以为他们会在日期指定的日期自动清除它们(collection that was used to set the date on which the cookie would expire. I left that company before we ever got around to verifying whether cookies did all they needed to or not. Maybe, I’m incorrect in thinking that they would automatically get cleaned up when the date indicated by the) Expires
属性已通过.取而代之的是,它可能只是Web页面的指示符,表明cookie太旧而无法使用,应该创建另一个cookie.我欢迎您提出任何关于此谜的意见.(attribute had passed. Instead, maybe it is simply an indicator to the Web page that the cookie is too old to be used and that another should be created. I welcome any input on this mystery.)
就是说,我以为我会整理一篇有关自动清除所有旧/未使用文件的Cookies文件夹所需内容的文章.以下各节是对该工作的简要讨论.随意更改代码以适合您的需求(例如,更多选项,更多灵活性,更多错误检查).(That said, I thought I would put together an article on what it would take to automatically clean out the Cookies folder of any old/unused files. The following sections are a brief discussion of that endeavor. Feel free to change the code to fit your needs (e.g., more options, more flexibility, more error checking).)
请注意,我并不是出于替换IE内部所做的事情而创建此文章/实用程序的.这仅仅是为了解决似乎在IE管理其Cookies文件夹方面存在的不一致之处.如果IE毫无问题地清理了Cookie文件夹,则本文不适合您使用.但是,如果发现IE无法清理Cookies文件夹,则可能对本文有所帮助.请享用!(Please note that I did not create this article/utility with the intent of replacing something that IE does internally. It was merely an attempt at addressing what appears to be an inconsistency in how IE manages its Cookies folder. If IE cleans up your Cookies folder with no troubles, this article is not intended for you. If, however, you find that IE does not clean up your Cookies folder, you might just find this article helpful. Enjoy!)
找到Cookies文件夹(Locating the Cookies folder)
那么,Cookie在哪里(So, where is the Cookie)罐(jar)文件夹吗?最简单的找出方法是(folder anyhow? The easiest way to find out is with the) SHGetFolderPath()
功能.(function.)
// call GetBuffer() once instead of potentially calling it twice below
CString strCookiesPath;
LPTSTR lpszPath = strCookiesPath.GetBuffer(MAX_PATH);
// where is the 'jar' of cookies?
HRESULT hResult = SHGetFolderPath(NULL, CSIDL_COOKIES,
NULL, SHGFP_TYPE_CURRENT, lpszPath);
if (FAILED(hResult))
hResult = SHGetFolderPath(NULL, CSIDL_COOKIES,
NULL, SHGFP_TYPE_DEFAULT, lpszPath);
strCookiesPath.ReleaseBuffer();
这一点,(At this point,) strCookiesPath
应该包含类似”(should contain something like “)C:\ Documents and Settings \ <用户> \ Cookies(C:\Documents and Settings<user>\Cookies)".要开始搜索此文件夹,我们需要做的是在反斜杠和一对通配符上加点,然后退出!在MFC之前,我将采用以下代码执行此操作:(”. To begin searching this folder, all we need to do is tack on a backslash and a pair of wildcard characters, and we’re off! Prior to MFC, I would have employed code like the following to do this:)
char szPath[256] = "C:\Documents and Settings\<user>\Cookies";
if (szPath[strlen(szPath)] != '\\')
strcat(szPath, "\\");
这按预期工作,但是当我在1993年开始使用MFC时,我切换到(This worked as intended, but when I started using MFC back in 1993, I switched to) CString
对象.结果是:(objects instead. This resulted in:)
strCookiesPath = "C:\Documents and Settings\<user>\Cookies";
if (strCookiesPath.Right(1) != "\\")
strCookiesPath += "\\";
这有点干净,但随后出现了本地化问题.硬编码反斜杠字符可能会引起麻烦.这与(This was a little bit cleaner but then the problem of localization came up. Hard-coding the backslash character could invite trouble. It has to do with the) Right()
方法.在DBCS语言(例如日语)中,这是一个常见的错误,因为如果字符串中的最后一个字符为双字节,并且尾随字节为0x5C(反斜杠的值),则代码将错误地认为反斜杠已经存在(感谢Michael Dunn).为了解决这个问题,我使用了(method. In DBCS languages (e.g., Japanese), this is a common bug because if the last character in the string is double-byte, and the trail byte is 0x5C (the value of backslash), the code will mistakenly think a backslash is already there (thanks Michael Dunn). To address that, I used) PathAddBackslash()
喜欢:(like:)
// add a backslash if necessary
PathAddBackslash(strCookiesPath.GetBuffer(MAX_PATH));
strCookiesPath.ReleaseBuffer();
处理Cookies文件夹(Process the Cookies folder)
现在,我们的字符串包含一个反斜杠.现在,我们可以搜索(或处理)Cookie文件夹了.为此,我只是使用了(Our string now contains a trailing backslash. Now we are ready to search (or process) the Cookies folder. To do this, I simply used a) CFileFind
目的.由于本文的整个前提是查找在一定天数内未访问过的cookie,因此我们只需要将cookie的上次访问日期与其他用户配置的日期(即截止日期)进行比较即可.如果cookie足够老,则将其添加到cookie列表中,以便以后删除或显示.我正在使用迈克尔邓恩的(*object. Since the whole premise of this article was to find cookies that had not been accessed in a certain number of days, we simply need to compare the cookie's last-accessed date with some other user-configured date (i.e., cutoff date). If the cookie is old enough, it is added to a list of cookies to be removed, or displayed, later. I am using Michael Dunn's*)
CShellFileOp这个班.这是一个很好的包装(*class for this. This is a nice wrapper around the*)
SHFileOperation()` 功能.(function.)
CShellFileOp shfo;
CTime dateFile,
dateCutoff;
CFileFind fileFind;
BOOL bFound = fileFind.FindFile(strCookiesPath + _T("*.*"));
while (TRUE == bFound)
{
bFound = fileFind.FindNextFile();
if (! fileFind.IsDirectory())
{
// if the file was last written to before the cutoff date,
// add it to the list of files to be removed
fileFind.GetLastWriteTime(dateFile);
if (CTime(dateFile.GetTime()) < dateCutoff)
shfo.AddSourceFile(fileFind.GetFilePath());
}
}
现在,我们有了可以删除或输出到屏幕的Cookie列表(也许您想查看(We now have a list of cookies that can either be deleted, or output to the screen (maybe you want to see the cookies that)将会(would be)删除).要显示cookie,只需遍历列表即可.(deleted). To show the cookies, just iterate through the list.) CShellFileOp
提供一种方法(provides a method,) GetSourceFileList()
,以访问此列表.(, to access this list.)
const CStringList &listSourceFiles = shfo.GetSourceFileList();
POSITION pos;
CString strFilename;
pos = listSourceFiles.GetHeadPosition();
while (NULL != pos)
{
strFilename = listSourceFiles.GetNext(pos);
cout << (LPCTSTR) strFilename << endl;
}
但是,如果我们实际上要删除Cookie,则只需使用(If however, we actually wanted to delete the cookies, we would just use the) CShellFileOp::Go()
方法.有关如何使用此类的更多信息,包括如何将cookie移到回收站而不是删除它们,请参阅Michael的页面.(method for this. For more on how to use this class, including how to move cookies to the Recycle Bin instead of deleting them, consult Michael’s page.)
计算截止日期(Calculating the cutoff date)
截止日期可以通过简单地构造一个(The cutoff date can be calculated by simply constructing a) CTimeSpan
对象并将其从当前日期中减去.例如,如果您要删除过去15天内未访问过的cookie,则可以将截止日期指定为:(object and subtracting it from the current date. For example, if you wanted to delete cookies that had not been accessed within the last 15 days, you would specify a cutoff date of:)
dateCutoff = CTime::GetCurrentTime() - CTimeSpan(15, 0, 0, 0);
现在,如果Cookie的最后访问日期早于截止日期,它将被删除.(Now, if the cookie’s last-accessed date is older than the cutoff date, it will get deleted.)
笔记(Notes)
使用恰当命名的实用程序(Using the utility, aptly named)饼干怪兽(cookiemonster),在当前状态下,支持五个命令行开关.这些的处理是通过Paul DiLascia的(, in its current state, five command-line switches are supported. The handling of these is done via Paul DiLascia’s) CCommandLineInfoEx
类.这是每个的概要:(class. Here is a synopsis of each:)
/天 ##(/days ##) | 自上次访问Cookie以来的天数.(Number of days since cookie was last accessed.) | |
---|---|---|
/list | 显示Cookie(Show cookies that)将会(would be)已删除.如果使用Cookies,则不受影响.(deleted. Cookies are not affected if this is used.) | |
/verbose | 显示程序正在执行的详细信息.(Show details of what the program is doing.) | |
/recycle | 将cookie移到回收站.忽略是否(Move cookies to the Recycle Bin. Ignored if)/list用来.(is used.) | |
/? | 显示帮助屏幕.(Show the help screen.) |
发行(Issuing)**cookiemonster/days 2/verbose/recycle(cookiemonster /days 2 /verbose /recycle)**在命令提示符下(可能很容易成为计划任务),在我的计算机上产生了以下结果:(at a command prompt, which could just as easily be a scheduled task, yielded the following result on my machine:)
Deleting cookies last accessed before 10/19/04.
Cookies will be moved to the Recycle Bin.
Deleting cookies from C:\Documents and Settings\davidc\Cookies.
File: davidc@arstechnica[2].txt.
...Date: 10/21/04
File: davidc@arvest[2].txt
...Date: 10/19/04
...Added
File: davidc@ask[2].txt
...Date: 10/20/04
File: davidc@bbc.co[1].txt
...Date: 10/19/04
...Added
File: davidc@cardeals[2].txt
...Date: 10/20/04
File: davidc@celebritytrivia[1].txt
...Date: 10/20/04
File: davidc@channelintelligence[2].txt
...Date: 10/21/04
<balance of output deleted for brevity>
请注意,将两个文件添加到列表中,而没有五个文件是因为它们的上次访问日期比截止日期新.(Note that two files were added to the list, while five were not because their last-accessed date was newer than the cutoff date.)
参考文献(References)
- 保罗`迪拉西亚(Paul DiLascia)(Paul DiLascia’s) CCommandLineInfoEx(CCommandLineInfoEx) 类.(class.)
- 迈克尔`邓恩(Michael Dunn)(Michael Dunn’s) CShellFileOp(CShellFileOp) 类.(class.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C++ VC6 Windows Win2K MFC Visual-Studio Dev 新闻 翻译