Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
haggy
- Posts: 11
- Joined: 24 May 2018 03:39
#1
Post
by haggy » 24 May 2018 04:03
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/*" ../
))
Last edited by
Squashman on 24 May 2018 10:34, edited 1 time in total.
Reason: MOD EDIT: Strange that you knew to use a BB tag for italics but did not know there was one for CODE.
-
pieh-ejdsch
- Posts: 240
- Joined: 04 Mar 2014 11:14
- Location: germany
#2
Post
by pieh-ejdsch » 25 May 2018 04:11
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
-
haggy
- Posts: 11
- Joined: 24 May 2018 03:39
#3
Post
by haggy » 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." 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
Last edited by
Squashman on 25 May 2018 06:08, edited 1 time in total.
Reason: MOD EDIT: Please use [code][/code] tags.
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#4
Post
by Squashman » 25 May 2018 06:06
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?
-
haggy
- Posts: 11
- Joined: 24 May 2018 03:39
#5
Post
by haggy » 25 May 2018 14:44
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.
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#6
Post
by Squashman » 25 May 2018 14:58
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.
-
haggy
- Posts: 11
- Joined: 24 May 2018 03:39
#7
Post
by haggy » 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.
-
haggy
- Posts: 11
- Joined: 24 May 2018 03:39
#8
Post
by haggy » 25 May 2018 16:15
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