Batch file to create a folder and move files to it
Moderator: DosItHelp
-
- Posts: 14
- Joined: 03 Jan 2013 22:45
Batch file to create a folder and move files to it
Hi there i am in desperate need of some help. i have a main folder with around 3000 subfolders in it (131 GB) in each sub folders there will be any number of folders and files in them. i would like to condense all of the sub folders and files into one new sub folder. Example of what i have
G:\clients\xxxxx client name\(random folders and files)
G:\clients\nnnnn client name\(random folders and files)
and on for around 3000 more.
What i would like to do is make i so each client folder will now only have one sub folder with all the other info moved into it:
G:\clients\xxxxx client name\"new subfolder"\(random folders and files)
G:\clients\nnnnn client name\"new subfolder"\(random folders and files)
the (random folders and files) need to keep their existing structure inside their "new subfolder"
"new subfolder" will be the same folder name in all client folders.
(if possible i would like to exclude a certain file type from moving into the "new subfolder" (all quickbooks files))
if i can't exclude them i will deal with them manually by creating a txt file batch script to find them and report their locations)
any help?
I think it can easily be done, just not sure how to make it happen to all 3000 folders automatically?
Thanks in advance for the assistance.
G:\clients\xxxxx client name\(random folders and files)
G:\clients\nnnnn client name\(random folders and files)
and on for around 3000 more.
What i would like to do is make i so each client folder will now only have one sub folder with all the other info moved into it:
G:\clients\xxxxx client name\"new subfolder"\(random folders and files)
G:\clients\nnnnn client name\"new subfolder"\(random folders and files)
the (random folders and files) need to keep their existing structure inside their "new subfolder"
"new subfolder" will be the same folder name in all client folders.
(if possible i would like to exclude a certain file type from moving into the "new subfolder" (all quickbooks files))
if i can't exclude them i will deal with them manually by creating a txt file batch script to find them and report their locations)
any help?
I think it can easily be done, just not sure how to make it happen to all 3000 folders automatically?
Thanks in advance for the assistance.
Re: Batch file to create a folder and move files to it
This should change
g:\clients\namexxx\folder a
g:\clients\namexxx\folder b
into
g:\clients\namexxx\new folder\folder a
g:\clients\namexxx\new folder\folder b
etc
It is untested so test it on some sample folders and files.
It doesn't exclude any filetypes.
g:\clients\namexxx\folder a
g:\clients\namexxx\folder b
into
g:\clients\namexxx\new folder\folder a
g:\clients\namexxx\new folder\folder b
etc
It is untested so test it on some sample folders and files.
It doesn't exclude any filetypes.
Code: Select all
@echo off
set "d=new folder"
pushd "g:\clients"
for /f "delims=" %%z in ('dir /ad /b') do (
pushd "%%z"
for /f "delims=" %%a in ('dir /ad /b') do (
md "%d%" 2>nul
move "%%a" "%d%"
)
move *.* "%d%"
popd
)
popd
Re: Batch file to create a folder and move files to it
Hi,
@Foxidrive, you are missing a ( at the end of the 2nd for loop.
BTW, how move command move folders ? i don't understand
@phone_tech,
This is the same code foxidrive posted, just added another for loop before moving the files to exclude certain type you specify in the variable "skip"
Though, it generate this error:
But it worked for me and skiped all .py files.
@Foxidrive, you are missing a ( at the end of the 2nd for loop.
BTW, how move command move folders ? i don't understand


@phone_tech,
This is the same code foxidrive posted, just added another for loop before moving the files to exclude certain type you specify in the variable "skip"
Though, it generate this error:
The process cannot access the file because it is being used by another process.
But it worked for me and skiped all .py files.
Code: Select all
@echo off
set "d=new folder"
set "skip=.py"
pushd "g:\clients"
for /f "delims=" %%z in ('dir /ad /b') do (
pushd "%%z"
for /f "delims=" %%a in ('dir /ad /b') do (
md "%d%" 2>nul
move "%%a" "%d%"
)
for /f "delims=" %%x in ('dir /b') do (
if not "%%x" == "%%~nx%skip%" move "%%x" "%d%"
)
popd
)
popd
pause
Re: Batch file to create a folder and move files to it
abc0502 wrote:Hi,
@Foxidrive, you are missing a ( at the end of the 2nd for loop.
BTW, how move command move folders ? i don't understand![]()
![]()
Thanks for that. I edited it.
The move command can also move folders. I think the code should work.
Try this test in a folder above the root.
Code: Select all
@echo off
md "xyz"
echo abc>"xyz\123.txt"
move "xyz" \
pause
Re: Batch file to create a folder and move files to it
Your code work great, i tested it, it just i tried to use the move command before on a folder but didn't work and in the help document it says it used to move files so i assumed it just move files.
-
- Posts: 14
- Joined: 03 Jan 2013 22:45
Re: Batch file to create a folder and move files to it
Man, you guys are awesome. So it worked (note to self, run it from the clients folder not on the root...) It thought that by looking at the code it was going to run itself in the right place regardless of where it was executed from. I had a little fixing and didn't work quite right because of that. Good thing i was on a test system.
So it worked awesome once i figured that out. I did see a few errors about not able to access a file i think they may have been some of the dumb quick books files that automatically create and delete when their associated files are deleted / created.
so, i will work with this and test some more.
Curious though, in that last code, how do i "skip" additional extensions (.qbw .qbb .tlg etc...)
Thanks again for the help, next thing i'll have to do is figure out the commands in the script to teach myself some more of this... love it.
let me know how to skip more extensions if you can.
Thanks.

so, i will work with this and test some more.
Curious though, in that last code, how do i "skip" additional extensions (.qbw .qbb .tlg etc...)
Thanks again for the help, next thing i'll have to do is figure out the commands in the script to teach myself some more of this... love it.
let me know how to skip more extensions if you can.
Thanks.
-
- Posts: 14
- Joined: 03 Jan 2013 22:45
Re: Batch file to create a folder and move files to it
Okay, so upon further testing...
If the client folder (namexxxx) doesn't have one or more subfolders (only has files, doc, xls, etc...) the script takes a random document in there and names it new folder (without an extension) (then it is an unknown file) it then skips the rest of the documents in there.
I also notice if the folder has a ~random name.doc file it doesn't move it (probably fine because i don't really want them anyway).
I won't use the skip option because it only skipped the files if they are in the namexxxx folder and move the ones that were in subfolders (as it should all the other files)
So, any help as to why if the "namexxx" folder doens't have at least one subfolder it would fail?
Thanks again.
If the client folder (namexxxx) doesn't have one or more subfolders (only has files, doc, xls, etc...) the script takes a random document in there and names it new folder (without an extension) (then it is an unknown file) it then skips the rest of the documents in there.

I also notice if the folder has a ~random name.doc file it doesn't move it (probably fine because i don't really want them anyway).
I won't use the skip option because it only skipped the files if they are in the namexxxx folder and move the ones that were in subfolders (as it should all the other files)
So, any help as to why if the "namexxx" folder doens't have at least one subfolder it would fail?
Thanks again.
Re: Batch file to create a folder and move files to it
Try this on some sample files. It should handle folders with no subdirectories too.
The line pushd "g:\clients" makes it operate on only that directory structure. Change it to your temporary folder to test it.
Exclusions have to be done on every folder. If you identify your excluded files then you can move them later - we don't know where you want them.
The line pushd "g:\clients" makes it operate on only that directory structure. Change it to your temporary folder to test it.
Exclusions have to be done on every folder. If you identify your excluded files then you can move them later - we don't know where you want them.
Code: Select all
@echo off
set "d=new folder"
pushd "g:\clients"
for /f "delims=" %%z in ('dir /ad /b') do (
pushd "%%z"
md "%d%" 2>nul
for /f "delims=" %%a in ('dir /ad /b ^|findstr /v /i "^%d%$"') do move "%%a" "%d%"
move *.* "%d%"
popd
)
popd
pause
Re: Batch file to create a folder and move files to it
phone_tech wrote: It thought that by looking at the code it was going to run itself in the right place regardless of where it was executed from.
Yes, it does. If "g:\clients" exists it would only operate on that directory structure.
If it doesn't exist then it will run from wherever the batch file is.
-
- Posts: 14
- Joined: 03 Jan 2013 22:45
Re: Batch file to create a folder and move files to it
Thanks again for all the help.
So g, is a network drive. The actual location will be d:\frank1\xxxx so on my test system here is what i ran
When i run it from D:\Frank1 it works fine (except for the issue i mentioned about it not doing the subfolders right if it only has files in it).
So what would make this whole thing work perfect would be for it to do what it does with moving everything to my "new folder" (lets call it what it is) "Prior to EFC" but within each client folder it would be GREAT if i could take all quickbooks files and put them in a subfolder (next to "Prior to EFC") called "QuickBooks". so in my client folder i would have two folders. One called "Prior to EFC" one called "QuickBooks". The qbooks files could come from any number of folders under a client folder.
Do you think this is possible with moving all the QB files also? they have extensions like, ".qbb" ".qbw" ".qba.dsn" ".qba.nd" "qba.tlg"
Also, using this last code, it did what it was supposed to with the Prior to EFC folders regardless of subdirectory structure.
Do you think we can make the QB files re-arrange also?
here is the script that is working for me now.
I do get errors though about a filename syntax wrong? everything seems to go though? i'll keep testing it also.
THANK you SO much.
So g, is a network drive. The actual location will be d:\frank1\xxxx so on my test system here is what i ran
Code: Select all
@echo off
set "d=Prior to EFC"
pushd "d:\frank1\clients"
for /f "delims=" %%z in ('dir /ad /b') do (
pushd "%%z"
for /f "delims=" %%a in ('dir /ad /b') do (
md "%d%" 2>nul
move "%%a" "%d%"
)
move *.* "%d%"
popd
)
popd
When i run it from D:\Frank1 it works fine (except for the issue i mentioned about it not doing the subfolders right if it only has files in it).
So what would make this whole thing work perfect would be for it to do what it does with moving everything to my "new folder" (lets call it what it is) "Prior to EFC" but within each client folder it would be GREAT if i could take all quickbooks files and put them in a subfolder (next to "Prior to EFC") called "QuickBooks". so in my client folder i would have two folders. One called "Prior to EFC" one called "QuickBooks". The qbooks files could come from any number of folders under a client folder.
Do you think this is possible with moving all the QB files also? they have extensions like, ".qbb" ".qbw" ".qba.dsn" ".qba.nd" "qba.tlg"
Also, using this last code, it did what it was supposed to with the Prior to EFC folders regardless of subdirectory structure.
Do you think we can make the QB files re-arrange also?
here is the script that is working for me now.
Code: Select all
@echo off
set "d=Prior to EFC"
pushd "D:\Frank1\clients"
for /f "delims=" %%z in ('dir /ad /b') do (
pushd "%%z"
md "%d%" 2>nul
for /f "delims=" %%a in ('dir /ad /b ^|findstr /v /i "^%d%$"') do move "%%a" "%d%"
move *.* "%d%"
popd
)
popd
pause
I do get errors though about a filename syntax wrong? everything seems to go though? i'll keep testing it also.
THANK you SO much.
Re: Batch file to create a folder and move files to it
Would any of the Quickbook files have the same name in any of the folders under a given client folder?
To move them all to a single quickbooks folder we'd need to know if there could be any filename clashes.
I'm not sure what causes the syntax error you see as I don't get it in tests here.
To move them all to a single quickbooks folder we'd need to know if there could be any filename clashes.
I'm not sure what causes the syntax error you see as I don't get it in tests here.
-
- Posts: 14
- Joined: 03 Jan 2013 22:45
Re: Batch file to create a folder and move files to it
More than likely they would not have the same names. QuickBooks though does store one "company file" with a few extensions. So like, ABC Company may be currently as follows:
d:\frank1\clients\12345 ABC Company\2010\ABC Company.QBB
d:\frank1\clients\12345 ABC Company\2010\ABC Company.QBW
d:\frank1\clients\12345 ABC Company\2010\ABC Company.QBA.TLG
They could also have another file for example:
d:\frank1\clients\12345 ABC Company\2010\ABC Company 12-2-10.QBB
d:\frank1\clients\12345 ABC Company\2010\ABC Company 12-2-10.QBW
d:\frank1\clients\12345 ABC Company\2010\ABC Company 12-2-10.QBA.TLG
and then of course they could be anywhere in the current folder structure under the client folders. (keep in mind once i run the other batch file these would be a directory lower) (under "Prior to EFC")
I can run this (move) as a separate batch before or after the "Prior to EFC" move.
I will be doing a batch file on the "clients" folder to list a .txt of all QB files before i move and after I move because our employees need to go in and delete a ton of these QB files that aren't needed anymore. If i manually have to work with the QB files that is okay, i would rather not but i am definitely way further ahead now that i have our script to combine everything into "Prior to EFC"
d:\frank1\clients\12345 ABC Company\2010\ABC Company.QBB
d:\frank1\clients\12345 ABC Company\2010\ABC Company.QBW
d:\frank1\clients\12345 ABC Company\2010\ABC Company.QBA.TLG
They could also have another file for example:
d:\frank1\clients\12345 ABC Company\2010\ABC Company 12-2-10.QBB
d:\frank1\clients\12345 ABC Company\2010\ABC Company 12-2-10.QBW
d:\frank1\clients\12345 ABC Company\2010\ABC Company 12-2-10.QBA.TLG
and then of course they could be anywhere in the current folder structure under the client folders. (keep in mind once i run the other batch file these would be a directory lower) (under "Prior to EFC")
I can run this (move) as a separate batch before or after the "Prior to EFC" move.
I will be doing a batch file on the "clients" folder to list a .txt of all QB files before i move and after I move because our employees need to go in and delete a ton of these QB files that aren't needed anymore. If i manually have to work with the QB files that is okay, i would rather not but i am definitely way further ahead now that i have our script to combine everything into "Prior to EFC"
Re: Batch file to create a folder and move files to it
This is untested: but is intended to be run after the initial folder change to create a "D:\Frank1\clients\clientxxx\Quickbooks" folder
It should move the files with the extensions listed below to that folder, or append entries to a file called "D:\Frank1\clients\qb.logs" with filenames that are duplicated and not moved.
Add any more extensions that you can think of to the list below, separated by a space.
It should move the files with the extensions listed below to that folder, or append entries to a file called "D:\Frank1\clients\qb.logs" with filenames that are duplicated and not moved.
Add any more extensions that you can think of to the list below, separated by a space.
Code: Select all
@echo off
pushd "d:\frank1\clients"
for /f "delims=" %%z in ('dir /ad /b') do (
md "%%z\Quickbooks" 2>nul
pushd "%%z\Prior to EFC\"
for /f "delims=" %%a in (' dir /b /s /a-d *.qbw *.qba.dsn *.qba.nd *qba.tlg ') do (
if not exist "..\Quickbooks\%%~nxa" (move "%%a" "..\Quickbooks\") else ( >>"D:\Frank1\clients\qb.logs" echo duplicate filename, not moved: "%%a")
)
popd
)
popd
-
- Posts: 14
- Joined: 03 Jan 2013 22:45
Re: Batch file to create a folder and move files to it
Man, this is sweet... works great. One question though. In this wonderfully awesome code is there anyway to not create the Quickbooks folder if there are none of those extensions found in their Prior to EFC folder? If not, no biggie would just be cleaner in my folders. Here is my edited code.
and i LOVE the log feature on here.. we have tons of duplicates just in the 10GB i've been testing.
Thank you so much for your help. I have got learn what these commands are so i can do this too...
One more question, (i haven't paid a lot of attention to it to find out for myself) in the "Prior to EFC" code, when it moves everything to it's new folder does it keep the "modified date" on the files and folders when it moves them? I know the Prior to EFC folder will have today's date but do the moved folders hold their original date? (not a big deal, just curious if it is or if it is possible)?
Code: Select all
@echo off
pushd "d:\frank1\clients"
for /f "delims=" %%z in ('dir /ad /b') do (
md "%%z\Quickbooks" 2>nul
pushd "%%z\Prior to EFC\"
for /f "delims=" %%a in (' dir /b /s /a-d *.qbw *.qba.dsn *.qba.nd *qba.tlg *.qbx *.qbb *.qbw.nd *.tlg *.lgb *.qbw.dsn *.qby *.qba *.nd *.iif') do (
if not exist "..\Quickbooks\%%~nxa" (move "%%a" "..\Quickbooks\") else ( >>"D:\Frank1\clients\qb.logs" echo duplicate filename, not moved: "%%a")
)
popd
)
popd
and i LOVE the log feature on here.. we have tons of duplicates just in the 10GB i've been testing.
Thank you so much for your help. I have got learn what these commands are so i can do this too...
One more question, (i haven't paid a lot of attention to it to find out for myself) in the "Prior to EFC" code, when it moves everything to it's new folder does it keep the "modified date" on the files and folders when it moves them? I know the Prior to EFC folder will have today's date but do the moved folders hold their original date? (not a big deal, just curious if it is or if it is possible)?
Re: Batch file to create a folder and move files to it
I added one line that should remove the Quickbooks folder if there are no files in it.
Hmmm. I tested a folder move and the folder keeps the last modified date as far as I can tell.
Code: Select all
@echo off
pushd "d:\frank1\clients"
for /f "delims=" %%z in ('dir /ad /b') do (
md "%%z\Quickbooks" 2>nul
pushd "%%z\Prior to EFC\"
for /f "delims=" %%a in (' dir /b /s /a-d *.qbw *.qba.dsn *.qba.nd *qba.tlg *.qbx *.qbb *.qbw.nd *.tlg *.lgb *.qbw.dsn *.qby *.qba *.nd *.iif') do (
if not exist "..\Quickbooks\%%~nxa" (move "%%a" "..\Quickbooks\") else ( >>"D:\Frank1\clients\qb.logs" echo duplicate filename, not moved: "%%a")
)
popd
rd "%%z\Quickbooks" 2>nul
)
popd
One more question, (i haven't paid a lot of attention to it to find out for myself) in the "Prior to EFC" code, when it moves everything to it's new folder does it keep the "modified date" on the files and folders when it moves them? I know the Prior to EFC folder will have today's date but do the moved folders hold their original date? (not a big deal, just curious if it is or if it is possible)?
Hmmm. I tested a folder move and the folder keeps the last modified date as far as I can tell.