加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

批处理文件 – Windows CMD – 在for循环中设置不起作用

发布时间:2020-12-13 22:39:15 所属栏目:Windows 来源:网络整理
导读:我想编写批处理文件,它将循环遍历包含备份目录的所有目录,并删除其中超过X天的文件. 在我想要运行我的脚本的计算机上,没有“forfile”命令. 没有Power Shell,因此CMD或VBScripts似乎只是完成此任务的方法. 目前我有“设置”语句的问题 – 似乎当我调用%chec
我想编写批处理文件,它将循环遍历包含备份目录的所有目录,并删除其中超过X天的文件.
在我想要运行我的脚本的计算机上,没有“forfile”命令.
没有Power Shell,因此CMD或VBScripts似乎只是完成此任务的方法.

目前我有“设置”语句的问题 – 似乎当我调用%checkpath%时,我没有收到预期的文件夹.

rem we will memorize current directory
pushd %cd%
set folder="C:Documents and SettingsmynameDesktop"
cd %folder%
rem loop only folders with five chars within their names (unfortunately on less also
for /D %%g in (?????) DO (
    set checkpath="%cd%%%gbackup"
    if exist %checkpath% (
        for %%a in ('%%gbackup*.*') do (
        set FileDate=%%~ta
        set FileDate=%%FileDate:~0,10%%
        rem here I want to compare file modification data with current date
        )
    )
popd
pause

解决方法

您需要使用延迟扩展来读取您在for循环中设置的变量.

试试这个

setlocal enabledelayedexpansion
rem we will memorize current directory
pushd %cd%
set folder="C:Documents and SettingsmynameDesktop"
cd %folder%
rem loop only folders with five chars within their names (unfortunately on less also
for /D %%g in (?????) DO (
    set checkpath="%cd%%%gbackup"
    if exist !checkpath! (
        for %%a in ('%%gbackup*.*') do (
        set FileDate=%%~ta
        set FileDate=!%FileDate:~0,10%!
        rem here I want to compare file modification data with current date
        )
    )
popd
pause

用您创建的变量替换%’,将表示它使用延迟扩展.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读