Search code examples
batch-filefindstr

Find files with spaces in findstr


I have this batch code and it works ONLY when the files do not have space in the name. What can I do to also find the files that include spaces in the name?

@echo off
echo.open localhost >file.tmp
echo.user user1 user1 >>file.tmp
echo.dir >>file.tmp
echo.bye >>file.tmp

FTP -n <file.tmp >output.tmp
Del file.tmp

echo.open localhost >file.tmp
echo.user user1 user1 >>file.tmp
FOR %%A IN (*.*) DO call :Existe %%A
echo.bye >>file.tmp

FTP -n <file.tmp >nul
DEL file.tmp
DEL output.tmp
GOTO :EOF

:Existe
    Findstr /I %1 output.tmp > nul
    IF NOT %ErrorLevel% == 0 ECHO.put %1 >>file.tmp

I also do that, but it does not work

:Existe
    Findstr /I /c:"%1" output.tmp > nul
    IF NOT %ErrorLevel% == 0 ECHO.put "%1" >>file.tmp

Solution

    • If the batch is in the current dir it will also transfer itself and file.tmp containing login data.
    • findstr /G option can process the check for existence more efficiently against a dir of the current folder.
    • To exclude the batch itself from transfer insert an additional ^|find /V "%0"

    @echo off
    Set "Host=localhost" & If "%~1" neq "" Set "Host=%~1"
    Set "User=user1"     & If "%~2" neq "" Set "User=%~2"
    Set "Pass=user1"     & If "%~3" neq "" Set "Pass=%~3"
    
    (echo.open %Host%
     echo.user %User% %Pass%
     echo.dir
     echo.bye
    ) >file.tmp
    
    FTP -n <file.tmp >output.tmp
    Del file.tmp
    
    (echo.open %Host%
     echo.user %User% %Pass%
     FOR %%A IN (*.*) DO call :Existe "%%A"
     echo.bye
    ) >file.tmp
    
    FTP -n <file.tmp >ftp.log
    DEL file.tmp
    DEL output.tmp
    
    GOTO :EOF
    
    :Existe
    Echo:%~1|Findstr /I "\.tmp$ \.cmd$">NUL 2>&1 && Goto :Eof
    Findstr /I "%~1" output.tmp >nul 2>&1 ||ECHO.put "%~1"