我正在尝试编写将从csv文件读取第一个值的脚本(即每行的第一列),包含文件名。 然后在某个位置搜索此文件,并将每个找到的路径添加到csv,作为新的最后一列。 我有下一个脚本,它只是试图搜索文件。 但它无法正常工作。好像我在这行中有空的%reportname%:
FOR /F "tokens=*" %%N IN ('dir /b /A:-D /s %reportname%')
完整的脚本:
setlocal enabledelayedexpansion
set currentdir=%CD%
set grspath=\Sources\ROL\Kesko\grs13\
set filename=sqrscripts.csv
set tmpfilename=scripts.new
SET reppath=""
SET reportname=""
IF NOT "%1"=="" grspath=%1
FOR /F "tokens=1,2,3,4,5,6,7,8* delims=;" %%a in (%filename%) do (
SET tempstr=%%a;%%b;%%c;%%d;%%e;%%f;%%g;
SET reportname=%%a
echo Serching !reportname! in %grspath% ...
cd %grspath%
FOR /F "tokens=*" %%N IN ('dir /b /A:-D /s %reportname%') DO (
IF NOT "%reppath%"=="" (SET reppath=%reppath%,%%N) ELSE (SET reppath=%%N)
)
cd %currentdir%
echo Found: %reppath%
)
请你看看这个,也许你会发现错误?
我的输入:
E:\tmp>(
SET tempstr=art.rep;1;2;3;4;5;6;7
SET reportname=art.rep
echo Serching !reportname! in e:\Sources\ ...
cd e:\Sources\
FOR /F "tokens=*" %N IN ('dir /b /A:-D /s ""') DO (IF NOT """" == "" (SET reppa
th="",%N ) ELSE (SET reppath=%N ) )
cd E:\tmp
echo Found: ""
)
然后可能会出现如下行:
E:\Sources\>(IF NOT """" == "" (SET reppath="",E:\Sources\somefolders\somefile.someext ) ELSE (SET reppath=E:\Sources\somefolders\somefile.someext ) )
Found: ""
答案 0 :(得分:0)
解决。 %替换为!在内部块:
cd %grspath%
FOR /F "tokens=*" %%N IN ('dir /b /A:-D /s !reportname!') DO (
IF NOT !reppath!==!! (
SET reppath=!reppath!;%%N
echo !reppath! - new one !!!
) ELSE (
SET reppath=%%N
echo !reppath! - new one !!!
)
)
cd %currentdir%
echo !reppath! - new one !!!!
IF NOT !reppath!==!! SET tempstr=!tempstr!;!reppath!
echo !tempstr!>>%tmpfilename%