批处理脚本,用于使用目录的结果

发布于 2024-07-27 23:35:46 字数 958 浏览 3 评论 0原文

我正在尝试编写一个小批处理脚本,该脚本将从目录中删除文件旧文件。 第一个参数是脚本要查看的目录,第二个参数是要保留的文件数。

rem @ECHO OFF

rem %1 is the path in which to look for the files
rem %2 is the number of recent files to preserve

if [%1] EQU [] (
    echo ERROR: Missing Required Paramater directory path.
    goto :eof
)

if [%2] EQU [] (
    echo ERROR: Missing Required Paramater, number of files to preserve
    goto :eof
)

if %2 LSS 0 (
    echo ERROR: Number of files to preserve provided was negative
    goto :eof
)

set FolderPath=%1
set SafeNumber=%2

cd %FolderPath%

for /f %%f in ('dir /O-D /A-D /B') do call :delete %%f
goto :eof

:delete
if %SafeNumber% LEQ 0 (
    del %1
) else (
    set /a SafeNumber-=1
)
goto :eof

:eof

本质上,我这里有一个目录,它输出按从最新到最旧的顺序排列的文件名列表。 根据 SafeNumber 的不同,它将跳过前几个文件,然后在 SafeNumber <= 0 后继续删除。

我现在遇到的问题是,如果文件名是“Test File.txt”(如包含空格。“Test”作为 %1 传递到删除中,而不是“Test File.txt”。

有关如何使我的脚本工作的任何想法,或者也许有人有更好的书面解决方案?

I'm trying to write a small batch script that will delete files old files from a directory.
The first parameter is the directory for the script to look into, and the second is the number of files to preserve.

rem @ECHO OFF

rem %1 is the path in which to look for the files
rem %2 is the number of recent files to preserve

if [%1] EQU [] (
    echo ERROR: Missing Required Paramater directory path.
    goto :eof
)

if [%2] EQU [] (
    echo ERROR: Missing Required Paramater, number of files to preserve
    goto :eof
)

if %2 LSS 0 (
    echo ERROR: Number of files to preserve provided was negative
    goto :eof
)

set FolderPath=%1
set SafeNumber=%2

cd %FolderPath%

for /f %%f in ('dir /O-D /A-D /B') do call :delete %%f
goto :eof

:delete
if %SafeNumber% LEQ 0 (
    del %1
) else (
    set /a SafeNumber-=1
)
goto :eof

:eof

Essentially what I have here is a dir that outputs a list of filenames ordered from newest to oldest. Depending on what SafeNumber is, it will skip the first few files and then procede with deletion once SafeNumber <= 0.

The problem I'm having right now, is if the filename is "Test File.txt" (as in contains a space. "Test" is passed into the delete as %1, rather then "Test File.txt".

Any ideas on how to get my script working, or perhaps someone has a better written solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

对你再特殊 2024-08-03 23:35:47

处理文件名等中的空格。 您需要引用文件名和路径“%1”等......也许这是您唯一的问题。

To handle whitespace in filenames,etc. You'll want to quote for filenames and paths "%1" etc.... Maybe that is your only problem.

白馒头 2024-08-03 23:35:47

您必须将 find 命令与 -newer 一起使用,然后使用 -exec rm 。 例如,要删除比 file.txt 新的所有文件,请执行以下操作:

find . -type f -newer file.txt -exec rm {} \;

your going to have to use the find command with -newer and then -exec rm. For example to delete all file newer than file.txt do:

find . -type f -newer file.txt -exec rm {} \;
高冷爸爸 2024-08-03 23:35:47

很久以前,我编写了一个简单的 Java prog,它应该从控制台启动,即从批处理文件启动。 prog 从指定目录中删除旧文件。 看看这里,我实际上不知道不记得它到底是如何工作的,但我建议首先在测试文件夹上使用它。 它应该用于Windows,至少我在那里使用它。

我现在切换到 Mac,使用每次启动时启动的以下 Automator 脚本来完成这项工作:

find /Users/<yourUser>/Downloads/* -type f -mtime +90 -exec mv -f {} /Users/<yourUser>/.Trash \;

Long time ago, I wrote a simple Java prog that is supposed to be launched from the console, i.e. from a batch file. The prog deletes the old files from a specified directory. Check it out here, I actually don't remember how exactly it works, but I would suggest using it first on a test-folder. It is supposed to be used for Windows, at least there it is where I used it.

I now switched to a Mac, where I'm using the following Automator script I launch on each boot for doing that job:

find /Users/<yourUser>/Downloads/* -type f -mtime +90 -exec mv -f {} /Users/<yourUser>/.Trash \;
千秋岁 2024-08-03 23:35:47

更改您的 for 循环,并取消删除功能。

for /f "skip=%safenum%" %%f in ('dir /o-d /a-d /b') do ( del %%f )

我对 for 循环有一种不健康的热爱......

Change your for loop, and do away with your delete function.

for /f "skip=%safenum%" %%f in ('dir /o-d /a-d /b') do ( del %%f )

I have an unhealthy love of for loops...

凉风有信 2024-08-03 23:35:46

您是否尝试过在输入或脚本中的文件名两边加上引号? 我的意思是脚本的 %f 或 %1 周围。

Have you tried putting quotation marks around the file name in your input, or in your script? I mean around the %f or the %1, for the script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文