Copy a certain file based on computer

Discussion in 'Scripting' started by hypedave, Mar 7, 2017.

  1. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    #1 hypedave, Mar 7, 2017
    Last edited by a moderator: Apr 20, 2017
    I have a folder with a 100 files in it. I have 100 PC's. The names match the first 6 characters of the computer. How can I copy each file to the matching computer based on it's first 6 characters of the computername? I want to use less code as possible

    Code:
    SET Name=%computername%
    SET FirstSixChars=%computername:~0,6%
    
    if /i "%Computername:~0,6%" EQU "123456" copy 123456.txt
    if /i "%Computername:~0,6%" EQU "789012" copy 789012.txt
    if /i "%Computername:~0,6%" EQU "345678" copy 345678.txt

    Any suggestion?
     
  2. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,069
    20,300
    340
    #2 s1ave77, Mar 7, 2017
    Last edited by a moderator: Apr 20, 2017
    Code:
    for /f %%a in ("%computername:~0,6%") do xcopy "%%a.txt" /s /q "Path\to\local\folder\" /y
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    #3 hypedave, Mar 7, 2017
    Last edited by a moderator: Apr 20, 2017
    (OP)
    I need to switch it up some due to the naming of the text file has changed.

    New txt file is

    123456-shdgs-config.txt
    789012-43563-config.txt
    345678-dghyt-config.txt

    Problem is the middle section of the file name will also be different.
     
  4. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,069
    20,300
    340
    How is the middle section created/generated :g:?

    Is it only 1 file per machine or could it be more?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10

    It's generated from the export settings in the application that creates the file. And yes it's one file per machine. The first six characters of the filename match the first six characters of the machine name. Your original command worked like a charm but I forgot to mention the middle section of the file.
     
  6. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,069
    20,300
    340
    #6 s1ave77, Mar 7, 2017
    Last edited by a moderator: Apr 20, 2017
    Code:
    for /f %%a in ("%computername:~0,6%") do for /r "Path\to\TXT\folder" %%G in (*.txt) do echo %%G | findstr /c:"%%a" 1>nul && xcopy "%%G" /s /q "Path\to\local\folder\" /y
    Where now %%a = first 6 chars of the computername and %%G is the path to the found file for the copy :cool2:.


    Slightly shortened:

    Code:
    for /r "Path\to\TXT\folder" %%G in (*.txt) do echo %%G | findstr /c:"%computername:~0,6%" 1>nul && xcopy "%%G" /s /q "Path\to\local\folder\" /y
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    #7 hypedave, Mar 7, 2017
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Where now %%a = first 6 chars of the computername and %%G is the path to the found file for the copy :cool2:.


    Works great but i need to disabled the output it's generating for files not matching the computer name.

    Code:
    @echo off
    for  /r ".\" %%G in (*.xml) do echo %%G | findstr /c:"%computername:~0,4%"   1>nul && xcopy "%%G" /s /q  "C:\ProgramData\MCS\NewtonPOS\Settings.xml" /y
    pause
    

    Slightly shortened:

    Code:
    for /r "Path\to\TXT\folder" %%G in (*.txt) do echo %%G | findstr  /c:"%computername:~0,6%" 1>nul && xcopy "%%G" /s /q  "Path\to\local\folder\" /y
    [/QUOTE]


    Works great but i need to disable the output of the loops for files not matching the computer name;

    Code:
    @echo off
    for /r ".\" %%G in (*.xml) do echo %%G | findstr /c:"%computername:~0,4%"  1>nul && xcopy "%%G" /s /q "C:\ProgramData\MCS\NewtonPOS\Settings.xml" /y
    pause
    
     
  8. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,069
    20,300
    340
    #8 s1ave77, Mar 7, 2017
    Last edited by a moderator: Apr 20, 2017
    Code:
    (for /r "Path\to\TXT\folder" %%G in (*.txt) do echo %%G | findstr /c:"%computername:~0,6%" 1>nul && xcopy "%%G" /s /q "Path\to\local\folder\" /y) >nul 2>&1
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    #9 hypedave, Mar 8, 2017
    Last edited by a moderator: Apr 20, 2017
    (OP)
    The path location of the install files will always be undetermined. I tried using %~dp0"
    When I run it locally it's fine. When I package it up and run on another machine it fails.

    Code:
    (for /r "Path\to\TXT\folder" %%G in (*.txt) do echo %%G | findstr  /c:"%computername:~0,6%" 1>nul && xcopy "%%G" /s /q  "Path\to\local\folder\" /y) >nul 2>&1
     
  10. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,069
    20,300
    340
    %cd% should work :g:. The /r loop recurses through all available subfolders.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    #11 hypedave, Mar 8, 2017
    Last edited by a moderator: Apr 20, 2017
    (OP)

    Yeah I placed %CD% in there

    Code:
    (for /r "%CD%" %%G in (*.txt) do echo %%G | findstr /c:"%computername:~0,6%" 1>nul && xcopy "%%G" /s /q "C:\FilePath" /y) >nul 2>&1