Simple batch file driving me nuts


  1. Posts : 14,046
    Windows 11 Pro X64 22H2 22621.1848
       #1

    Simple batch file driving me nuts


    All I want to do is check if a folder exists on a network share and if it does, copy a file to it. Should be a piece of cake. Here is my code:

    Code:
    @echo on                                                                                                                
    set "newFile=Just a test file.txt"                                                                                      
    set "hostTarget=\\Kfwserver\HP_D\ShowkeyPlus Data\"                                                                                                                                                                                             
    if exists "%hostTarget%" copy "%userprofile%\Documents\%newfile%" "%hostTarget%"
    else echo "%hostTarget%" not found.
    When I run it I get this:

    Code:
    >test
    
    C:\Users\ztruk>set "newFile=Just a test file.txt"
    
    C:\Users\ztruk>set "hostTarget=\\Kfwserver\HP_D\ShowkeyPlus Data\"
    "\\Kfwserver\HP_D\ShowkeyPlus Data\" was unexpected at this time.
    C:\Users\ztruk>if exists "\\Kfwserver\HP_D\ShowkeyPlus Data\" copy "C:\Users\ztruk\Documents\Just a test file.txt" "\\Kfwserver\HP_D\ShowkeyPlus Data\"
    If I have just the first two lines all is well, so it's the third line that is causing the problems but I cana't figure out what is wrong.
      My Computers


  2. Posts : 30,588
    Windows 10 (Pro and Insider Pro)
       #2

    Check position of quotes.
    think this should work.. instead
    set "newFile=Just a test file.txt"
    set "hostTarget=\\Kfwserver\HP_D\ShowkeyPlus Data"
    set them here
    set newFile="Just a test file.txt"
    set hostTarget="\\Kfwserver\HP_D\ShowkeyPlus Data"
      My Computers


  3. Posts : 14,046
    Windows 11 Pro X64 22H2 22621.1848
    Thread Starter
       #3

    Nope, that's how I started. If you do that you assign the quotes to the variable.

    set newFile="Just a test file.txt"

    echo %newFile% --> "Just a test file.txt"

    Placing the quote before the variable name you get

    set "newFile=Just a test file.txt"

    echo %newFile% --> Just a test file.txt

    Thanks though
      My Computers


  4. Posts : 5,325
    Windows 11 Pro 64-bit
       #4

    Hi @Ztruker

    Problem is IF EXIST instead of if exists.
      My Computer


  5. Posts : 14,046
    Windows 11 Pro X64 22H2 22621.1848
    Thread Starter
       #5

    OH CRAP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    I spent about 5 hours with this and that was the solution..


    Thank you.
      My Computers


  6. Posts : 5,325
    Windows 11 Pro 64-bit
       #6

    You are very welcome!
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 04:11.
Find Us




Windows 10 Forums