Please, help with batch files

Discussion in 'backup, imaging & disk mgmt' started by Decopi, Jun 14, 2023.

  1. Decopi

    Decopi Registered Member

    Joined:
    May 13, 2017
    Posts:
    89
    Location:
    USA
    Please, if possible I request help, I need 3 batch files in order to do the following tasks:

    BATCH01
    Go to drive X1:\ => Delete everything (folder, subfolders and files) exept folders "X1:\System Volume Information" and "X1:\$RECYCLE.BIN"
    Go to drive X2:\ => Copy everything (folder, subfolders and files) exept folders "X2:\System Volume Information" and "X2:\$RECYCLE.BIN" => Go to drive X1:\ => Paste everything (folder, subfolders and files)

    BATCH02
    Go to drive X3:\ => Go to folder X3:\Y3 => Delete everything (folder, subfolders and files)
    Go to drive X4:\ => Go to folder X4:\Y4 => Copy everything (folder, subfolders and files) => Go to folder X3:\Y3 => Paste everything (folder, subfolders and files)

    BATCH03
    Go to drive X5:\ => Delete everything (folder, subfolders and files) exept folders "X5:\System Volume Information", "X5:\$RECYCLE.BIN" and "X5:\Whatever"
    Go to drive X6:\ => Copy everything (folder, subfolders and files) exept folders "X6:\System Volume Information", "X6:\$RECYCLE.BIN" and "X6:\WhateverAgain" => Go to drive X5:\ => Paste everything (folder, subfolders and files)

    Thank you in advance
     
  2. xxJackxx

    xxJackxx Registered Member

    Joined:
    Oct 23, 2008
    Posts:
    9,147
    Location:
    USA
  3. Decopi

    Decopi Registered Member

    Joined:
    May 13, 2017
    Posts:
    89
    Location:
    USA
    Thanks for your help.
    I understand you're busy. But I never wrote a batch file.
    I tried to follow the example you kindly shared with me.
    Please, it'll be great if you can give me a hand, confirming me if this (below) will work for the BATCH01 I requested.
    Thks!

    @Echo Off

    SETLOCAL
    SET "sourcedir=F:\"
    SET "keepdir=System Volume Information"
    SET "keepdir="$RECYCLE.BIN"
    FOR /d %%a IN ("%sourcedir%\*") DO IF /i NOT "%%~nxa"=="%keepdir%" RD /S /Q "%%a"

    SETLOCAL
    SET "sourcedir=E:\"
    SET "keepdir=System Volume Information"
    SET "keepdir="$RECYCLE.BIN"
    FOR /d %%a IN ("%sourcedir%\*") DO IF /i NOT "%%~nxa"=="%keepdir%" XCOPY E:\*.* /s F:\

    GOTO :EOF
     
  4. xxJackxx

    xxJackxx Registered Member

    Joined:
    Oct 23, 2008
    Posts:
    9,147
    Location:
    USA
    If you're not in a hurry and someone else doesn't help first I'll see what I can do later. I see you're setting the "keepdir" twice. The second one will probably replace the first one in that example so it will need some tweaking.
     
  5. Decopi

    Decopi Registered Member

    Joined:
    May 13, 2017
    Posts:
    89
    Location:
    USA
    I'll wait for your help.
    Thank you!
     
  6. Brian K

    Brian K Imaging Specialist

    Joined:
    Jan 28, 2005
    Posts:
    12,560
    Location:
    NSW, Australia
    Another method for BATCH03
    X5 is F:\
    X6 is H:\

    Two batch files. Run them as Admin.
    1.cmd is...

    Code:
    @echo off
    CD /d %~dp0
    MD F:\delete
    robocopy F:\ F:\delete /e /move /create /XD Whatever "System Volume Information" Recycler $Recycle.bin
    RD F:\delete /s /q
    
    2.cmd is...

    Code:
    @echo off
    CD /d %~dp0
    robocopy.exe H:\ F:\ *.* /mir /XD Whatever "System Volume Information" Recycler $Recycle.bin /copy:DAT /dcopy:DA /r:0 /v /xj /mt:32
    
    Good luck. I did some rough testing.
     
  7. Brian K

    Brian K Imaging Specialist

    Joined:
    Jan 28, 2005
    Posts:
    12,560
    Location:
    NSW, Australia
    A single batch file will work...

    3.cmd is...

    Code:
    @echo off
    CD /d %~dp0
    MD F:\delete
    robocopy.exe F:\ F:\delete /e /move /create /XD Whatever "System Volume Information" Recycler $Recycle.bin
    RD F:\delete /s /q
    robocopy.exe H:\ F:\ *.* /mir /XD Whatever "System Volume Information" Recycler $Recycle.bin /copy:DAT /dcopy:DA /r:0 /v /xj /mt:32
    
     
  8. Brian K

    Brian K Imaging Specialist

    Joined:
    Jan 28, 2005
    Posts:
    12,560
    Location:
    NSW, Australia
    BATCH02

    Using the same partitions.
    4.cmd is...

    Code:
    @echo off
    RD F:\Y3 /s /q
    robocopy.exe H:\Y4 F:\Y3 *.* /mir /copy:DAT /dcopy:DA /r:0 /v /xj /mt:32 
     
  9. Brian K

    Brian K Imaging Specialist

    Joined:
    Jan 28, 2005
    Posts:
    12,560
    Location:
    NSW, Australia
    BATCH01

    Using the same partitions.
    5.cmd is...

    Code:
    @echo off
    MD F:\delete
    robocopy F:\ F:\delete /e /move /create /XD "System Volume Information" $Recycle.bin
    RD F:\delete /s /q
    robocopy.exe H:\ F:\ *.* /mir /XD "System Volume Information" $Recycle.bin /copy:DAT /dcopy:DA /r:0 /v /xj /mt:32 
     
    Last edited: Jun 15, 2023
  10. Decopi

    Decopi Registered Member

    Joined:
    May 13, 2017
    Posts:
    89
    Location:
    USA
    Hi Brian K!

    Firstly... thousand thanks to you for your great help!!!!!!!!!!!!!!!
    Since yesterday I'm following your answers, but I preferred to wait before replaying you, because I was testing line by line each one of your codes (in order to learn what each line exactly does).

    I tested everything (BATCH 01, 02 and 03), and in general everything works.
    I said "in general" because sometimes, here and there, a folder is not deleted or not copied. I don't know why. But again, in general, your batches are doing 99% of the job.

    Please, if you allow me, I have few simple questions, starting with BATCH01:

    1. Comparing your BATCH01 to your BATCH03 (single batch file version), at BATCH01 you are not using "CD /d %~dp0". Do you confirm?

    2. Again, comparing your BATCH01 to your BATCH03, the semantics changed a little, at BATCH03 we can see "Recycler $Recycle.bin", but in BATCH01 is "$Recycle.bin". Do you confirm?

    Thank you once again!
     
  11. Brian K

    Brian K Imaging Specialist

    Joined:
    Jan 28, 2005
    Posts:
    12,560
    Location:
    NSW, Australia
    Decopi,

    I was using some of my old batch files and didn't notice....

    CD /d %~dp0

    is not necessary.

    Recycler

    is not necessary either.

    These entries will do no harm but aren't needed.
     
  12. Brian K

    Brian K Imaging Specialist

    Joined:
    Jan 28, 2005
    Posts:
    12,560
    Location:
    NSW, Australia
    CD /d %~dp0

    Can be useful in a batch file as it changes the path to the folder where you are running the batch file. This saves you from typing multiple long paths. For example...
    C:\Windows\System32

    becomes

    D:\My_files\_IFLlatest\__tbs boot scripts start here RESTORES\brian diff restore C

    Files can now be easily accessed in that long path.

    %~dp0 is D:\My_files\_IFLlatest\__tbs boot scripts start here RESTORES\brian diff restore C



    Code:
    C:\Windows\System32>pause
    Press any key to continue . . .
    
    C:\Windows\System32>CD /d D:\My_files\_IFLlatest\__tbs boot scripts start here RESTORES\brian diff restore C
    
    D:\My_files\_IFLlatest\__tbs boot scripts start here RESTORES\brian diff restore C>pause
    Press any key to continue . . .
     
  13. Decopi

    Decopi Registered Member

    Joined:
    May 13, 2017
    Posts:
    89
    Location:
    USA
    Perfect! Thanks.

    Please, two more simple questions about BATCH01:

    1. Based on your code, at F: in BATCH01 I don't care about "System Volume Information" and "$Recycle.bin" folders.
    So, from my ignorance, I wonder: In order to make your batch faster, instead of "MD F:\delete" and "robocopy F:\ F:\delete /e /move /create /XD "System Volume Information" $Recycle.bin", what do you think if we replace that part in the batch with "format F:"? I never wrote a batch file, so I could be totally wrong! But I think that by (quick) formatting F:, it will avoid all the "copy/delete" part of your code, which perhaps will be faster with "quick format", and also less I/O on the SSD. Any thoughts?

    2. Is it possible to make this BATCH01 to always run as "administrator"?

    Thks!!!!!!!!!!
     
  14. Brian K

    Brian K Imaging Specialist

    Joined:
    Jan 28, 2005
    Posts:
    12,560
    Location:
    NSW, Australia
    I've never run "format" from a batch file.

    I right click and run most of my batch files as "Run as administrator".
     
  15. Brian K

    Brian K Imaging Specialist

    Joined:
    Jan 28, 2005
    Posts:
    12,560
    Location:
    NSW, Australia
    I've used TBOSDT Pro from a batch file to delete and recreate partitions. I don't think I could do it from a Diskpart batch file. Only manually.
     
  16. Dmitry_rus

    Dmitry_rus Registered Member

    Joined:
    Nov 20, 2016
    Posts:
    18
    Location:
    Russia
  17. Decopi

    Decopi Registered Member

    Joined:
    May 13, 2017
    Posts:
    89
    Location:
    USA
    Hi Dmitry_rus,

    I tested your code, unfortunately I couldn't make it work.
    It asks for name of the volume, and even when I write the name, it does nothing after it.

    However, googling the subject, I found something that works: Format F: /FS:NTFS /V:Whatever /Q /X /y

    Please, do you approve it?
    I never wrote a batch file. Again, I tested "Format F: /FS:NTFS /V:EM2 /Q /X /y" and it works. But I'm blind, I don't know what I'm doing. Please, is "Format F: /FS:NTFS /V:EM2 /Q /X /y" fine?

    Thks!
     
  18. Dmitry_rus

    Dmitry_rus Registered Member

    Joined:
    Nov 20, 2016
    Posts:
    18
    Location:
    Russia
    Please press Win+R, type "cmd" (without quotes) and press Enter. Then type "format /?" (without quotes) in the black console window. It will print out a list of format's parameters. Every command has built-in "help", so don't hesitate to use it.. :)
    V: means volume name, FS: type of file system, Q - quick formatting, /Y - start without confirmation (undocumented?). Your command mentioned above should work fine.
     
  19. xxJackxx

    xxJackxx Registered Member

    Joined:
    Oct 23, 2008
    Posts:
    9,147
    Location:
    USA
    I appreciate tremendously that he was able to step in and help, I was unable to find the time and he is more experienced with this anyway. :thumb:
     
  20. Brummelchen

    Brummelchen Registered Member

    Joined:
    Jan 3, 2009
    Posts:
    6,294
    normally batch = command line cannot delete those two folders because hidden and read only and not sufficient rights, messages need to verbosed. if your batch already do you have messed it up with them.

    and for copying files the command line is sooo slow...
     
  21. Decopi

    Decopi Registered Member

    Joined:
    May 13, 2017
    Posts:
    89
    Location:
    USA
    Thank you for your replay.
    Do you have a better alternative than a batch? Something simple I can implement?
     
  22. Dmitry_rus

    Dmitry_rus Registered Member

    Joined:
    Nov 20, 2016
    Posts:
    18
    Location:
    Russia
    "attrib -h -s -r dir_or_filename" clears hidden/system/readonly attributes and file/directory can be deleted without problems (requires admin privileges). Robocopy with /mt:32 should work smooth enough.
     
  23. Brian K

    Brian K Imaging Specialist

    Joined:
    Jan 28, 2005
    Posts:
    12,560
    Location:
    NSW, Australia
    There is a bug in Robocopy that can sometimes cause the target files/folders to be hidden after copying from the root of a partition. You didn't experience this bug and I haven't seen it for a long while. For example, copying to K:\House from M:\

    When you look in K:\House there is "nothing". Fixed by...

    Code:
    attrib -s -h K:\House
    Now the files/folders are visible.
     
    Last edited: Jun 15, 2023
  24. Brian K

    Brian K Imaging Specialist

    Joined:
    Jan 28, 2005
    Posts:
    12,560
    Location:
    NSW, Australia
    I backup my Data partition with images and Robocopy. There is 400 GB of data and a daily Robocopy backup takes 1 minute.
     
  25. Decopi

    Decopi Registered Member

    Joined:
    May 13, 2017
    Posts:
    89
    Location:
    USA
    Please, may I ask you if your robocopy backup strategy is differential or incremental or anything else?
    Why do you prefer robocopy instead backup software?

    For specific backups, I can't use backup software nor backup windows.
    So, thanks to your great help, now I'm using batches.
    But it is kind of messy, because I have lot of drives, and some of my backups are too specific, with folders that shouldn't be deleted, or folders that don't need backup at all.
    Do you have similar situation with your backup strategy?
     
  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.