从目录中删除所有文件名及其创建和修改日期

时间:2015-08-05 05:33:42

标签: windows batch-file

我正在运行此bat脚本fileNames.bat > print.txt,以获取所有文件名。

@echo off
setlocal disableDelayedExpansion
:: Load the file path "array"
for /f "tokens=1* delims=:" %%A in ('dir /s /b^|findstr /n "^"') do (
  set "file.%%A=%%B"
  set "file.count=%%A"
)

:: Access the values
setlocal enableDelayedExpansion
for /l %%N in (1 1 %file.count%) do echo !file.%%N!

但是,我还希望为每个文件路径输入creationmodification date

我当前的输出如下:

C:\Programs\FirefoxPortable\fileNames.bat 
C:\Programs\FirefoxPortable\Firefox
C:\Programs\FirefoxPortable\print.txt
C:\Programs\FirefoxPortable\Firefox\App
C:\Programs\FirefoxPortable\Firefox\Data

有关如何获取每个文件路径旁边的creationmodification date的任何建议吗?

感谢您的回复!

1 个答案:

答案 0 :(得分:1)

关注.bat脚本会生成一个csv - 类型的输出,其下一个模式的垂直行分隔值为|

type|creation datetime|modification datetime|full path|

延迟扩展保持禁用,因为文件名包含!个感叹号:请参阅Variables: extract part of a variable (substring)和{{3}中的call set技巧}}。
请注意,检索创建日期和时间会区分文件夹和文件 对于其他区域设置, %%G标记化可能会有所不同!

@ECHO OFF
@SETLOCAL enableextensions disableDelayedExpansion
:: Set the working directory and store the previous folder/path
pushd d:\test
:: Load the file path "array"
set "file.count=0"
for /f "tokens=1* delims=:" %%A in ('dir /s /b^|findstr /n "^"') do (
  set "file.%%A=%%B"
  set "file.count=%%A"
)
:: Restore path/folder most recently stored by the PUSHD command
popd 

:: Access the values (keep )
set /A "ii=0"
:loop
if %ii% GEQ %file.count% goto :loopend
set /A "ii+=1"
call set "file.curr=%%file.%ii%%%"
if exist "%file.curr%\" (
  rem folder
  for %%g in ("%file.curr%") do (
    set "defined="
    for /F "skip=5 tokens=1,2" %%G in ('
      dir /-c /a:d /t:C "%file.curr%"
      ') do (
          if not defined defined (
            echo FLDR^|%%G %%H^|%%~tg^|%file.curr%^|
            set "defined=%%~tg %%G %%H"
          )
    )
  )
) else (
  rem file
  for %%g in ("%file.curr%") do (
    set "defined="
    for /F "skip=5 tokens=1,2" %%G in ('
      dir /-c /a:-d /t:C "%file.curr%"
      ') do (
          if not defined defined (
            echo FILE^|%%G %%H^|%%~tg^|%file.curr%^|
            set "defined=%%~tg %%G %%H"
          )
    )
  )
)
goto :loop
:loopend
ENDLOCAL
goto :eof

我当前的输出看起来如下(缩小到合理的大小):

==>D:\bat\SO\31824138.bat
FILE|26.07.2015 11:02|26.07.2015 11:02|d:\test\File N.txt|
FLDR|24.07.2015 20:21|05.08.2015 18:44|d:\test\set|
FILE|24.07.2015 20:23|24.07.2015 20:23|d:\test\set\hklm.txt|
FILE|24.07.2015 20:26|24.07.2015 20:30|d:\test\set\regs.txt|
FILE|05.08.2015 18:44|05.08.2015 18:45|d:\test\set\t!exclam!t.txt|

==>