Command Prompt Copy/XCOPY

Page 1 of 2 12 LastLast

  1. Posts : 5
    Windows 7 Professional x64
       #1

    Command Prompt Copy/XCOPY


    Hi All

    I want to start off saying I'm new to command prompt and scripting so please be nice :)

    Want I am trying to do it every time I run a script it copies 1 file from the same source folder to a folder with subdirectories in it, I need the file to be copied to every subdirectory.

    Basically, the destination folder will over time have more sub-directories added and I need to always be able to run this script and it will just add it to the new directories also.

    Any help? I have heard XCOPY can but I'm unsure on understanding the swicthes
      My Computer


  2. Posts : 2,774
    Windows 7 Professional 64-bit
       #2

    IMO, the best long-term approach is a DOS command box batchfile or a powershell script. I only know about DOS command box BATchfile operations. Which would you prefer?
      My Computer


  3. Posts : 5
    Windows 7 Professional x64
    Thread Starter
       #3

    Hi Roland,

    At the moment i can current using DOS command (Windows 7 .bat file)
      My Computer


  4. Posts : 3,785
    win 8 32 bit
       #4

    A bat file is old MS-DOS use . CMD as that's what your running not command.com. built in robocopy has lots of options but why are you doing this?
      My Computer


  5. Posts : 2,774
    Windows 7 Professional 64-bit
       #5

    In short, my "hard-wired" batchfile would "sniff" with IF EXIST (IF NOT EXIST - EXIT: etc etc etc) source file before copying said file into whatever folders are present at the time of "hard-wired" batchfile's execution. If more folders are added, the batchfile must be updated. I know nothing about programming a powershell script - I suspect it too will have to be upated when new folders are added.
    Now, there are some DOS batchfile writers that can write a batchfile that can "grow" with the folders growth, I have no idea on how to do that.
      My Computer


  6. Posts : 1,363
    Win7 pro x64
       #6

    say you have a directory c:\main that has an indeterminant number of subdirectories beneath it. The first line of your batch file could be:

    dir c:\main\ /a:d /b >t.t

    this creates a file named t.t that contains nothing but a list of the subdirectories beneath main. From there, your batch file would need to parse the contents of t.t line by line and perform an xcopy operation that functions like this.

    set x=1
    :begin
    read line x from t.t (edit: you gotta figure out how to parse the file)
    xcopy filename.txt c:\main\line1\filename.txt /y
    set x=x+1
    goto :begin

    and repeat that until you come to the end of t.t
      My Computer


  7. Posts : 5
    Windows 7 Professional x64
    Thread Starter
       #7

    Hi John

    Thanks for getting back to me, I have managed to do the output file which is probably the easiest thing to do :) but I am still having issues, if I show you what I'm trying to do then maybe you can assist and I will make more sense to me:

    I have a batch file with the below content

    ---------------------------
    xcopy /s/y "\\tmt-svr-002\blackbelt\master xml files" "C:\ProgramData\BlackBelt SmartPhone Defence\BlackBelt Analyst\meconfigure"


    del /f "%USERPROFILE%\Desktop\Analyst.lnk"
    del /f "C:\Users\Public\Desktop\Analyst.lnk"

    start /d "C:\Program Files (x86)\BlackBelt SmartPhone Defence\BlackBelt Analyst" Analyst.exe
    start /d "C:\Program Files\BlackBelt SmartPhone Defence\BlackBelt Analyst" Analyst.exe
    ----------------------------------

    This will copy files from source to destination, delete icons from public and user desktop - then start program from program directory
    ---------------------------------

    I need to add another command in the batch file which will add a file from source to destination which the destination has subfolders (more will be added when an update to the software (this creates a new folder per update)
    ---------------------------------
    dir "C:\ProgramData\BlackBelt SmartPhone Defence\BlackBelt Analyst" /a:d /b > "C:\ProgramData\BlackBelt SmartPhone Defence\BlackBelt Analyst\List.txt"

    Below shows the content from the list.txt

    2.2.0027
    2.2.0033
    2.2.0037
    2.2.0044
    2.2.0045
    GridSettings
    meconfigure
    ResultData
    TestConfig

    I need to copy a file into each directory each time the batch file is ran it will do the same (if file exists just overwrite)
      My Computer


  8. Posts : 21,482
    Win 7 x64 Home Premium (and x86 VirtualBox VM)/Win10
       #8

    you can do that with a loop - which doesn't require any known names at all.

    I know I'm going to get shot down for this, but I'm most familiar for/next loops

    What you want is something that does this...


    For each [subfolder] in [base folder]
    copy [sourcefile] [subfolder]
    next [subfolder]

    you'll need to add switches to the copy line to either force over-writes or prevent errors if the file exists already. This should work with COPY, XCOPY, and ROBOCOPY (check the switches in robocopy - its' a long time since I used it, and it may even be possible to do the whole thing simply using the correct switches - robocopy /? )

    Now that should be easy enough to write in Command Line, and also in virtually any scripting language - one possible problem may be to stop it recursing down any further levels in the subfolders.
      My Computer


  9. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #9

    MarcChamberlain, if I understood correctly you basically want to copy a single file to each subfolder in a folder, non-recursively.

    Here’s a way to do it.
    Code:
    @echo off
    
    set "source_file=bacon.txt"
    set "destination_folder=grill"
    
    for /f "delims=" %%J in (' dir "%destination_folder%" /a:d /b ') do (
    	copy "%source_file%" "%destination_folder%\%%~J"
    )
    The copy command accepts UNC paths. Duplicates will be overwritten.

    You’ll probably want to add checks to see if the source and destination are available, and also test if the remote host is active.
      My Computer


  10. Posts : 2,774
    Windows 7 Professional 64-bit
       #10

    Youse guys are aok! I've never done stuff like that! :)
      My Computer


 
Page 1 of 2 12 LastLast

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

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