2012-04-19 71 views
0

我试图编写脚本,它将从csv文件(即每行第1列)读取第1个值,包含文件名。 然后在某个位置搜索此文件,并将每个找到的路径添加到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

解决。 %替换为!在内部块中:

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% 
相关问题