Solved Command Prompt Copy/XCOPY

MarcChamberlain

New member
Local time
2:17 AM
Messages
5
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

Computer type
PC/Desktop
OS
Windows 7 Professional x64
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

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Antec desktop; Acer Aspire laptops
OS
Windows 7 Professional 64-bit
CPU
Desktop i5; Acers i5 & i7
Memory
desktop 16GB; 1 Acer 8GB & 1 Acer 16GB
Hard Drives
1TB split into 2 equal partitions [OS and data] usable by RJS
Internet Speed
AT&T DSL
Browser
FF, GChrome, msIE
Other Info
Windows 7 Firewall, Emsisoft AM/AV, MSE [scan-only], SpywareBlaster, Ruiware/BillP combine
Hi Roland,

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

My Computer

Computer type
PC/Desktop
OS
Windows 7 Professional x64
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

Computer type
PC/Desktop
OS
win 8 32 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 Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Antec desktop; Acer Aspire laptops
OS
Windows 7 Professional 64-bit
CPU
Desktop i5; Acers i5 & i7
Memory
desktop 16GB; 1 Acer 8GB & 1 Acer 16GB
Hard Drives
1TB split into 2 equal partitions [OS and data] usable by RJS
Internet Speed
AT&T DSL
Browser
FF, GChrome, msIE
Other Info
Windows 7 Firewall, Emsisoft AM/AV, MSE [scan-only], SpywareBlaster, Ruiware/BillP combine
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

Computer type
PC/Desktop
Computer Manufacturer/Model Number
diy
OS
Win7 pro x64
CPU
stock i7 7700k
Motherboard
Gigabyte Z270N-WIFI mini-ITX
Memory
Corsair Vengeance LPX 16GB (2x8GB) DDR4 @ 3200MHz
Graphics Card(s)
integrated Intel HD 630
Sound Card
onboard Realtek ALC1220
Monitor(s) Displays
two vertically mounted samsung 55" 4k un55mu8000
Screen Resolution
1920x1280
Hard Drives
256GB Samsung EVO 960 M.2 pci-e NVMe SSD
PSU
SilverStone Nightjar ST45NF 450Watt Fanless
Case
No case. Motherboard is mounted directly onto power supply
Cooling
Evercool low profile 815EP with Panaflow 12L fan at 7v
Keyboard
Ortek MCK-86 mini
Mouse
Belkin 5-button USB
Internet Speed
spectrum 400mbps
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

Computer type
PC/Desktop
OS
Windows 7 Professional x64
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

Computer type
Laptop
Computer Manufacturer/Model Number
Asus K52F or Lenovo B51-80
OS
Win 7 x64 Home Premium (and x86 VirtualBox VM)/Win10
CPU
i3 370M/i7 6500U
Motherboard
Asus/Lenovo
Memory
8GB - finally :)/8GB
Graphics Card(s)
it's an i3, dude!/dual Intel&nVidia
Sound Card
onboard
Monitor(s) Displays
15.6" built-in
Screen Resolution
1366x768/1920x1080
Hard Drives
750GB Seagate internal
Sundry external drives attached to other computers on the local network
1TB SSD on the Lenovo
PSU
n/a
Internet Speed
as much as I can get - usually on a dongle/phone, so <1MB/s
Antivirus
MSE/Defender
Browser
IE11/12/Edge/Chrome/FF(if I must)
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

Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
Youse guys are aok! I've never done stuff like that! :)
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Antec desktop; Acer Aspire laptops
OS
Windows 7 Professional 64-bit
CPU
Desktop i5; Acers i5 & i7
Memory
desktop 16GB; 1 Acer 8GB & 1 Acer 16GB
Hard Drives
1TB split into 2 equal partitions [OS and data] usable by RJS
Internet Speed
AT&T DSL
Browser
FF, GChrome, msIE
Other Info
Windows 7 Firewall, Emsisoft AM/AV, MSE [scan-only], SpywareBlaster, Ruiware/BillP combine
Pyprohly - You are the MAN, this worked like a treat!!!!

Thank you so much for everyone's input of this, I'm really happy with the help.
 

My Computer

Computer type
PC/Desktop
OS
Windows 7 Professional x64
Back
Top