Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 842
  • Last Modified:

Copy one file to multiple directories w/ batch file

Greetings Experts.  I have a spreadsheet that needs to be copied to a set of top level folders but not the sub-folders.  Having most of my experience in scripting coming from batch files, please aim your response in that direction.

file A needs to go to folders A-Z:

FolderA [yes]
--sub folderA [no]
Folder B [yes]
--sub folderB [no]

I asked a similar question before on here but can't find it nor the solution.  I appreciate your time and help with this one!!
0
samiam41
Asked:
samiam41
  • 2
  • 2
1 Solution
 
Thomas GrassiSystems AdministratorCommented:
@echo off
CD \
cd \to folder where your spreadsheet is
copy  spreadsheet.xls c:\foldera\spreadsheet.xls
copy  spreadsheet.xls c:\folderb\spreadsheet.xlscopy  spreadsheet.xls c:\folderc\spreadsheet.xls
etc
etc
etc
copy  spreadsheet.xls c:\folderz\spreadsheet.xls
EXIT
0
 
lharrison26Commented:
example:

Need K:\NewCustomers\NewPartNumber.Bat to go into K:\NewCustomers\Customer Name\
 but not any subfolder of \Customer Name\, there being 200-300 "Customer Name" folders.

Answer:

Using for /d to loop through folders in a non-recursive fashion is the way to go

for /d %%a in ("K:\NewCustomers\*") do copy "K:\NewCustomers\NewPartNumber.bat" "%%a"

 Alternatively, you can use a for /f loop in combination with dir:

@echo off
pushd "K:\NewCustomers"
for /f "tokens=*" %%a in ('dir /A:D /B') do copy "NewPartNumber.bat" "%%a"
popd

 Personally, I prefer the first method more, though.
0
 
samiam41Author Commented:
Thanks Experts.  Testing now.
0
 
samiam41Author Commented:
One line of code and the task was finished.  I appreciate everyone's help and look forward to working with you all again soon.  Thanks.
0
 
lharrison26Commented:
Glad to help.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now