Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Command Prompt Copy/XCOPY

20 Apr 2017   #1
MarcChamberlain

Windows 7 Professional x64
 
 
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 System SpecsSystem Spec
.
20 Apr 2017   #2
RolandJS

Windows 7 Professional 64-bit
 
 

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 System SpecsSystem Spec
20 Apr 2017   #3
MarcChamberlain

Windows 7 Professional x64
 
 

Hi Roland,

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

20 Apr 2017   #4
samuria

win 8 32 bit
 
 

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 System SpecsSystem Spec
20 Apr 2017   #5
RolandJS

Windows 7 Professional 64-bit
 
 

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 System SpecsSystem Spec
20 Apr 2017   #6
johnhoh

Win7 pro x64
 
 

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 System SpecsSystem Spec
21 Apr 2017   #7
MarcChamberlain

Windows 7 Professional x64
 
 

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 System SpecsSystem Spec
21 Apr 2017   #8
NoelDP

Microsoft Community Contributor Award Recipient

Win 7 x64 Home Premium (and x86 VirtualBox VM)/Win10
 
 

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 System SpecsSystem Spec
21 Apr 2017   #9
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

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 System SpecsSystem Spec
21 Apr 2017   #10
RolandJS

Windows 7 Professional 64-bit
 
 

Youse guys are aok! I've never done stuff like that!
My System SpecsSystem Spec
Reply

 Command Prompt Copy/XCOPY




Thread Tools




Similar help and support threads
Thread Forum
Command Prompt: Repeat del command continuosly
Hi All, I'm running a program that parses .csv files and then changes them into ._OK files. This process takes longer when there are a large number of *.OK files in the folder where the files being converted are located. To remedy this I occasionally manually run this command: del *._OK ...
General Discussion
"Copy" command in command prompt generates 0x1a artifact?
Hello Everybody! I don't know if this is a real issue, or just one of those "What the . . . .!!" situations. You judge. Situation: I was working on a batch file that combines two .txt (ASCII?) files created in Notepad to create a composite file, and I noticed something really interesting. ...
Software
Meaning of Parameters in the XCOPY command
I'm trying to create bootable flash drives. At one point, I'm told to use the command, xcopy f:*.* /s/e/f hg: in which f: is the source, g: is the destination. Why am I told to type, "hg:"? What does the "h" do? When I enter the command, Windows 7 asks me if g: refers to a file or a...
General Discussion
Run administrative command prompt from command prompt
Ok So I got myself into a unique bind here. I was trying to install some custom system files (for a theme) and I did it wrong and now I cant open explorer.exe . I dont want to goto a backup because this is a easy fix. (in my view). So I need to know how to run command prompt with administrative...
General Discussion
Command Prompt - Copy to Clipboard
How to Enable Copy to Clipboard from Command Prompt in Windows 7 This will show you how to enable or disable Quick Edit mode in the command prompt to highlight text within the command prompt to copy to the clipboard in Windows 7.You can use this for a normal command prompt or elevated...
Tutorials


Our Sites

Site Links

About Us

Find 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 16:35.
Twitter Facebook Google+ Seven Forums iOS App Seven Forums Android App