Help with copying file to \sub\subdirectory

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
machone
Posts: 31
Joined: 01 Oct 2012 18:36

Help with copying file to \sub\subdirectory

#1 Post by machone » 16 Jul 2015 06:41

I've got a series of folders with a TXT file sitting in the top level folder, like so:

toplevel\
toplevel\info.txt
toplevel\subdir-01\
toplevel\subdir-02\
toplevel\subdir-03\

I need to do 2 things.. The first is to create a 'readme' folder in each of the subdirs, and then copy info.txt from \toplevel\ to the 'readme' subdir.. so the end result would be:

toplevel\
toplevel\info.txt
toplevel\subdir-01\readme\info.txt
toplevel\subdir-02\readme\info.txt
toplevel\subdir-03\readme\info.txt

I tried chaining two scripts I already have into one, and it creates the 'readme' subdir, but fails on the file copying part, apparently it doesn't travel through the subdirs properly:

Code: Select all

@echo off
setLocal EnableDelayedExpansion
pushd d:\files
for /f "tokens=* delims= " %%a in ('dir/b/ad') do (
mkdir "%%a\readme"
)
for /f "delims=" %%a in ( ' dir "*.txt" /b /s /a-d ' ) do (
echo processing "%%a"
set "name=%%~nxa"
pushd "%%~dpa"
copy /b /y "%%a" ".\readme\!name:~0!"
popd
)


Thanks in advance for any help with this!

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Help with copying file to \sub\subdirectory

#2 Post by Yury » 16 Jul 2015 13:09

machone wrote:I need to do 2 things.. The first is to create a 'readme' folder in each of the subdirs, and then copy info.txt from \toplevel\ to the 'readme' subdir.. so the end result would be:

toplevel\
toplevel\info.txt
toplevel\subdir-01\readme\info.txt
toplevel\subdir-02\readme\info.txt
toplevel\subdir-03\readme\info.txt




Code: Select all

@echo off
pushd "d:\files"|| exit
for /f "delims=" %%a in ('dir/ad/b') do (
 xcopy/y "info.txt" "%%a\readme\"
)
popd

machone
Posts: 31
Joined: 01 Oct 2012 18:36

Re: Help with copying file to \sub\subdirectory

#3 Post by machone » 17 Jul 2015 07:48

Thanks Yury! I had to modify it to the following to get it to work properly. Your version immediately closed the command prompt without creating the subfolder or copying the TXT file...

Code: Select all

@echo off
pushd "d:\files"
for /f "delims=" %%a in ('dir/ad/b') do (
 xcopy/y "*.txt" "%%a\readme\"
)

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Help with copying file to \sub\subdirectory

#4 Post by Squashman » 17 Jul 2015 08:09

Should not have had to remove the conditional execution to get it to create the directories. It will only exit if that path does not exist.

machone
Posts: 31
Joined: 01 Oct 2012 18:36

Re: Help with copying file to \sub\subdirectory

#5 Post by machone » 17 Jul 2015 10:30

You're right, that folder doesn't exist. That was the cause.

Post Reply