此批处理是创建一个报告,输出文件夹的最新文件。主要目的是查看目录中生成文件的日期。还可以轻松检查目标目录是否更新。
问题:
更改输出以获取三个最新的文件,也被卡在此处,其中 For Loop 在另一个 For Loop 中,我在这里也使用延迟扩张。
尝试在 FOR循环中使用延迟扩展变量(!forOPT!)
FOR ... (
FOR /F "!forOPT!" %%a IN ('dir "!path2check!" /A-D /OD /t:w ^| FIND "/"') DO ( )
)
我想使用变量" forOPT"在另一个 For Loop 中替换" skip = 10 tokens = 1-5"但由于 For Loop 并不喜欢延迟扩展(!forOPT!),我被卡住了。
编辑 :
现在通过Mofi的建议和建议解决了问题。谢谢Mofi。
至于如何表达的答案,也是由杰布做出的。谢谢你,杰比。
我的问题的解决方案是使用 CALL 函数而不是嵌套 FOR循环。
请参阅我的回答以获取最终代码,并进行一些调整以及满足我需求的其他要求。
@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)));
报告也应如下所示。 (每个目录的三个最新文件)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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(&lt; 谢谢你)和SS64(&lt; 谢谢)的许多网站上合并并混合在一起。
即使在解释我没那么好的时候,你也可以问我,我可以澄清一下。 :)
如果标题需要编辑,请告诉我,这也可以帮助其他有类似问题的人。
谢谢 Stack Overflow,Mofi和Jeb。
答案 0 :(得分:2)
在FOR选项中,只有百分比扩展有效,因此您需要使用函数。
FOR /f "usebackq delims=" %%I IN (destPath.txt) DO (
...
SET "forOPT=skip=!totalSkip! tokens=1-5"
call :innerLoop
)
:innerLoop
FOR /F "%forOPT%" %%a IN ('dir "!path2check!" ...) do (
....
)
exit /b
答案 1 :(得分:0)
在Mofi和Jeb的帮助和建议下,这就是我想出来的。
最终代码。
@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)));
见下面的输出示例。
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 -