我遇到多个if语句时遇到问题。
文件的重命名取决于第3列中的字符串(标记= 3)。我没有问题抓住第3列字符串并将其设置为变量%fy%,并且设置16,17和18变量没有问题。多个IF语句发生错误。注 - 如果我只使用1 IF语句运行代码,它可以正常工作。为什么多个ifs绊倒了?
@echo off
for /f "tokens=3 delims=, skip=1" %%i in (DownloadedData.csv) do if not defined line set "fy=%%i"
echo %fy%
SET sixteen=^"FY16^
SET seventeen=^"FY17^
SET eightteen=^"FY18^
IF %fy% == %sixteen% goto :YEAR16
IF %fy% == %seventeen% goto :YEAR17
IF %fy% == %eightteen% goto :YEAR18
:YEAR16
set "filename=DATA_2016.csv"
echo %filename%
goto :COMMONEXIT
:YEAR17
set "filename=DATA_2017.csv"
echo %filename%
goto :COMMONEXIT
:YEAR18
set "filename=DATA_2018.csv"
echo %filename%
goto :COMMONEXIT
:COMMONEXIT
pause
REM - Code then goes on to use the filename variable to rename the file in the download folder.
答案 0 :(得分:1)
试试这个......
@echo off
for /f "tokens=3 delims=, skip=1" %%i in (DownloadedData.csv) do if not defined line set "fy=%%i"
echo %fy%
SET sixteen="FY16
SET seventeen="FY17
SET eighteen="FY18
IF "%fy%"" == "%sixteen%"" goto YEAR16
IF "%fy%"" == "%seventeen%"" goto YEAR17
IF "%fy%"" == "%eighteen%"" goto YEAR18
GOTO COMMONEXIT :: Here, if you wish to exit as a default if none matched. Otherwise, it will fall into YEAR16.
:YEAR16
set filename=DATA_2016.csv
goto COMMONEXIT
:YEAR17
set filename=DATA_2017.csv
goto COMMONEXIT
:YEAR18
set filename=DATA_2018.csv
goto COMMONEXIT
:COMMONEXIT
echo %filename%
pause
REM - Code then goes on to use the filename variable to rename the file in the download folder.
set fy=
set sixteen=
set seventeen=
set eighteen=
在将初始字符设置为双引号时,您需要在比较结束时匹配该双引号,因此为什么我在开头有两个双引号,在结尾有两个。
答案 1 :(得分:1)
我相信ManoDestra有一个可行的解决方案。
我认为您的FOR / F声明是错误的。我相信你想要
set "fy="
for /f .... do if not defined fy set "fy=%%i"
如果是这种情况,您可以大大简化代码:
@echo off
for /f "tokens=3 delims=, skip=1" %%i in (DownloadedData.csv) do goto %%i
:"FY16
set "filename=DATA_2016.csv"
echo %filename%
goto :COMMONEXIT
:"FY17
set "filename=DATA_2017.csv"
echo %filename%
goto :COMMONEXIT
:"FY18
set "filename=DATA_2018.csv"
echo %filename%
goto :COMMONEXIT
:COMMONEXIT
pause
REM - Code then goes on to use the filename variable to rename the file in the download folder
或者更好:
@echo off
set "fy="
for /f "tokens=3 delims=, skip=1" %%i in (DownloadedData.csv) do if not defined fy set "fy=%%i"
set "filename=DATA_20%fy:~-2%.csv"
echo %filename%
pause
REM - Code then goes on to use the filename variable to rename the file in the download folder