# Batch to move files and delete folder



## poleary2000 (Mar 24, 2004)

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


----------



## TheOutcaste (Aug 8, 2007)

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*:

```
C:\SOURCE
&#9474;   File1.txt
&#9474;
&#9492;&#9472;&#9472;&#9472;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 (Mar 24, 2004)

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 (Mar 24, 2004)

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.


----------



## TheOutcaste (Aug 8, 2007)

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.

```
@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 (Mar 24, 2004)

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.


----------



## TheOutcaste (Aug 8, 2007)

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:

```
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:

```
C:\SOURCE
¦   File0.txt
¦   File1.txt
¦   File2.txt
¦   File3.txt
¦   File4.txt
¦   File5.txt
¦   File6.txt
+-----SubFolder1
+-----SubFolder2
+-----SubFolder3
+-----SubFolder4
+-----SubFolder5
```


----------



## TheOutcaste (Aug 8, 2007)

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 (Mar 24, 2004)

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​


----------



## TheOutcaste (Aug 8, 2007)

Give this a try, use some test data of course:

```
@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 (Mar 24, 2004)

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.


----------



## TheOutcaste (Aug 8, 2007)

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


----------



## Squashman (Apr 4, 2003)

Are you setting the source directory in the batch file?


----------



## poleary2000 (Mar 24, 2004)

Yeah, I've set it and checked it like 5 times. I'll try the command prompt now.


----------



## poleary2000 (Mar 24, 2004)

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!


----------



## TheOutcaste (Aug 8, 2007)

Just make sure none of the files are being used when it's run, it doesn't verify that it was able to move them, and will delete the Home folder even if not empty. Guess it wouldn't be too hard to add a check to make sure the Hiome folder is empty before removing it. If not, log the path to a file, and open it in notepad when done if there was a problem


----------



## Squashman (Apr 4, 2003)

poleary2000 said:


> Works perfectly!!!! Beautiful. This will save oodles of time each week. No joke. I wish I could buy you a beer!


http://www.yougotbeer.com/ygb/home.jsf


----------



## poleary2000 (Mar 24, 2004)

My goodness, it works so fast too. Very nice indeed!


----------



## TheOutcaste (Aug 8, 2007)

OK, here's version three. This one won't remove the Home folder if it's not empty, so if a file was in use, or is set with hidden or system attribute, or some other glitch caused the files to not all be moved, it won't delete the folder.
If will list the path of the folder that is not empty in a log file in the same folder as the batch file, and will open it in notepad when the program finishes.
If Notepad doesn't open, then there were no errors:


```
@Echo Off
Set _Source=C:\Temp Dir\Source
Set _FindDir=Home
Set _ErrLog=%~dp0Errlog.txt
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
>"%_ErrLog%" Echo.Batch started on %date% at %time%
Set _ErrFlag=0
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\"
    Set _Empty=
    For /F "Tokens=* Delims=" %%E In ('RD "%_Source%\%%A\%_FindDir%" 2^>^&1') Do (
      If /I "%%E"=="The directory is not empty." (Set _Errflag=1) & Echo.The folder "%_Source%\%%A\%_FindDir%" is not empty, did not delete it>>"%_ErrLog%")
  ) Else (
    Echo There is no %_FindDir% folder in %_Source%\%%A\
  ))
PopD
If %_ErrFlag%==0 Goto :EOF
Start "" Notepad "%_ErrLog%"
```


----------

