2

When running this script the following error message is output:

The syntax of command is incorrect

Folder is on a server and folder name has spaces.

@echo off

for  %%I in ("\\path31vm\file transfers\Data Files\Inbound\VMs\su\Reg\new\*.csv") do (
if not exist 
"\\path31vm\file transfers\Data Files\Inbound\VMs\su\Reg\new\CSV\%%~nI.csv.completed" 
xcopy "\\path31vm\file transfers\Data Files\Inbound\VMs\su\Reg\new\%%~nxI" "\\path31vm\file transfers\Data Files\Inbound\VMs\su\Reg\new\CSV"/D
)
Mofi
  • 46,139
  • 17
  • 80
  • 143
S B
  • 519
  • 1
  • 4
  • 10
  • The `{}` button will format the selected code. If the edit I've made is not what you actually have, please correct it using the `edit` facility below the post. The syntax error would be, if I've formatted the code as I believe you posted, that the target of the `if not exist` **must** be on the same physical line as the `if` keyword. If the `xcopy` is also on a separate line, then this also must be on the same physical line as the `if` **except** that you can have `if not exist ... (` on one line, the `xcopy` on the next and a `)` to match the extra `(` following. – Magoo Nov 14 '17 at 16:50
  • if i put the target of if not exist on the same physical line then it take transfers and data word in the path was key word(which is the part of folder name). They get light blue color. I have done the changes which you have but still giving same error. – S B Nov 14 '17 at 17:04

1 Answers1

0

Single line solution:

@for %%I in ("\\path31vm\file transfers\Data Files\Inbound\VMs\su\Reg\new\*.csv") do @if not exist "%%~dpICSV\%%~nxI.completed" %SystemRoot%\System32\xcopy.exe "%%I" "%%~dpICSV\" /C /D /Q /Y >nul 2>&1

Multi-line solution:

@echo off
for %%I in ("\\path31vm\file transfers\Data Files\Inbound\VMs\su\Reg\new\*.csv") do (
    if not exist "%%~dpICSV\%%~nxI.completed" (
        %SystemRoot%\System32\xcopy.exe "%%I" "%%~dpICSV\" /C /D /Q /Y >nul 2>&1
    )
)

Important here is the backslash at end of destination folder path because this is a clear indication for XCOPY that the target specifies a directory and not a file name. Otherwise XCOPY used here to copy a single file would prompt the user if target specifies a directory or file and waits for the input of the user. For details see batch file asks for file or folder.

For testing purposes with additional debugging information and without suppressing any standard or error output:

@echo off
for %%I in ("\\path31vm\file transfers\Data Files\Inbound\VMs\su\Reg\new\*.csv") do (
    echo CSV file is:  %%I
    if not exist "%%~dpICSV\%%~nxI.completed" (
        echo Not existing: %%~dpICSV\%%~nxI.completed
        %SystemRoot%\System32\xcopy.exe "%%I" "%%~dpICSV\" /C /D /Y
    ) else (
        echo Existing is:  %%~dpICSV\%%~nxI.completed
    )
)
pause

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • for /?
  • if /?
  • xcopy /?

Read also the Microsoft article about Using Command Redirection Operators.

Mofi
  • 46,139
  • 17
  • 80
  • 143