循环遍历postgresql文件会在Windows中出错

时间:2014-10-06 14:06:03

标签: postgresql batch-file batch-processing

我正在尝试读取文件并加载所有csv文件,一旦完成将文件移动到其他位置:

cd E:\data\
for /f %%a in (’dir /b filename*.CSV) do (
   psql -U postgre -W password -c "COPY INTO LA from %%a" zipcodes
   mv %%a E:\data\bc\
)

但是它给了我以下错误:

E:\data>for /F %a in (ΓÇÖdir /b filename*.CSV) do (
psql -U postgre -W password-c "COPY INTO LA from %a" zipcodes
 mv %a E:\data\bc\
)
The system cannot find the file ΓÇÖdir.

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "psql=c:\wherever\psqlIs\psql.exe"

    pushd "e:\data" && (
        for %%a in ("filename*.CSV") do (
           "%psql%" -U postgre -W password -c "COPY INTO LA from %%~a" zipcodes
           move "%%~a" "E:\data\bc\"
        )
        popd
    )

for /f用于文件/字符串处理。要迭代一组文件,请使用简单的for

无论如何,你的代码中的问题是开头的单引号(正如Alex K指出的那样),缺少的结束引用和窗口中不存在的mv命令应该是move

已编辑似乎文件加载存在问题。 psql copy命令表明最好使用完整路径名(反斜杠加倍),所以

@echo off
    setlocal enableextensions enabledelayedexpansion

    set "psql=c:\wherever\psqlIs\psql.exe"

    pushd "e:\data" && (
        for %%a in ("filename*.CSV") do (
            set "file=%%~fa"
           "%psql%" -U postgre -W password -c "COPY INTO LA from  E'!file:\=\\!'" zipcodes
           move "%%~a" "E:\data\bc\"
        )
        popd
    )