batch:如何将文件名赋给变量

时间:2018-05-29 09:49:57

标签: batch-file variables

我需要使用批处理一遍又一遍地过滤大量文件,因此在批处理文件中编辑每个文件的名称会有很多工作要做。所以我想添加像这样的东西

SET "CName=dir "U:\Morheng S\Partage\_Scans\Result ICT\Done (Filter rework)\*.txt""

但显然它没有按预期工作。目标是将其与其他变量一起使用,但我无法使其发挥作用。该文件的名称总是类似于.Complete.txt,但只设置而不是只检查任何文件会更难。

我虽然喜欢这样的事情。

  • 启动循环虽然var CName不是空或
  • 搜索txt-file
  • 编辑txt文件
  • 移动txt文件
  • 再次启动循环

我认为这实际上非常简单,但我还没弄明白如何将结果中的值传递给var。我也试过这个:

SET CName=*
dir "U:\Morheng S\Partage\_Scans\Result ICT\Done (Filter rework)\*.txt" > %CName%

但是这只会给我一个非常新的文件,该文件被移动到该文件夹​​中。 :C

编辑: 这是当前的批次,但我无法弄清楚如何正常工作。

@ECHO off
    SET SPT="U:\Morheng S\Partage\_Scans\Result ICT\Done (Filter rework)\%CNAME%.Complete.txt"
    SET SPT2="U:\Morheng S\Partage\_Scans\Result ICT\Done (Filter rework)\%CNAME%.Filtrer.txt"
    SET SPT3="U:\Morheng S\Partage\_Scans\Result ICT\Done (Filter rework)\%CNAME%.Temp.txt"
    SET SPT4="U:\Morheng S\Partage\_Scans\Result ICT\Done (Filter rework)\Refiltered"
    SET SPT5="U:\Morheng S\Partage\_Scans\Result ICT\Done (Filter rework)\%CNAME%.Programms.txt"

ECHO Cleaning Collected Entries ...
for %%a in ("U:\Morheng S\Partage\_Scans\Result ICT\Done (Filter rework)\*.Complete.txt") do (
    SET CName=%%a
    echo Filename: %CName%

    ::Removing of all the already checked lines (Common programs only)
    type %SPT% | findstr /v /c:"Office Professional Plus 2016" | findstr /v /c:"Office Professional Plus 2013" | findstr /v /c:"Office Home and Business 2016" | findstr /v /c:"DisplayName    REG_SZ    Microsoft Office Professionnel Plus" | findstr /v /c:"Trend Micro" | findstr /v /c:"7-Zip"  | findstr /v /c:"QuickZip" | findstr /v /c:"Mozilla Firefox" | findstr /v /c:"Google Chrome" | findstr /v /c:"Opera" | findstr /v /c:".NET Framework 4.7" | findstr /v /c:".NET Framework 4.6" | findstr /v /c:".NET Framework 4.5" | findstr /v /c:"Adobe Acrobat Reader" | findstr /v /c:"Dynamics NAV 2015" | findstr /v /c:"MobaXterm" > %SPT2%
    ::Removing of all the already checked lines (Additional common programs)
    type %SPT2% | findstr /v /c:"PDFCreator" | findstr /v /c:"CDBurnerXP" | findstr /v /c:"Java 8" | findstr /v /c:"Java 7" | findstr /v /c:"Java 6" | findstr /v /c:"Java 5" | findstr /v /c:"paint.net" | findstr /v /c:"FileZilla Client" | findstr /v /c:"Notepad++" | findstr /v /c:"EaseUS Partition Master" > %SPT3%
    ::Removing all the unnecessary lines
    type %SPT3% | findstr /v "Word OneNote Access Excel Outlook PowerPoint InfoPath Publisher Skype Proofing Groove HPCLJProM452 Taalprogramma C++ UninstallLegacy linguistique Herramientas Korrekturhilfen DeskUpdate Plusÿ2016 Description" | findstr /v /c:"Logiciel pour p" | findstr /v /c:"USB 3.0 eXtensible Host" | findstr /v /c:"Intel(R) Network Connections" | findstr /v /c:"Mozilla Maintenance Service" | findstr /v /c:"System CLR Types" | findstr /v /c:"HP Color LaserJet Pro M452" | findstr /v /c:"64-bit Components" | findstr /v /c:"Visual Studio 2010 Tools" | findstr /v /c:"Adobe Refresh Manager" | findstr /v /c:"Realtek High Definition Audio" | findstr /v /c:"Processor Graphics" | findstr /v /c:"Report Viewer" | findstr /v /c:"HP Unified" | findstr /v /c:"Chipset Device Software" | findstr /v /c:"Management Engine Components" | findstr /v /c:"Office OSM" | findstr /v /c:"Open XML SDK" | findstr /v /c:"Recover Keys" | findstr /v /c:"Dynamics NAV Setup" | findstr /v /c:"DCF MUI" | findstr /v /c:"Office OSM" | findstr /v /c:"Office OSM" | findstr /v /c:"Office Shared" | findstr /v /c:"Components Installer" | findstr /v /c:"Java Auto Updater" > %SPT2%
    timeout 1 /nobreak > nul
    DEL %SPT3%
    timeout 1 /nobreak > nul
    move %SPT% %SPT4%
    move %SPT2% %SPT4%
)
ECHO Cleaning complete!
ECHO Please press any key to finish!
PAUSE > NUL

我可能遗漏了一些东西。 :/

2 个答案:

答案 0 :(得分:0)

要获取命令输出到变量,请使用for /f循环:

for /f "delims=" %%a in ('dir /b /s "U:\Morheng S\Partage\_Scans\Result ICT\Done (Filter rework)\*.txt"') do set "file=%%a"

但是for可以自己处理文件名,使其更容易:

for %%a in ("U:\Morheng S\Partage\_Scans\Result ICT\Done (Filter rework)\*.txt") do set "file=%%a"

注意两种方法都会返回 last 文件,如果找到多个。

如果要处理每个发现,请将代码包含在循环中:

for %%a in ("U:\Morheng S\Partage\_Scans\Result ICT\Done (Filter rework)\*.txt") do (
  echo found file %%a
  echo modifying %%a
  echo move "%%~a" "C:\temp\"
)

答案 1 :(得分:0)

不清楚循环是否应处理当前存在的文件或
不断等待扩展名为.Complete.txt的新文件到达。 该批处理使用for命令和被调用的子例程。

@Echo off
Set "BaseDir=U:\Morheng S\Partage\_Scans\Result ICT\Done (Filter rework)\"
PushD "%BaseDir%" || (Echo Can't locate %BaseDir% & PAuse & Exit /B 1)
Set Delay=30
:Loop
For %%F in (*.Complete.txt ) do Call :Sub %%F
Waiting for %Delay% seconds
Timeout %Delay%
goto :Loop

:Sub
Echo Processing %1
notepad %1
Echo move %1 x:\path\