* For循环*和*延迟扩展*的问题 [英] Issue with *For loop* and *delayed expansion*

查看:62
本文介绍了* For循环*和*延迟扩展*的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此批次用于创建报告,该报告输出文件夹的最新文件.主要目的是查看目录内生成文件的日期.还可以轻松检查目标目录是否已更新.

This batch is to create a report which outputs the latest file of a folder. Main purpose is to see the date of generated files inside directories. To also easily check whether the target directory is update or not.

问题:
更改输出以获取三个最新文件,也将 For循环卡在另一个 For循环中,我也在使用延期扩张.

Issue :
Change the output to get the three latest files instead, also got stuck here with the For Loop inside another For Loop, I'm also using delayed expansion.

尝试在 FOR循环(!forOPT!)

Try to use delayed expansion variable inside FOR loop (!forOPT!)

FOR ... (
    FOR /F "!forOPT!" %%a IN ('dir "!path2check!" /A-D /OD /t:w ^| FIND "/"') DO ( )
)

我想在另一个 For循环中使用变量"forOPT"来代替"skip = 10 tokens = 1-5",但是由于 For循环没有不喜欢延迟的扩展(!forOPT!),我被困住了.

I would like to use variable "forOPT" in an another For Loop as to replace the "skip=10 tokens=1-5" but since For Loop doesn't like delayed expansion (!forOPT!), I'm stuck.

编辑 :

Edit :

现在,Mofi的建议可以解决问题.谢谢Mofi.

Issue is now solved by the suggestion and advice of Mofi. Thank you Mofi.

杰布(Jeb)也做了回答,以说明如何做.所以也谢谢杰布.

As for the answer to show how, also had been made by Jeb. So thank you too Jeb.

我所关心的问题的解决方案是使用 CALL 函数而不是嵌套 FOR循环.

The solution to my regarding issue is to use a CALL function instead of nesting FOR loops.

有关最终代码,请参见我的答案,同时还要进行一些调整和满足我需要的其他要求.

Please see my answer for the final code with also some tweaks and additional requirement to accomplish my needs.

@if (@CodeSection == @Batch) @then
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET "path2check=NULLLL"
SET "TargetDIR=%~dp0"
SET "curPATH=%~F0"
SET "newSize1=0"
SET JScall=Cscript //nologo //E:JScript !curPATH!
FOR /f "usebackq delims=" %%I IN (destPath.txt) DO (
SET "path2check=%%I"
SET cnt=0
SET totalSkip=1
FOR %%A IN ("!path2check!\*") DO SET /a cnt+=1
SET /a totalSkip=!cnt!-3

SET "forOPT=skip=!totalSkip! tokens=1-5"

ECHO !forOPT! ^<^<^< shows option
ECHO.

ECHO ---------------------

FOR /F "!forOPT!" %%a IN ('dir "!path2check!" /A-D /OD /t:w ^| FIND "/"') DO (
    SET "Date1=%%a"
    SET "Time1=%%b"
    SET "Time2=%%c"
    SET "Size1=%%d"
    SET "Filename1=%%e"
    SET newSize1=!size1:,=!
    IF !newSize1! LEQ 1000 (
      SET "newSize2=!newSize1! Bytes"
    )
    IF !newSize1! GEQ 1000 (
      FOR /F %%a IN ('!JScall! "!newSize1!/1024"') DO SET "newSize2=%%a"
      FOR /F "delims=." %%z IN ("!newSize2!") DO SET "newSize2=%%z KB"
    )
    IF !newSize1! GEQ 1000000 (
      FOR /F %%a IN ('!JScall! "!newSize1!/1024/1024"') DO SET  "newSize2=%%a"
      FOR /F "delims=." %%z IN ("!newSize2!") DO SET "newSize2=%%z MB"
    )
    ECHO !Filename1! - !newSize2! - !Time1!!Time2! - !Date1!
    )
    ECHO - EOR - !path2check!
    ECHO.
)
GOTO :EOF

@end
WScript.ECHO(eval(WScript.Arguments.Unnamed.Item(0)));

该报告也应如下所示. (每个目录的三个最新文件)

The report should also look like below. (The three latest file of each directory)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Most recently file of path : E:\

Date: dd/mm/yyyy hh:mmPM   |   Size: xxx MB  |   Filename: abc.abc
Date: dd/mm/yyyy hh:mmPM   |   Size: xxx MB  |   Filename: abc.abc
Date: dd/mm/yyyy hh:mmPM   |   Size: xxx MB  |   Filename: abc.abc

                   -   EOR   -

此批处理文件还通过StackOverflow(<谢谢)和SS64(<谢谢)上的许多站点进行了组合和混合.

This batch file was also combined and mixed together from many sites on both StackOverflow (<Thank you) and SS64 (<Thank you).

即使在解释上我也不是很好,但是您可以提出要求,我可以澄清一下. :)

Even in explaining I'm not that good, but you can ask and I can clarify. :)

如果标题需要编辑,请告诉我,这也可能会帮助遇到类似问题的其他人.

If the title need to be edited please let me know, this also may help others with similar problems.

谢谢堆栈溢出,Mofi和Jeb.

Thank you Stack Overflow, Mofi and Jeb.

推荐答案

在Mofi和Jeb的帮助和建议下,这就是我想出的.

With help and suggestion of Mofi and Jeb this is what I came up with.

  • 从文本文件获取目标路径.
  • 列出目标路径中最新的三个文件.
  • 最终代码.

  • Get target path from a text file.
  • List the latest three files from the target path.
  • Final code.

@if (@CodeSection == @Batch) @then
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET "path2check=NULLLL"
SET "TargetDIR=%~dp0"
SET "curPATH=%~F0"
SET "newSize1=0"
SET JScall=Cscript //nologo //E:JScript !curPATH!

FOR /f "usebackq delims=" %%I IN (destPath.txt) DO (
  SET "path2check=%%I"
  ECHO - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  ECHO(
  ECHO Most recently file of path : !path2check:~0,47!
  ECHO(
  CALL :countFilesFunc path2check forOPT Date1 Time1 Time2 newSize2 Filename1
  ECHO(
  ECHO                        -   EOR   -
  ECHO(
)
GOTO :EOF

REM- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
REM- Count files in folder function section starts below
REM- - - - - - - - - - - - - - - - - - - - - - - - - - - - -

:countFilesFunc    - passing a variable by reference
SET "cnt=0"
SET "totalSkip=1"
FOR %%A IN ("!path2check!\*") DO SET /a cnt+=1
SET /a totalSkip=!cnt!-3
SET "forOPT=skip=!totalSkip! tokens=1-4*"

FOR /F "%forOPT%" %%a IN ('dir "!path2check!" /A-D /OD /t:w ^| FIND "/"') DO (
  SET "Date1=%%a"
  SET "Time1=%%b"
  SET "Time2=%%c"
  SET "Size1=%%d"
  SET "Filename1=%%e"
  SET newSize1=!size1:,=!
  IF !newSize1! LEQ 1000 (
    SET "newSize2=!newSize1! Bytes"
  )
  IF !newSize1! GEQ 1000 (
    FOR /F %%a IN ('!JScall! "!newSize1!/1024"') DO SET "newSize2=%%a"
    FOR /F "delims=." %%z IN ("!newSize2!") DO SET "newSize2=%%z KB"
  )
  IF !newSize1! GEQ 1000000 (
    FOR /F %%a IN ('!JScall! "!newSize1!/1024/1024"') DO SET "newSize2=%%a"
    FOR /F "delims=." %%z IN ("!newSize2!") DO SET "newSize2=%%z MB"
  )
  ECHO !Filename1!> tempfile.txt
  FOR %%? IN (tempfile.txt) DO ( SET /A strlength=%%~z? - 2 )
  IF !strlength! GEQ 15 SET "Filename1=!Filename1:~0,15!.."
  ECHO Date: !Date1! !Time1!!Time2!   ^|   Size: !newSize2!  ^|   Filename: !Filename1!
)
ECHO.
GOTO :EOF

@end
WScript.ECHO(eval(WScript.Arguments.Unnamed.Item(0)));

  • 请参见下面的输出示例.

  • See below example of the output.

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Most recently file of path : E:\
    
    Date: 05/07/2015 08:19PM   |   Size: 278 MB  |   Filename: 353.30-desktop-..
    Date: 09/07/2015 08:27PM   |   Size: 2 MB  |   Filename: teracopy.exe
    Date: 10/07/2015 09:23PM   |   Size: 51 Bytes  |   Filename: a amprex.txt
    
                   -   EOR   -
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Most recently file of path : C:\NVIDIA\DisplayDriver\337.88\Win8_WinVista_Wi
    
    Date: 11/06/2014 03:58PM   |   Size: 27 KB  |   Filename: setup.cfg
    Date: 11/06/2014 03:58PM   |   Size: 14 KB  |   Filename: EULA.txt
    Date: 11/06/2014 04:01PM   |   Size: 200 KB  |   Filename: ListDevices.txt..
    
                   -   EOR   -
    

  • 这篇关于* For循环*和*延迟扩展*的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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