1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Batch to move files and delete folder

Discussion in 'DOS/Other' started by poleary2000, Apr 11, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. poleary2000

    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
     
  2. TheOutcaste

    TheOutcaste

    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?
     
  3. poleary2000

    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.
     
  4. poleary2000

    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.
     
  5. TheOutcaste

    TheOutcaste

    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%
    ) 
    
     
  6. poleary2000

    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.
     
  7. TheOutcaste

    TheOutcaste

    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
    
    
     
  8. TheOutcaste

    TheOutcaste

    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.
     
  9. poleary2000

    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​
     
  10. TheOutcaste

    TheOutcaste

    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
     
  11. poleary2000

    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.
     
  12. TheOutcaste

    TheOutcaste

    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
     
  13. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Are you setting the source directory in the batch file?
     
  14. poleary2000

    poleary2000 Thread Starter

    Joined:
    Mar 24, 2004
    Messages:
    33
    Yeah, I've set it and checked it like 5 times. I'll try the command prompt now.
     
  15. poleary2000

    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!
     
  16. Sponsor

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/916240

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice