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?
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.
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.
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 . 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
Where now %%a = first 6 chars of the computername and %%G is the path to the found file for the copy . 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
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
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
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