Copy if not exist

Discussion in 'Scripting' started by Kamrul08, Apr 13, 2015.

  1. Kamrul08

    Kamrul08 MDL Member

    Dec 28, 2013
    104
    13
    10
    I made a batch file to copy fonts into windows fonts directory. It’s working on 32bit OS but windows 8.1 64bit. What’s the problem?

    @echo off
    set dest=%windir%\Fonts
    for %%K in (*.*) do if not exist "%dest%\%%~nxK" copy "%%K" "%dest%\%%~nxK"
     
  2. Compo

    Compo MDL Member

    Mar 29, 2011
    136
    106
    10
    Since the Windows directory is a protected area in that OS, you did 'Run as administrator', didn't you?
     
  3. Kamrul08

    Kamrul08 MDL Member

    Dec 28, 2013
    104
    13
    10
    I tried 'Run as administrator' mode. Its not working. Any suggestions?
     
  4. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    287
    10
    #4 wk-952, Apr 15, 2015
    Last edited by a moderator: Apr 20, 2017
    @Kamrul08

    The problem when you run the script as admin is that the active directory gets changed to: "C:\Windows\system32"
    or generally to "%windir%\system32",
    so the 'FOR loop' is actually looking into "C:\Windows\system32" for files to copy,
    and you can see that if you 'ECHO' "%%K" in the loop
    or 'ECHO' "%CD%" outside the loop.

    Anyway i guess you're trying to copy fonts from the same dir as the script to "%windir%\fonts", am i right?
    if so simply change the active/working directory by adding this line at the beginning of the script or just before the loop:
    Code:
    cd /d "%~dp0"
    
    another solution, in case you don't want to change the active directory for the rest of the script,
    or you want to change it only until the 'FOR' loop is finished
    add this line just before the loop:
    Code:
    pushd "%~dp0"
    
    then this line just after the loop:
    Code:
    popd
    
    then you can run the script as admin safely.
     
  5. Kamrul08

    Kamrul08 MDL Member

    Dec 28, 2013
    104
    13
    10
    #5 Kamrul08, Apr 15, 2015
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Yes, I’m trying to copy from same directory of my batch file to Windows\Fonts directory.

    Code:
     pushd "%~dp0" 
    I also used your pushd %~dp0 command. It shows files copied, but actually not. No copied files in the Windows\Fonts directory. The command is working with 32Bit or another directory in 64Bit.
    1.png
     
  6. Compo

    Compo MDL Member

    Mar 29, 2011
    136
    106
    10
    #6 Compo, Apr 15, 2015
    Last edited by a moderator: Apr 20, 2017
    If you're copying using the 'all files' wildcard from the same directory as the script then you will also be copying the script to the Fonts directory!

    Try this 'Run as administrator' instead!
    Code:
    @Echo Off
    SetLocal
    Set "dest=%SystemRoot%\Fonts"
    If "%~dp0" NEq "%CD%" (If "%~dp0" NEq "%CD%\" PushD %~dp0)
    RoboCopy "%CD%" "%dest%" *.fon *.otf *.pfm *.ttf /XC /XN /XO
    As you can see it selects only specific file types to copy, (please change those to suit your needs).

    Additional note: I'm not sure that copying files to that location actually installs them for use, so if that is your intent the you may need to completely change your script.
     
  7. user_hidden

    user_hidden MDL Expert

    Dec 18, 2007
    1,034
    1,061
    60
  8. Kamrul08

    Kamrul08 MDL Member

    Dec 28, 2013
    104
    13
    10
    Thanks all. Is there any difference between 32Bit and 64Bit command line? Especially for copy command? I used two commands in a batch file (added bellow). Command 1 doesn't work in 64Bit but 2 works fine. Difference is \ only.

    1. copy "%~dp0x86.exe" "%ProgramFiles%\VS Revo Group\Revo Uninstaller Pro"

    2. copy "%~dp0x86.exe" "%ProgramFiles%\VS Revo Group\Revo Uninstaller Pro\"