Page 1 of 1
How do i move files from series of folders
Posted: 24 May 2018 04:03
by haggy
Hi All,
I've got series of folders in a folder(Root) and in these folders exist one file each. I'm trying to move each file from thier corresponding folders to the main root folder(Root). I have tried this but not yeilding.
Code: Select all
@echo off
rem For each folder in your folders
for /R %%~dp0 %%i in (.) do (
cd "%%~dp/%%i"
if "%%~xa" NEQ "" if "%%~dpxi" NEQ "%~dpx0" (
rem Move the file to directory
move /y "%%i/*" ../
))
Re: How do i move files from series of folders
Posted: 25 May 2018 04:11
by pieh-ejdsch
You mean so?
Code: Select all
PushD D:\rootfolder
For /r %%i in (*) do if "%%~fi" neq "%~f0" if NOT exist %%~nxi move "%%i"
Popd
Phil
Re: How do i move files from series of folders
Posted: 25 May 2018 04:36
by haggy
Thank you phil for this though it doesn't work for me. it throws error "%%i was unexpected at this time." and secondly i want a scenario where i don't have an fore knowledge what the root-folder is. can i do?
Code: Select all
PushD ..
For /r %%i in (*) do if "%%~fi" neq "%~f0" if NOT exist %%~nxi move "%%i"
Popd
Re: How do i move files from series of folders
Posted: 25 May 2018 06:06
by Squashman
haggy wrote: ↑25 May 2018 04:36
Thank you phil for this though it doesn't work for me. it throws error "%%i was unexpected at this time."
You ran it from the command line or a batch file?
haggy wrote: ↑25 May 2018 04:36
and secondly i want a scenario where i don't have an fore knowledge what the root-folder is.
So give us some type of technical information on how we can determine what your root folder is? Are you running the batch file from the root folder?
Re: How do i move files from series of folders
Posted: 25 May 2018 14:44
by haggy
Yes . i ran it from command line.
I actually intend it to be a generic script regardless of wherever i'm running it from. it should be able to access each folder and move out the file to the upper folder.
Re: How do i move files from series of folders
Posted: 25 May 2018 14:58
by Squashman
Read the first 8 lines of the help file for the FOR command.
You failed to answer my question on how to determine what the root folder is. Please describe your requirements for that.
Re: How do i move files from series of folders
Posted: 25 May 2018 15:24
by haggy
By root folder i mean the upper folder. Take this scenario:
mkdir a
cd a
mkdir b
cd b
move txtfile ../
"Dir a is the upper folder" I need the to enter a folder and move the file to the upper folder. Thanks.
Re: How do i move files from series of folders
Posted: 25 May 2018 16:15
by haggy
haggy wrote: ↑25 May 2018 15:24
By root folder i mean the upper folder. Take this scenario:
mkdir a
cd a
mkdir b
cd b
move txtfile ../
"Dir a is the upper folder" I need the to enter a folder and move the file to the upper folder. Thanks.
Directory "A" is the upper. All i want to achieve is enter the folders and move out the file to upper folder