move files down one sub-folder
Moderator: DosItHelp
move files down one sub-folder
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.
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.
Re: move files down one sub-folder
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.
Re: move files down one sub-folder
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.
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.
Re: move files down one sub-folder
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.
Re: move files down one sub-folder
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
ie in \root\a & in \root\b
I want to move it down one level, to
\root\a\a1 & \root\b\b1
Re: move files down one sub-folder
This might work. Test it on some sample files and folders first.
EDITED: I had a "" where it should have been "
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 "
Re: move files down one sub-folder
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.
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.
Re: move files down one sub-folder
Try it now. I tested it here and I found a typo.
Re: move files down one sub-folder
Thank you.
It works perfectly, now I plan to try breaking it down to understand it.
Thanks again
It works perfectly, now I plan to try breaking it down to understand it.
Thanks again
