如果文件存在则在条件下更新csv文件的批处理文件

时间:2016-09-20 06:33:02

标签: batch-file

以下是Report.csv文件,我需要根据条件更新Status列,Lockbox中的文件存在。我编写了if文件是否存在的代码,但不知道如何更新Status列。

Lockbox   Location    Status       Reason(N)
3080      Minot      Y  
1780      Minot      N        Not Scheduled
2280      Minot      Y  
3015      Windsor    Y  
2215      Windsor    Y  
1515      Windsor    Y  
29011   Windsor      Y  

以下是我编写的代码。请帮忙..

    @echo off
    setlocal
    set count=0
    echo %time%
    echo %date%

   set y=%Date:~10,5%
   set m=%Date:~4,2%
   set d=%Date:~7,2%

   if "%time:~0,1%"==" " (set tym=0%time:~1,1%) ELSE set tym=%time:~0,2%
  set tm=%tym%%time:~2,0%
  echo %tm%
  pause
  set pattern1=INGFINS.IMAGE.image1.29011.%y%%m%%d%%tm%


 set pattern2=INGFINS.IMAGE.image2.2215.%y%%m%%d%%tm%


set pattern3=INGFINS.IMAGE.image3.1515.%y%%m%%d%%tm%


set pattern4=INGFINS.IMAGE.image4.3015.%y%%m%%d%%tm%


set pattern5=INGFINS.IMAGE.image5.1780.%y%%m%%d%%tm%


set pattern6=INGFINS.IMAGE.image6.2280.%y%%m%%d%%tm%


set pattern7=INGFINS.IMAGE.image7.3080.%y%%m%%d%%tm%

IF EXIST "C:\Users\Desktop\zipLocation\*%pattern1%*.<" (
   ECHO "%pattern1% exist"

) ELSE (
   ECHO "%pattern1% not exist"
)

IF EXIST "C:\Users\Desktop\zipLocation\*%pattern2%*.<" (
   ECHO "%pattern2% exist"
) ELSE (
   ECHO "%pattern2% not exist"
)

IF EXIST "C:\Users\Desktop\zipLocation\*%pattern3%*.<" (
   ECHO "%pattern3% exist"
) ELSE (
   ECHO "%pattern3% not exist"
)

IF EXIST "C:\Users\Desktop\zipLocation\*%pattern4%*.<" (
   ECHO "%pattern4% exist"
) ELSE (
   ECHO "%pattern4% not exist"
)

IF EXIST "C:\Users\Desktop\zipLocation\*%pattern5%*.<" (
   ECHO "%pattern5% exist"
) ELSE (
   ECHO "%pattern5% not exist"
)

IF EXIST "C:\Users\Desktop\zipLocation\*%pattern6%*.<" (
   ECHO "%pattern6% exist"
) ELSE (
   ECHO "%pattern6% not exist"
)

IF EXIST "C:\Users\Desktop\zipLocation\*%pattern7%*.<" (
   ECHO "%pattern7% exist") ELSE (
   ECHO "%pattern7% not exist"

)
rem call "statusReport.vbs"
endlocal
pause

1 个答案:

答案 0 :(得分:1)

虽然其他一些用户倾向于关闭你的问题,但我给出了下一个解决方案,希望它可以提供帮助。 评论.bat脚本

@ECHO OFF >NUL
SETLOCAL EnableExtensions DisableDelayedExpansion

    rem set input and output files: change to meet your circumstances
set "_fileIn=d:\bat\so\files\39587196_Report.TSV"       tab-separated values
set "_fileOut=d:\bat\so\files\39587196_ReportOut.csv"   comma-separated values
    rem empty output file
>"%_fileOut%" type NUL

    rem show input file
type "%_fileIn%"
echo(

    rem obtain %_datetime% variable in locale independent yyyymmddHHMMSS format
for /f "tokens=2 delims==" %%G in (
  'wmic OS get localdatetime /value') do set "_datetime=%%G"
set "_datetime=%_datetime:~0,14%"
    rem reduce %_datetime% variable to yyyymmddHH format as the OQ does 
set "_datetime=%_datetime:~0,10%"

set count=0
echo %date%  %time%
echo %_datetime%

set "_pattern1=INGFINS.IMAGE.image1.29011.%_datetime%"
set "_pattern2=INGFINS.IMAGE.image2.2215.%_datetime%"
set "_pattern3=INGFINS.IMAGE.image3.1515.%_datetime%"
set "_pattern4=INGFINS.IMAGE.image4.3015.%_datetime%"
set "_pattern5=INGFINS.IMAGE.image5.1780.%_datetime%"
set "_pattern6=INGFINS.IMAGE.image6.2280.%_datetime%"
set "_pattern7=INGFINS.IMAGE.image7.3080.%_datetime%"
set "_pattern8=INGFINS.IMAGE.image7.8888.%_datetime%" for testing purposes

set "_ForGDelims= delims=," CSV input: delimiter is Comma (0x2C Comma)
set "_ForGDelims="          TSV input: delimiter is Tab   (0x09 Character Tabulation)
if /I "%_fileIn:~-4%"==".tsv" set "_ForGDelims=" soft-code instead of above line

set "_csvHeader="             CSV header not processed yet 
    rem iterate input file, line by line, tokenized
for /F "usebackq tokens=1,2,3,*%_ForGDelims%" %%G in ("%_fileIn%") do (
  set "_Lockbox=%%~G"
  set "_Location=%%~H"
  set "_Status=%%~I"
  set "_Reason=%%~J"
  if defined _csvHeader (
        rem show checked Lockbox value (no NewLine) 
    <NUL set /P "=checking %%~G"
    set "_LockboxFoundInVariable="     
        rem iterate all _pattern* variables 
    for /F "tokens=1,2 delims==" %%g in ('set _pattern') do (
          rem take 4th token in %_pattern*% value  
      for /F "tokens=4 delims=." %%# in ("%%~h") do (
        if "%%#"=="%%~G" (
          echo  - found %%G in %%g:%%h
          set "_LockboxFoundInVariable=%%g"
                rem ???   I don't comprehend what this ↓ LessThan should mean ???  
          IF EXIST "C:\Users\Desktop\zipLocation\*%%h*.<" (
                rem ???   I don't comprehend what this ↑ LessThan should mean ???  
            set "_Status=y"    pattern exists
          ) ELSE (
            set "_Status=n"    pattern does not exist
          )
        )
      )
    )
        rem echo NewLine if necessary i.e. if checked Lockbox value not in any _pattern*  
    if not defined _LockboxFoundInVariable (echo()
  ) else (
    set "_csvHeader=anything"     CSV header processed already  
  )
  call :csvLineOut
)

    rem show output file
echo( 
type "%_fileOut%"

ENDLOCAL
goto :eof

:csvLineOut
    rem you could use <TAB>s  ↓           ↓         ↓  instead of commas 
>>"%_fileOut%" echo %_Lockbox%,%_Location%,%_Status%,"%_Reason%"
goto :eof

请阅读有关.csv.tsv差异的Delimiter-separated values。请注意,我不会严格使用引号来围绕每个字段...

<强>输出

==> D:\bat\SO\39587196.bat
Lockbox Located Status  Reason(N)
3080    Minot   Y
1111    Test    Y
1780    Minot   N       Not Scheduled
2280    Minot   Y
2215    Windsor Y
29011   Windsor Y
9999    Test    Y

20.09.2016  17:41:53,87
2016092017
checking 3080 - found 3080 in _pattern7:INGFINS.IMAGE.image7.3080.2016092017
checking 1111
checking 1780 - found 1780 in _pattern5:INGFINS.IMAGE.image5.1780.2016092017
checking 2280 - found 2280 in _pattern6:INGFINS.IMAGE.image6.2280.2016092017
checking 2215 - found 2215 in _pattern2:INGFINS.IMAGE.image2.2215.2016092017
checking 29011 - found 29011 in _pattern1:INGFINS.IMAGE.image1.29011.2016092017
checking 9999

Lockbox,Located,Status,"Reason(N)"
3080,Minot,n,""
1111,Test,Y,""
1780,Minot,n,"Not Scheduled"
2280,Minot,n,""
2215,Windsor,n,""
29011,Windsor,n,""
9999,Test,Y,""

==>

更多资源(必读,不完整):