0

I am trying to run a batch file against a list of remote computers.

The batch file will check for a specific file, if the file is not found in the directory it should be copied there.

I am using the %%a variable for the PC names.

The xcopy command fails in the batch file with invalid drive specification.

If I take the xcopy command and replace the variable with the computer name and run from a command prompt it will copy the file over.

I am not sure why the xcopy command fails in the batch file when using %%a

PC.txt is my text file with the list of computers.

I am using the hostname and not the FQDN in the text file

    for /f %%a in (PCList.txt) do (
        If Exist "\\%%a\c$\Program Files\Folder\App.exe" (
            ECHO %%a App.exe exists
        ) ELSE (
           ECHO %%a App.exe Does Not Exist
           xcopy "C:\Folder\App.exe" "\\%%a\c$\Program Files\SubFolder\" /Q /R /Y
    
2
  • Is here a final ) on that? Commented Apr 23, 2021 at 21:25
  • 1
    There should be at least two closing parentheses, @ChuckWalbourn! I would also would like to know based upon those leading spaces zimboy, is that code also nested within another parenthesized block? Commented Apr 23, 2021 at 21:44

1 Answer 1

0

First of all, it looks like you have one or two syntax errors. You opened a parenthesis in ... Folder\App.exe" (and you haven't closed it since, the same thing with the ) ELSE (. Also, sometimes the parenthesis inside a FOR can cause interference with the rest of the code. If you add the missing parenthesis does not resolve, try to fix the code to: Folder\App.exe" ^( which can help.

Try this:

for /f %%a in (PCList.txt) do (
        If Exist "\\%%a\c$\Program Files\Folder\App.exe" ECHO %%a App.exe exists
        ) ELSE (
           ECHO %%a App.exe Does Not Exist
           xcopy "C:\Folder\App.exe" "\\%%a\c$\Program Files\SubFolder\" /Q /R /Y )
    

Also, you can use the COPY only, maybe the problem is with the COPY. And using the parameter if exist to a entire .exe isn't the best idea, the user can just create a txt file and rename it as App.exe.

Hope this helps,
K.

Sign up to request clarification or add additional context in comments.

4 Comments

I am sorry for my delay in responding but work related issues took precedence. I appreciate the comments and I tried all of the options and I am still not able to copy the file over, as I am still getting the error message invalid drive specification.
@zimboy ...It means that you are not using a valid drive letter, probably, so you just need to look at the paths...
I took another look at the paths and if I run the xcopy command from from a command prompt and replace the %%a with the computer name the xcopy command completes successfully. But in the batch file with %%a the xcopy commands errors out. But it does work to check for the file existence
@zimboy Try to use echo %%a and echo (the path, copy & paste) like `echo \\%%a\c$\Program Files\SubFolder`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.