Move all files within subfolders to parent folder











up vote
0
down vote

favorite
1












Firstly, I'm aware there are tons of similar questions, and I know because I must have tried adapting at least 5 of them, but I'm still here asking because my crappy grasp of CMD (which I actually thought I was getting good at up until this point) is frustrating me with generic, unhelpful errors, and I can't understand what part of the syntax is wrong. This is likely due to the fact that other questions deal with the folder/filename structures of their respective OPs, and when I try to adapt the code to my own folder/file structure it refuses to work. Therefore, I'm here asking for a CMD for loop solution that'll actually work for my own file and folder structure.



I have a parent folder which contains many other folders, in which there are files:



Parent Folder
Subfolder
File1
File2
Subfolder
File1
File2
Subfolder
File1
File2


...and so on.



I'm wanting to use a CMD for loop in combination with the move command to extract all the files in the subfolders to the parent folder, leaving each subfolder empty.



When giving the CMD for loop please break down what each part of the command is doing, as this'll help make sure I understand the code and hopefully minimise the trouble I have getting it to work for myself.










share|improve this question






















  • what if you did dir /s/b >a.a to get a list of all files, one per line. Then you move at the beginning of each line, and c:blahmyparentfolder at the end of each line then you have a list of command and can name the file blah.bat and run it
    – barlop
    Nov 17 '16 at 2:12

















up vote
0
down vote

favorite
1












Firstly, I'm aware there are tons of similar questions, and I know because I must have tried adapting at least 5 of them, but I'm still here asking because my crappy grasp of CMD (which I actually thought I was getting good at up until this point) is frustrating me with generic, unhelpful errors, and I can't understand what part of the syntax is wrong. This is likely due to the fact that other questions deal with the folder/filename structures of their respective OPs, and when I try to adapt the code to my own folder/file structure it refuses to work. Therefore, I'm here asking for a CMD for loop solution that'll actually work for my own file and folder structure.



I have a parent folder which contains many other folders, in which there are files:



Parent Folder
Subfolder
File1
File2
Subfolder
File1
File2
Subfolder
File1
File2


...and so on.



I'm wanting to use a CMD for loop in combination with the move command to extract all the files in the subfolders to the parent folder, leaving each subfolder empty.



When giving the CMD for loop please break down what each part of the command is doing, as this'll help make sure I understand the code and hopefully minimise the trouble I have getting it to work for myself.










share|improve this question






















  • what if you did dir /s/b >a.a to get a list of all files, one per line. Then you move at the beginning of each line, and c:blahmyparentfolder at the end of each line then you have a list of command and can name the file blah.bat and run it
    – barlop
    Nov 17 '16 at 2:12















up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





Firstly, I'm aware there are tons of similar questions, and I know because I must have tried adapting at least 5 of them, but I'm still here asking because my crappy grasp of CMD (which I actually thought I was getting good at up until this point) is frustrating me with generic, unhelpful errors, and I can't understand what part of the syntax is wrong. This is likely due to the fact that other questions deal with the folder/filename structures of their respective OPs, and when I try to adapt the code to my own folder/file structure it refuses to work. Therefore, I'm here asking for a CMD for loop solution that'll actually work for my own file and folder structure.



I have a parent folder which contains many other folders, in which there are files:



Parent Folder
Subfolder
File1
File2
Subfolder
File1
File2
Subfolder
File1
File2


...and so on.



I'm wanting to use a CMD for loop in combination with the move command to extract all the files in the subfolders to the parent folder, leaving each subfolder empty.



When giving the CMD for loop please break down what each part of the command is doing, as this'll help make sure I understand the code and hopefully minimise the trouble I have getting it to work for myself.










share|improve this question













Firstly, I'm aware there are tons of similar questions, and I know because I must have tried adapting at least 5 of them, but I'm still here asking because my crappy grasp of CMD (which I actually thought I was getting good at up until this point) is frustrating me with generic, unhelpful errors, and I can't understand what part of the syntax is wrong. This is likely due to the fact that other questions deal with the folder/filename structures of their respective OPs, and when I try to adapt the code to my own folder/file structure it refuses to work. Therefore, I'm here asking for a CMD for loop solution that'll actually work for my own file and folder structure.



I have a parent folder which contains many other folders, in which there are files:



Parent Folder
Subfolder
File1
File2
Subfolder
File1
File2
Subfolder
File1
File2


...and so on.



I'm wanting to use a CMD for loop in combination with the move command to extract all the files in the subfolders to the parent folder, leaving each subfolder empty.



When giving the CMD for loop please break down what each part of the command is doing, as this'll help make sure I understand the code and hopefully minimise the trouble I have getting it to work for myself.







windows-7 windows command-line windows-explorer cmd.exe






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 17 '16 at 2:04









Hashim

2,83962852




2,83962852












  • what if you did dir /s/b >a.a to get a list of all files, one per line. Then you move at the beginning of each line, and c:blahmyparentfolder at the end of each line then you have a list of command and can name the file blah.bat and run it
    – barlop
    Nov 17 '16 at 2:12




















  • what if you did dir /s/b >a.a to get a list of all files, one per line. Then you move at the beginning of each line, and c:blahmyparentfolder at the end of each line then you have a list of command and can name the file blah.bat and run it
    – barlop
    Nov 17 '16 at 2:12


















what if you did dir /s/b >a.a to get a list of all files, one per line. Then you move at the beginning of each line, and c:blahmyparentfolder at the end of each line then you have a list of command and can name the file blah.bat and run it
– barlop
Nov 17 '16 at 2:12






what if you did dir /s/b >a.a to get a list of all files, one per line. Then you move at the beginning of each line, and c:blahmyparentfolder at the end of each line then you have a list of command and can name the file blah.bat and run it
– barlop
Nov 17 '16 at 2:12












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Okay, I finally figured it out by adapting code from yet another question. Thanks to @AFH for clarifying the parts I was unsure about in the comments to this answer, and for his general help with it - it's much appreciated.



To Extract All Files from Subfolders to their Parent Folder



for /r "PARENTFOLDER" %d in (*.*) do move "%d" "PARENTFOLDER"



Remember to replace all instances of PARENTFOLDER with the path to the folder that you want to extract the files to.



To Extract All Files from Subfolders and Delete Empty Subfolders



It's unlikely you'll need to keep the empty subfolders left over after extracting the files from them - I certainly didn't - so the below command automates the deletion of them, too.



for /r "PARENTFOLDER" %d in (*.*) do move "%d" "PARENTFOLDER" && cd "PARENTFOLDER" && for /f "delims=" %d in ('dir /s /b /ad ^| sort /r') do rd "%d"



Once again, replace all instances of PARENTFOLDER with the path to the folder that you want to extract the files to.



By this point, however, it's no longer a one-liner, and starts to get a bit convoluted when pasted into the command line, so it's easier to just put the whole thing in a batch file. Using variables for the path to the parent folder allows you to replace just the one instance of PARENTFOLDER at the beginning of the file, and it's also the safer option, preventing against any accidental deletion of empty folders that you might want to keep.



The Batch File



Paste into a text file, replace PARENTFOLDER with the path to the folder you want it to work with, and save it with the .bat extension. Run in any directory.



@ECHO OFF
SETLOCAL
SET parent="PARENTFOLDER"
CD /d %parent%
FOR /r %parent% %%d IN (*.*) DO MOVE "%%d" %parent%
FOR /f "delims=" %%d IN ('DIR /a:d /s /b ^| SORT /r') DO RD "%%d"
ECHO Done. Press any key to terminate script.
PAUSE >NUL





share|improve this answer



















  • 1




    The for command syntax is not rational, but the first %d defines d as the loop variable, and the second %d is what the loop variable expands to on each pass of the loop (Unix syntax is more rational: for ... d in ... do ...). The first instance is not expanded, so it does not need quoting, unlike after the do, where %d may expand to file names with embedded blanks. There is nothing special about d: any single letter could be used, but d is logical for directories (or f for files). Even more irrationally, %%d must be used in a batch (.cmd or .bat) file.
    – AFH
    Nov 17 '16 at 2:58






  • 1




    Without testing, I think you want a nested for, along the lines of for /d %d in (PARENTFOLDER*) do for /r "%d" %f in (*.*) do move "%f" TARGETLOCATION. If you include the extra requirements into your question, I'll submit an answer, for the benefit of others.
    – AFH
    Nov 18 '16 at 0:58






  • 1




    If PARENTFOLDER contains blanks, it will need to be quoted. I suggest you put an echo between the first do and the second for, to check exactly what nested for command would be executed. Your conditions have so many possible variants that I can't set up a representative test here.
    – AFH
    Nov 20 '16 at 0:12








  • 1




    If you don't quote PARENTFOLDER and it's something like name with spaces, the first for will evaluate as for /d %d in (name with spaces*) do ..., so d will become successively name, with, then any files matching the mask spaces*. Parentheses do not stop the requirement to quote. The only time you needn't quote is with the cd command, which takes the rest of the line as a single parameter, including any spaces (though Tab expansion inserts quotes); md and rd can act on multiple directories, so need quotes if the names have spaces.
    – AFH
    Nov 20 '16 at 3:08








  • 1




    I'm glad you have solved it.
    – AFH
    Jan 4 '17 at 23:44











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1146686%2fmove-all-files-within-subfolders-to-parent-folder%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Okay, I finally figured it out by adapting code from yet another question. Thanks to @AFH for clarifying the parts I was unsure about in the comments to this answer, and for his general help with it - it's much appreciated.



To Extract All Files from Subfolders to their Parent Folder



for /r "PARENTFOLDER" %d in (*.*) do move "%d" "PARENTFOLDER"



Remember to replace all instances of PARENTFOLDER with the path to the folder that you want to extract the files to.



To Extract All Files from Subfolders and Delete Empty Subfolders



It's unlikely you'll need to keep the empty subfolders left over after extracting the files from them - I certainly didn't - so the below command automates the deletion of them, too.



for /r "PARENTFOLDER" %d in (*.*) do move "%d" "PARENTFOLDER" && cd "PARENTFOLDER" && for /f "delims=" %d in ('dir /s /b /ad ^| sort /r') do rd "%d"



Once again, replace all instances of PARENTFOLDER with the path to the folder that you want to extract the files to.



By this point, however, it's no longer a one-liner, and starts to get a bit convoluted when pasted into the command line, so it's easier to just put the whole thing in a batch file. Using variables for the path to the parent folder allows you to replace just the one instance of PARENTFOLDER at the beginning of the file, and it's also the safer option, preventing against any accidental deletion of empty folders that you might want to keep.



The Batch File



Paste into a text file, replace PARENTFOLDER with the path to the folder you want it to work with, and save it with the .bat extension. Run in any directory.



@ECHO OFF
SETLOCAL
SET parent="PARENTFOLDER"
CD /d %parent%
FOR /r %parent% %%d IN (*.*) DO MOVE "%%d" %parent%
FOR /f "delims=" %%d IN ('DIR /a:d /s /b ^| SORT /r') DO RD "%%d"
ECHO Done. Press any key to terminate script.
PAUSE >NUL





share|improve this answer



















  • 1




    The for command syntax is not rational, but the first %d defines d as the loop variable, and the second %d is what the loop variable expands to on each pass of the loop (Unix syntax is more rational: for ... d in ... do ...). The first instance is not expanded, so it does not need quoting, unlike after the do, where %d may expand to file names with embedded blanks. There is nothing special about d: any single letter could be used, but d is logical for directories (or f for files). Even more irrationally, %%d must be used in a batch (.cmd or .bat) file.
    – AFH
    Nov 17 '16 at 2:58






  • 1




    Without testing, I think you want a nested for, along the lines of for /d %d in (PARENTFOLDER*) do for /r "%d" %f in (*.*) do move "%f" TARGETLOCATION. If you include the extra requirements into your question, I'll submit an answer, for the benefit of others.
    – AFH
    Nov 18 '16 at 0:58






  • 1




    If PARENTFOLDER contains blanks, it will need to be quoted. I suggest you put an echo between the first do and the second for, to check exactly what nested for command would be executed. Your conditions have so many possible variants that I can't set up a representative test here.
    – AFH
    Nov 20 '16 at 0:12








  • 1




    If you don't quote PARENTFOLDER and it's something like name with spaces, the first for will evaluate as for /d %d in (name with spaces*) do ..., so d will become successively name, with, then any files matching the mask spaces*. Parentheses do not stop the requirement to quote. The only time you needn't quote is with the cd command, which takes the rest of the line as a single parameter, including any spaces (though Tab expansion inserts quotes); md and rd can act on multiple directories, so need quotes if the names have spaces.
    – AFH
    Nov 20 '16 at 3:08








  • 1




    I'm glad you have solved it.
    – AFH
    Jan 4 '17 at 23:44















up vote
2
down vote



accepted










Okay, I finally figured it out by adapting code from yet another question. Thanks to @AFH for clarifying the parts I was unsure about in the comments to this answer, and for his general help with it - it's much appreciated.



To Extract All Files from Subfolders to their Parent Folder



for /r "PARENTFOLDER" %d in (*.*) do move "%d" "PARENTFOLDER"



Remember to replace all instances of PARENTFOLDER with the path to the folder that you want to extract the files to.



To Extract All Files from Subfolders and Delete Empty Subfolders



It's unlikely you'll need to keep the empty subfolders left over after extracting the files from them - I certainly didn't - so the below command automates the deletion of them, too.



for /r "PARENTFOLDER" %d in (*.*) do move "%d" "PARENTFOLDER" && cd "PARENTFOLDER" && for /f "delims=" %d in ('dir /s /b /ad ^| sort /r') do rd "%d"



Once again, replace all instances of PARENTFOLDER with the path to the folder that you want to extract the files to.



By this point, however, it's no longer a one-liner, and starts to get a bit convoluted when pasted into the command line, so it's easier to just put the whole thing in a batch file. Using variables for the path to the parent folder allows you to replace just the one instance of PARENTFOLDER at the beginning of the file, and it's also the safer option, preventing against any accidental deletion of empty folders that you might want to keep.



The Batch File



Paste into a text file, replace PARENTFOLDER with the path to the folder you want it to work with, and save it with the .bat extension. Run in any directory.



@ECHO OFF
SETLOCAL
SET parent="PARENTFOLDER"
CD /d %parent%
FOR /r %parent% %%d IN (*.*) DO MOVE "%%d" %parent%
FOR /f "delims=" %%d IN ('DIR /a:d /s /b ^| SORT /r') DO RD "%%d"
ECHO Done. Press any key to terminate script.
PAUSE >NUL





share|improve this answer



















  • 1




    The for command syntax is not rational, but the first %d defines d as the loop variable, and the second %d is what the loop variable expands to on each pass of the loop (Unix syntax is more rational: for ... d in ... do ...). The first instance is not expanded, so it does not need quoting, unlike after the do, where %d may expand to file names with embedded blanks. There is nothing special about d: any single letter could be used, but d is logical for directories (or f for files). Even more irrationally, %%d must be used in a batch (.cmd or .bat) file.
    – AFH
    Nov 17 '16 at 2:58






  • 1




    Without testing, I think you want a nested for, along the lines of for /d %d in (PARENTFOLDER*) do for /r "%d" %f in (*.*) do move "%f" TARGETLOCATION. If you include the extra requirements into your question, I'll submit an answer, for the benefit of others.
    – AFH
    Nov 18 '16 at 0:58






  • 1




    If PARENTFOLDER contains blanks, it will need to be quoted. I suggest you put an echo between the first do and the second for, to check exactly what nested for command would be executed. Your conditions have so many possible variants that I can't set up a representative test here.
    – AFH
    Nov 20 '16 at 0:12








  • 1




    If you don't quote PARENTFOLDER and it's something like name with spaces, the first for will evaluate as for /d %d in (name with spaces*) do ..., so d will become successively name, with, then any files matching the mask spaces*. Parentheses do not stop the requirement to quote. The only time you needn't quote is with the cd command, which takes the rest of the line as a single parameter, including any spaces (though Tab expansion inserts quotes); md and rd can act on multiple directories, so need quotes if the names have spaces.
    – AFH
    Nov 20 '16 at 3:08








  • 1




    I'm glad you have solved it.
    – AFH
    Jan 4 '17 at 23:44













up vote
2
down vote



accepted







up vote
2
down vote



accepted






Okay, I finally figured it out by adapting code from yet another question. Thanks to @AFH for clarifying the parts I was unsure about in the comments to this answer, and for his general help with it - it's much appreciated.



To Extract All Files from Subfolders to their Parent Folder



for /r "PARENTFOLDER" %d in (*.*) do move "%d" "PARENTFOLDER"



Remember to replace all instances of PARENTFOLDER with the path to the folder that you want to extract the files to.



To Extract All Files from Subfolders and Delete Empty Subfolders



It's unlikely you'll need to keep the empty subfolders left over after extracting the files from them - I certainly didn't - so the below command automates the deletion of them, too.



for /r "PARENTFOLDER" %d in (*.*) do move "%d" "PARENTFOLDER" && cd "PARENTFOLDER" && for /f "delims=" %d in ('dir /s /b /ad ^| sort /r') do rd "%d"



Once again, replace all instances of PARENTFOLDER with the path to the folder that you want to extract the files to.



By this point, however, it's no longer a one-liner, and starts to get a bit convoluted when pasted into the command line, so it's easier to just put the whole thing in a batch file. Using variables for the path to the parent folder allows you to replace just the one instance of PARENTFOLDER at the beginning of the file, and it's also the safer option, preventing against any accidental deletion of empty folders that you might want to keep.



The Batch File



Paste into a text file, replace PARENTFOLDER with the path to the folder you want it to work with, and save it with the .bat extension. Run in any directory.



@ECHO OFF
SETLOCAL
SET parent="PARENTFOLDER"
CD /d %parent%
FOR /r %parent% %%d IN (*.*) DO MOVE "%%d" %parent%
FOR /f "delims=" %%d IN ('DIR /a:d /s /b ^| SORT /r') DO RD "%%d"
ECHO Done. Press any key to terminate script.
PAUSE >NUL





share|improve this answer














Okay, I finally figured it out by adapting code from yet another question. Thanks to @AFH for clarifying the parts I was unsure about in the comments to this answer, and for his general help with it - it's much appreciated.



To Extract All Files from Subfolders to their Parent Folder



for /r "PARENTFOLDER" %d in (*.*) do move "%d" "PARENTFOLDER"



Remember to replace all instances of PARENTFOLDER with the path to the folder that you want to extract the files to.



To Extract All Files from Subfolders and Delete Empty Subfolders



It's unlikely you'll need to keep the empty subfolders left over after extracting the files from them - I certainly didn't - so the below command automates the deletion of them, too.



for /r "PARENTFOLDER" %d in (*.*) do move "%d" "PARENTFOLDER" && cd "PARENTFOLDER" && for /f "delims=" %d in ('dir /s /b /ad ^| sort /r') do rd "%d"



Once again, replace all instances of PARENTFOLDER with the path to the folder that you want to extract the files to.



By this point, however, it's no longer a one-liner, and starts to get a bit convoluted when pasted into the command line, so it's easier to just put the whole thing in a batch file. Using variables for the path to the parent folder allows you to replace just the one instance of PARENTFOLDER at the beginning of the file, and it's also the safer option, preventing against any accidental deletion of empty folders that you might want to keep.



The Batch File



Paste into a text file, replace PARENTFOLDER with the path to the folder you want it to work with, and save it with the .bat extension. Run in any directory.



@ECHO OFF
SETLOCAL
SET parent="PARENTFOLDER"
CD /d %parent%
FOR /r %parent% %%d IN (*.*) DO MOVE "%%d" %parent%
FOR /f "delims=" %%d IN ('DIR /a:d /s /b ^| SORT /r') DO RD "%%d"
ECHO Done. Press any key to terminate script.
PAUSE >NUL






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 4 '17 at 23:50

























answered Nov 17 '16 at 2:16









Hashim

2,83962852




2,83962852








  • 1




    The for command syntax is not rational, but the first %d defines d as the loop variable, and the second %d is what the loop variable expands to on each pass of the loop (Unix syntax is more rational: for ... d in ... do ...). The first instance is not expanded, so it does not need quoting, unlike after the do, where %d may expand to file names with embedded blanks. There is nothing special about d: any single letter could be used, but d is logical for directories (or f for files). Even more irrationally, %%d must be used in a batch (.cmd or .bat) file.
    – AFH
    Nov 17 '16 at 2:58






  • 1




    Without testing, I think you want a nested for, along the lines of for /d %d in (PARENTFOLDER*) do for /r "%d" %f in (*.*) do move "%f" TARGETLOCATION. If you include the extra requirements into your question, I'll submit an answer, for the benefit of others.
    – AFH
    Nov 18 '16 at 0:58






  • 1




    If PARENTFOLDER contains blanks, it will need to be quoted. I suggest you put an echo between the first do and the second for, to check exactly what nested for command would be executed. Your conditions have so many possible variants that I can't set up a representative test here.
    – AFH
    Nov 20 '16 at 0:12








  • 1




    If you don't quote PARENTFOLDER and it's something like name with spaces, the first for will evaluate as for /d %d in (name with spaces*) do ..., so d will become successively name, with, then any files matching the mask spaces*. Parentheses do not stop the requirement to quote. The only time you needn't quote is with the cd command, which takes the rest of the line as a single parameter, including any spaces (though Tab expansion inserts quotes); md and rd can act on multiple directories, so need quotes if the names have spaces.
    – AFH
    Nov 20 '16 at 3:08








  • 1




    I'm glad you have solved it.
    – AFH
    Jan 4 '17 at 23:44














  • 1




    The for command syntax is not rational, but the first %d defines d as the loop variable, and the second %d is what the loop variable expands to on each pass of the loop (Unix syntax is more rational: for ... d in ... do ...). The first instance is not expanded, so it does not need quoting, unlike after the do, where %d may expand to file names with embedded blanks. There is nothing special about d: any single letter could be used, but d is logical for directories (or f for files). Even more irrationally, %%d must be used in a batch (.cmd or .bat) file.
    – AFH
    Nov 17 '16 at 2:58






  • 1




    Without testing, I think you want a nested for, along the lines of for /d %d in (PARENTFOLDER*) do for /r "%d" %f in (*.*) do move "%f" TARGETLOCATION. If you include the extra requirements into your question, I'll submit an answer, for the benefit of others.
    – AFH
    Nov 18 '16 at 0:58






  • 1




    If PARENTFOLDER contains blanks, it will need to be quoted. I suggest you put an echo between the first do and the second for, to check exactly what nested for command would be executed. Your conditions have so many possible variants that I can't set up a representative test here.
    – AFH
    Nov 20 '16 at 0:12








  • 1




    If you don't quote PARENTFOLDER and it's something like name with spaces, the first for will evaluate as for /d %d in (name with spaces*) do ..., so d will become successively name, with, then any files matching the mask spaces*. Parentheses do not stop the requirement to quote. The only time you needn't quote is with the cd command, which takes the rest of the line as a single parameter, including any spaces (though Tab expansion inserts quotes); md and rd can act on multiple directories, so need quotes if the names have spaces.
    – AFH
    Nov 20 '16 at 3:08








  • 1




    I'm glad you have solved it.
    – AFH
    Jan 4 '17 at 23:44








1




1




The for command syntax is not rational, but the first %d defines d as the loop variable, and the second %d is what the loop variable expands to on each pass of the loop (Unix syntax is more rational: for ... d in ... do ...). The first instance is not expanded, so it does not need quoting, unlike after the do, where %d may expand to file names with embedded blanks. There is nothing special about d: any single letter could be used, but d is logical for directories (or f for files). Even more irrationally, %%d must be used in a batch (.cmd or .bat) file.
– AFH
Nov 17 '16 at 2:58




The for command syntax is not rational, but the first %d defines d as the loop variable, and the second %d is what the loop variable expands to on each pass of the loop (Unix syntax is more rational: for ... d in ... do ...). The first instance is not expanded, so it does not need quoting, unlike after the do, where %d may expand to file names with embedded blanks. There is nothing special about d: any single letter could be used, but d is logical for directories (or f for files). Even more irrationally, %%d must be used in a batch (.cmd or .bat) file.
– AFH
Nov 17 '16 at 2:58




1




1




Without testing, I think you want a nested for, along the lines of for /d %d in (PARENTFOLDER*) do for /r "%d" %f in (*.*) do move "%f" TARGETLOCATION. If you include the extra requirements into your question, I'll submit an answer, for the benefit of others.
– AFH
Nov 18 '16 at 0:58




Without testing, I think you want a nested for, along the lines of for /d %d in (PARENTFOLDER*) do for /r "%d" %f in (*.*) do move "%f" TARGETLOCATION. If you include the extra requirements into your question, I'll submit an answer, for the benefit of others.
– AFH
Nov 18 '16 at 0:58




1




1




If PARENTFOLDER contains blanks, it will need to be quoted. I suggest you put an echo between the first do and the second for, to check exactly what nested for command would be executed. Your conditions have so many possible variants that I can't set up a representative test here.
– AFH
Nov 20 '16 at 0:12






If PARENTFOLDER contains blanks, it will need to be quoted. I suggest you put an echo between the first do and the second for, to check exactly what nested for command would be executed. Your conditions have so many possible variants that I can't set up a representative test here.
– AFH
Nov 20 '16 at 0:12






1




1




If you don't quote PARENTFOLDER and it's something like name with spaces, the first for will evaluate as for /d %d in (name with spaces*) do ..., so d will become successively name, with, then any files matching the mask spaces*. Parentheses do not stop the requirement to quote. The only time you needn't quote is with the cd command, which takes the rest of the line as a single parameter, including any spaces (though Tab expansion inserts quotes); md and rd can act on multiple directories, so need quotes if the names have spaces.
– AFH
Nov 20 '16 at 3:08






If you don't quote PARENTFOLDER and it's something like name with spaces, the first for will evaluate as for /d %d in (name with spaces*) do ..., so d will become successively name, with, then any files matching the mask spaces*. Parentheses do not stop the requirement to quote. The only time you needn't quote is with the cd command, which takes the rest of the line as a single parameter, including any spaces (though Tab expansion inserts quotes); md and rd can act on multiple directories, so need quotes if the names have spaces.
– AFH
Nov 20 '16 at 3:08






1




1




I'm glad you have solved it.
– AFH
Jan 4 '17 at 23:44




I'm glad you have solved it.
– AFH
Jan 4 '17 at 23:44


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1146686%2fmove-all-files-within-subfolders-to-parent-folder%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

QoS: MAC-Priority for clients behind a repeater

Ивакино (Тотемский район)

Can't locate Autom4te/ChannelDefs.pm in @INC (when it definitely is there)