move files down one sub-folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
grc
Posts: 5
Joined: 15 Aug 2013 13:58

move files down one sub-folder

#1 Post by grc » 15 Aug 2013 14:04

Hi,
I am trying to figure out how to move a file into a pre-existing sub-folder, recursively, using a batch file(s) on a windows 7 machine.
example:
I have a root folder, with a bunch of subfolders. each sub-folder also has subfolders within it as well as a file "file.ext".
"file.ext" contains different data in each instance.
I want to move each "file.ext" into the first (or all if easier) sub-folder of the folder in which it resides.
The target sub-folders all have different names.
I hope my explanation is clear, I would certainly appreciate any help.

Thanks in advance.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: move files down one sub-folder

#2 Post by foxidrive » 15 Aug 2013 21:17

grc wrote:I want to move each "file.ext" into the first (or all if easier) sub-folder of the folder in which it resides.


This is not clear.

c:\root\a\b\c\d\file.ext

where does file.ext go?

What about the filenames - if they are all the same filename then renaming has to occur.

grc
Posts: 5
Joined: 15 Aug 2013 13:58

Re: move files down one sub-folder

#3 Post by grc » 15 Aug 2013 21:46

foxidrive, thanks for the reply. Sorry , let me try to clarify:

c:\root\a\a1
c:\root\a\a2
c:\root\a\a3
c:\root\a\file.ext

c:\root\b\b1
c:\root\b\b2
c:\root\b\b3
c:\root\b\file.ext

file.ext from c:\root\a goes into a1 ( or a1,a2 and a3 if that is easier )

file.ext from c:\root\b goes into b1 ( or b1,b2 and b3 )

and so on for all sub-directories of root.

Hope this is clearer.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: move files down one sub-folder

#4 Post by foxidrive » 15 Aug 2013 22:15

grc wrote:c:\root\a\a1
c:\root\a\a2
c:\root\a\a3
c:\root\a\file.ext

c:\root\b\b1
c:\root\b\b2
c:\root\b\b3
c:\root\b\file.ext

and so on for all sub-directories of root.


c:\root\b\b1
c:\root\b\file.ext

So file.ext is to move to b1. You indicate that all folder levels have a file.ext so b1 is already going to have one there.

grc
Posts: 5
Joined: 15 Aug 2013 13:58

Re: move files down one sub-folder

#5 Post by grc » 15 Aug 2013 22:26

file.ext currently resides only in each first sub-folder of \root.
ie in \root\a & in \root\b
I want to move it down one level, to
\root\a\a1 & \root\b\b1

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: move files down one sub-folder

#6 Post by foxidrive » 15 Aug 2013 23:34

This might work. Test it on some sample files and folders first.

Code: Select all

@echo off
cd /d "root"
for /f "delims=" %%a in (' dir /b /ad ') do (
   if exist "%%a\file.ext" (
      for /f "delims=" %%b in (' dir "%%a" /b /ad ') do if exist "%%a\file.ext" if not exist "%%a\%%b\file.ext" move "%%a\file.ext" "%%a\%%b" >nul
   )
)


EDITED: I had a "" where it should have been "

grc
Posts: 5
Joined: 15 Aug 2013 13:58

Re: move files down one sub-folder

#7 Post by grc » 15 Aug 2013 23:48

Returns the error "the syntax of the command is incorrect"
Is this caused by nested "for" command?
As I stated earlier, my batch experience is limited so I appreciate the help and am trying to learn from your posts.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: move files down one sub-folder

#8 Post by foxidrive » 15 Aug 2013 23:54

Try it now. I tested it here and I found a typo.

grc
Posts: 5
Joined: 15 Aug 2013 13:58

Re: move files down one sub-folder

#9 Post by grc » 16 Aug 2013 00:09

Thank you.
It works perfectly, now I plan to try breaking it down to understand it.

Thanks again :D

Post Reply