Simple batch file driving me nuts

  1. Ztruker's Avatar
    Posts : 8,817
    Windows 10 Pro X64 1903 18362.295
       #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 ComputersSystem Spec

  2. AndreTen's Avatar
    Posts : 18,175
    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 ComputersSystem Spec

  3. Ztruker's Avatar
    Posts : 8,817
    Windows 10 Pro X64 1903 18362.295
    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 ComputersSystem Spec

  4. FreeBooter's Avatar
    Posts : 1,866
    Windows 10 Pro 64-bit
       #4

    Hi @Ztruker

    Problem is IF EXIST instead of if exists.
      My ComputerSystem Spec

  5. Ztruker's Avatar
    Posts : 8,817
    Windows 10 Pro X64 1903 18362.295
    Thread Starter
       #5

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

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


    Thank you.
      My ComputersSystem Spec

  6. FreeBooter's Avatar
    Posts : 1,866
    Windows 10 Pro 64-bit
       #6

    You are very welcome!
      My ComputerSystem Spec



 

Related Threads
TiWorker.exe driving me nuts - disc usage in Performance & Maintenance
TiWorker keeps chewing up my HDD today. It keeps popping up in my resource monitor, causing my HDD to make a ton of racket (it's a loud WD black drive). In task manager, I right-click on it and select low priority, but then a few minutes later when...
Windows updates driving me NUTS! in Windows Updates and Activation
It seems like lately, that every time Windows does a patch/upgrade, it wipes out my Edge favorites! This has happened 3 times in the last two weeks! Also, I suddenly cannot download anything from the Windows store either. Ready to pull my hair out!...
Hello all, Yes, unfortunately another thread about windows 10's wonderful temp profile. I thought i fixed it last week with the changing of the .bak in registry, but no. And it's not just one profile, every local profile i have does it, even the...
yes, i need help! random bsod driving me nuts in BSOD Crashes and Debugging
hi i tried interpreting these dump files myself to work out my woes, but no, no, no...i give up and am not ashamed to ask for help! there are 30 dmp files, but i don't want to bother someone to go through them all, but i suppose there might be...
Hello and thank you for taking the time to read my post! Please see attached the Minidumps that were collected after running dm_log_collector.exe Computer specs I expect should be visible in my profile. In case I missed some crucial information,...
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 08:40.
Find Us