Batch to move files and delete folder

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

poleary2000

Thread Starter
Joined
Mar 24, 2004
Messages
33
I would like to write a batch file that searches a directory and does the following. Any time it finds a folder named Home, it moves the files in that folder to the folder immediately above it and then deletes that folder named Home. Possible?

I don't know how to write batch files other than to close a process. Help please.

Patrick
 
Joined
Aug 7, 2007
Messages
9,028
Do you want to search just a directory, or a directory and all it's subfolders?
Does the Home folder contain just files, or does it also contain folders?
Does Home contain any hidden or system files?
What do you want to do if there is a duplicate file name?

For example, File1.txt already exists in the folder that contains Home:
Code:
C:\SOURCE
│   File1.txt
│
└───Home
        File1.txt
In this case do you want to overwrite C:\Source\File1.txt with C:\Source\Home\File1.txt, delete C:\Source\Home\File1.txt, or rename it somehow?
 

poleary2000

Thread Starter
Joined
Mar 24, 2004
Messages
33
I'd like to search all sub folders. There will be no duplicate files names as long as the files are placed directly above the "Home" folder.

I guess it doesn't matter if the folder is called home or not. Any time a file is found, it needs to be moved up one level in the structure. I can guarantee there will be no duplicate files.

Not sure if that makes sense.
 

poleary2000

Thread Starter
Joined
Mar 24, 2004
Messages
33
I should clarify. There are tons of duplicate file names, but they will never be in the same folder....even after moving with the batch file.
 
Joined
Aug 7, 2007
Messages
9,028
This will not move hidden or system files that are in the Home folder, they will be deleted. If that needs to be check, that will take a bit more, especially if you want to preserve the attributes.
Any hidden or system files in subfolders will be moved.

It will prompt to overwrite if there is a file in the top level of the home folder with the same name as a file in the parent. Remove the /-Y switch to have it silently overwrite
Make sure none of the files are being access, or it will fail. Even having any subfolder folder open in My Computer will cause it to fail.

Set the parent folder in the Set _Source line
Set the folder to move up in the Set _FindDir line.
Code:
@Echo Off
Set _Source=C:\Temp Dir\Source
Set _FindDir=Home
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Set _Path=%_Source%\%_FindDir%
If Exist "%_Path%" (
  Move /-Y "%_Path%\*.*" "%_Source%"
  For /F "Tokens=* Delims=" %%I In ('Dir /AD /B "%_Path%"') Do Move "%_Path%\%%I" "%_Source%"
  RD /S /Q "%_Path%"
) Else (
  Echo There is no %_FindDir% folder in %_Source%
)
 

poleary2000

Thread Starter
Joined
Mar 24, 2004
Messages
33
Thank you for your help so far. Just to make sure I was clear, my structure looks like this:

Folder A
Sub Folder A​
Home​
File A​
File B​
File C​
SubFolder B​
Home​
File A​
File B​
File C​

There are probably 500 of the Sub Folders. Will this bat code take all of the File A, B, C's, move them into their respective Sub Folder and then delete the Home folder? I believe it will based on what I can tell from the code, but wanted to show you the structure again just to make sure.

Thank you so much for your help so far. This is a weekly thing that needs to be done (don't ask, it's a long story). Let me know what you think.
 
Joined
Aug 7, 2007
Messages
9,028
I'm not exactly clear on what you want, the part where you say it doesn't matter what it's named is confusing. Are you just wanting to start at the top folder and move everything up one level?
In other words, start with this:
Code:
C:\SOURCE
¦   File0.txt
¦
+---Folder1
¦       File1.txt
+----------SubFolder1
¦
+---Folder2
¦       File2.txt
¦       File3.txt
+---------SubFolder2
¦
+---Folder3
¦       File4.txt
+---------SubFolder3
¦
+---Folder4
¦       File5.txt
+---------SubFolder4
¦
+---Folder5
¦       File6.txt
+---------SubFolder5
And end up with this:
Code:
C:\SOURCE
¦   File0.txt
¦   File1.txt
¦   File2.txt
¦   File3.txt
¦   File4.txt
¦   File5.txt
¦   File6.txt
+-----SubFolder1
+-----SubFolder2
+-----SubFolder3
+-----SubFolder4
+-----SubFolder5
 
Joined
Aug 7, 2007
Messages
9,028
OK, Now I think I got it. That first file won't do that. Give me a bit.

So we look in Folder A and find all the SubfolderX folders.
Inside those folders, if Home exists, move it up.
 

poleary2000

Thread Starter
Joined
Mar 24, 2004
Messages
33
Yes. This:

Folder A
Sub Folder A​
Home​
File A​
File B​
File C​
SubFolder B​
Home​
File A​
File B​
File C​

Becomes this...

Folder A
Sub Folder A​
File A​
File B​
File C​
SubFolder B​
File A​
File B​
File C​
 
Joined
Aug 7, 2007
Messages
9,028
Give this a try, use some test data of course:
Code:
@Echo Off
Set _Source=C:\Temp Dir\Source
Set _FindDir=Home
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
PushD "%_Source%"
For /F "Tokens=* Delims=" %%A In ('Dir /AD /B') Do (
  If Exist "%_Source%\%%A\%_FindDir%" (
    Move /-Y "%%A\%_FindDir%\*.*" "%_Source%\%%A"
    For /F "Tokens=* Delims=" %%I In ('Dir /AD /B "%_Source%\%%A\%_FindDir%"') Do Move "%_Source%\%%A\%_FindDir%\%%I" "%_Source%\%%A\"
  RD /S /Q "%_Source%\%%A\%_FindDir%"
  ) Else (
    Echo There is no %_FindDir% folder in %_Source%\%%A\
  ))
PopD
 

poleary2000

Thread Starter
Joined
Mar 24, 2004
Messages
33
Hmmm, I tried it but it just flashes the screen real quick and closes the file.

The directory is on a network share. Thus, it is on a network \\mercury\folder A. I tried the network name and that didn't do it.

Then, I mapped \\mercury\folder A to the Z drive. And used Z:\ and got the same results. Does it matter that it is a network folder?

Edit: I just tried it locally and had the same results.
 
Joined
Aug 7, 2007
Messages
9,028
Shouldn't make a difference as long as the account it's running under has permissions to create and delete files and folders.

open a Command Prompt first, navigate to the folder where you samved the file, then run it friom there. That way you can see any error messages. May want to comment out the Echo statement so you can see each command as it's run.

I'd suggest you first create a test folder on the Network server to test with rather than using live data, just in case. It's quite possible it could fail to move the files and folders, but still delete the Home folder and all it's contents
 

poleary2000

Thread Starter
Joined
Mar 24, 2004
Messages
33
Ah.....

Got it. I was putting the source level one level too low. Thus, it wasn't finding the folder.

Works perfectly!!!! Beautiful. This will save oodles of time each week. No joke. I wish I could buy you a beer!
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Top