CMD窗口 - 设置为内循环不工作 [英] Windows CMD - set within for loop is not working

查看:127
本文介绍了CMD窗口 - 设置为内循环不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写批处理文件将通过包含备份目录中的所有目录和环比删除在它X天的文件。
关于这一点我想运行我的脚本没有forfile命令的计算机。
有没有PowerShell的,所以CMD或VB脚本似乎只有完成这一任务的方式。

目前我有问题设置语句 - 看来,当我打电话的%的CheckPath%我没有收到预期的文件夹

  REM,我们将记住当前目录
PUSHD%CD%
集文件夹=C:\\ Documents和Settings \\ MYNAME \\桌面
CD%文件夹%
雷姆循环只有五个字符的文件夹名称中(很不幸的也少
针对/ D %%克(?????)DO(
    集的CheckPath =%CD%\\ %%摹\\备份
    如果存在%的CheckPath%(
        对于一个%%在('%%摹\\备份\\ *。*')做(
        设置FILEDATE = %%〜TA
        设置FILEDATE = %% FILEDATE:〜0.10 %%
        REM在这里我想比较当前日期文件修改数据
        )
    )
POPD
暂停


解决方案

您需要使用延迟扩展来读取你设置了一个循环中的变量。

试试这个

  SETLOCAL enabledelayedexpansion
REM我们会记住当前目录
PUSHD%CD%
集文件夹=C:\\ Documents和Settings \\ MYNAME \\桌面
CD%文件夹%
雷姆循环只有五个字符的文件夹名称中(很不幸的也少
针对/ D %%克(?????)DO(
    集的CheckPath =%CD%\\ %%摹\\备份
    如果存在!的CheckPath! (
        对于一个%%在('%%摹\\备份\\ *。*')做(
        设置FILEDATE = %%〜TA
        设置FILEDATE =%FILEDATE:〜0,10%!
        REM在这里我想比较当前日期文件修改数据
        )
    )
POPD
暂停

针对你所创建的信号,它使用延迟扩展变量

更换取值!来代替。

I want to write batch file which will loop through all directories containing backup directory and remove files older than X days within it. On computer which I want run my script there's no "forfile" command. There's no PowerShell, so CMD or VBScripts seems to be only way of accomplishing this task.

Currently I have problem with "set" statement - it seems that when I'm calling %checkpath% I didn't receive expected folder.

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

解决方案

You need to use delayed expansion to read a variable that you have set inside a for loop.

Try this instead

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

Replacing the %'s with !'s on variables you have created will signal it to use delayed expansion instead.

这篇关于CMD窗口 - 设置为内循环不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆