0

我正在尝试编写脚本,该脚本将从 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: ""
4

1 回答 1

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%
于 2012-04-19T13:43:21.437 回答