【问题标题】:batch monitor a folder and unzip each file批量监控文件夹并解压缩每个文件
【发布时间】:2017-06-02 18:47:07
【问题描述】:

我们的 ERP 程序生成包含 .xml 文档的 .zip 文件。 我需要确保将每个 .zip 文件提取到目标文件夹。 我找到了一个比较两个日志文件的批处理脚本,但我不知道如何处理新的(尚未解压缩的)文件

   @echo off
   if not exist c:\OldDir.txt echo. > c:\OldDir.txt
   dir /b "C:\Spektra\Gelen" > c:\NewDir.txt
   set equal=no
   fc c:\OldDir.txt c:\NewDir.txt | find /i "no differences" > nul && set   equal=yes
  copy /y c:\Newdir.txt c:\OldDir.txt > nul
  if %equal%==yes goto :eof
  rem Your batch file lines go here
  **********
  c:\unzip.exe (the_new_files) -d (destination)
  *******************

这是脚本

我需要处理旧日志文件中不存在的新文件

谢谢

【问题讨论】:

  • 我会走另一条路。归档属性最有可能在所有新文件上设置。所以我只会列出设置了存档属性的文件并解压缩这些文件。完成解压缩文件后,使用attrib 命令关闭存档属性。这需要一些设置才能开始。任何已经解压缩的文件都需要关闭它们的存档属性。
  • 好主意。我尝试使用日志文件中的当前操作日期和时间作为参考并解压缩以后的文件,但是我不知道如何列出比某个日期时间更新的文件。

标签: batch-file logging compare unzip


【解决方案1】:

要找出新的日志文件C:\NewDir.txt 相对于旧的OldDir.txt 添加了哪些行,您可以使用findstr command,它具有选项/G 来指定包含搜索的文件字符串。加上/X(完全匹配)和/V(返回不匹配的行),只返回NewDir.txt中添加的那些行,假设每一行都是唯一的:

findstr /V /X /I /G:"C:\OldDir.txt" "C:\NewDir.txt"

要处理返回的项目,请使用for /F loop 捕获它们:

for /F "eol=| delims=" %%F in ('
    findstr /V /X /I /G:"C:\OldDir.txt" "C:\NewDir.txt"
') do (
    rem // Do whatever you want with each file in `%%F`...
)

所以您的脚本可能如下所示:

@echo off
rem // Change to the working directory `C:\` once:
pushd "C:\" || exit /B 1 & rem/ ("C:\" is the root directory of drive "C:")
rem // Ensure `OldDir.txt` exists by appending nothing:
>> "OldDir.txt" rem/
rem // Create new log file `NewDir.txt`:
> "NewDir.txt" dir /B "C:\Spektra\Gelen"
rem // Process all newly added items in `NewDir`:
for /F "eol=| delims=" %%F in ('
    findstr /V /X /I /G:"OldDir.txt" "NewDir.txt"
') do (
    rem // Do whatever you want with each file in `%%F`:
    unzip "%%F" -d "C:\some\destination\folder"
)
rem // Move new log file onto old one, suppress report message:
> nul move /Y "NewDir.txt" "OldDir.txt"
rem // Restore previous working directory:
popd

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-05
    • 2019-11-29
    • 2019-05-07
    • 2017-12-16
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    相关资源
    最近更新 更多