-1

I'm trying to use FOR loops to create a batch that checks if file exists in a directory. I'm struggling with files that has spaces with the file name and i don't want to have to use a .txt list if possible. So here is what i have tested so far.

@echo OFF
:: Variables
set Folder=D:\Test Folder
set Space=File Name With Space.txt
set NoSpace=FileNameWithoutSpace.txt

:: For Loop

FOR %%A in (%Space% %nospace%) do (
if exist "%Folder%\%%A" (
        ECHO %%A exist
        ) else (
            ECHO %%A doesn't exist
            echo. )
)

Here is what it reads when turned echo back on.

(if exist "D:\Test Folder\File" (ECHO File exist ) else ( ECHO File doesn't exist echo. ) )

And if i use this code:

@echo OFF    
:: Variables
set Folder=D:\Test Folder
set Space="File Name With Space.txt"
set NoSpace=FileNameWithoutSpace.txt

:: For Loop

FOR %%A in (%Space% %nospace%) do (

if exist "%Folder%\%%A" (
    ECHO %%A exist
    ) else (
        ECHO %%A doesn't exist
        echo. )
)

It reads the "IF" command as:

(if exist "D:\Test Folder\"File Name With Space.txt"" (ECHO "File Name With Space.txt" exist ) else ( ECHO "File Name With Space.txt" doesn't exist echo. ) )

What am I doing wrong?

Aaron
  • 57
  • 1
  • 8
  • Try `%~A` (read `set /?` or `cmd /?` for more details, I don't remember which) – user202729 Apr 20 '18 at 17:12
  • [Possible duplicate](https://stackoverflow.com/questions/5310550/for-loop-in-batch-file-fails-when-file-names-are-given-in-double-quotes?rq=1)? Or [this](https://stackoverflow.com/a/804782/5267751)? – user202729 Apr 20 '18 at 17:13
  • Oh, and BTW, saying "thanks" is [discouraged](https://meta.stackoverflow.com/questions/288160/no-thanks-damn-it) on [so] (similar to Wikipedia) – user202729 Apr 20 '18 at 17:15

2 Answers2

0

You have

set Space=File Name With Space.txt

so

FOR %%A in (%Space% %nospace%) do (

translates to

FOR %%A in (File Name With Space.txt FileNameWithoutSpace.txt) do (

which searches for the following files:

File
Name
With
Space.txt
FileNameWithoutSpace.txt

Quote your arguments:

FOR %%A in ("%Space%" "%nospace%") do (
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • I tried your suggestion, it reads the "FOR" command correctly, but in the "IF" command it reads: (if exist "D:\Test Folder\"File Name With Space.txt"" (ECHO "File Name With Space.txt" exist ) else ( ECHO "File Name With Space.txt" doesn't exist). – Aaron Apr 20 '18 at 17:26
  • `if exist "D:\Test Folder\"File Name With Space.txt""...`?? I think, you didn't understand the concept of quoting: `if exist "D:\Test Folder\File Name With Space.txt" ...` – Stephan Apr 20 '18 at 18:38
0

Aaron, this is the syntax you should be using, (pay special attention to the placement of doublequotes, " and the use of the metavariable modifier, ~):

@Echo Off    
Rem Variables
Set "Folder=D:\Test Folder"
Set "Space=File Name With Space.txt"
Set "NoSpace=FileNameWithoutSpace.txt"

Rem For Loop
For %%A In ("%Space%" "%nospace%") Do (
    If Exist "%Folder%\%%~A" (
        Echo %%~A exists
    ) Else (
        Echo %%~A does not exist
        Echo=
    )
)
Pause
Compo
  • 36,585
  • 5
  • 27
  • 39
  • That works perfectly! The echo returns result with the doublequotes though. How do i get rid of that. Can you suggest reading material about the use of ~? I'd like to understand how that works too. **_"File Name With Space.txt" exist_** is what i got. – Aaron Apr 20 '18 at 17:49
  • I asked you to pay attention to the **`~`** metavariable modifier. Looking at the output from `For /?` when ran at the Command prompt would reveal, `%~I - expands %I removing any surrounding quotes (")`. I've edited the code to match the request however. – Compo Apr 20 '18 at 17:58