2016-09-20 59 views
-1

以下是Report.csv文件,這裏我需要根據條件更新Status列,我的文件在​​存在。我寫了如果文件存在與否的代碼,但不知道如何更新Status列。批處理文件如果文件存在則更新一個csv文件

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

我看不到任何**逗號**在您的** C ** SV s充足的數據... – aschipfl

回答

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 

請閱讀Delimiter-separated values.csv.tsv差異。請注意,我不繼續使用引號包圍各個領域嚴格...

輸出

==> 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,"" 

==> 

更多資源(必讀的,不完全):