0

I try to write a batch file which:

  1. Set one drive online
  2. Backup files
  3. Set drive back offline

I have a batch file that sets the drive online, one file taking it offline and a batch file that robocopys the files I want.

BUT

  1. I dont get them together. I have now three files and have to wait for one getting done to start the next one.
  2. robocopy isnt done til the drive is set offline again. I dont know how I get the offline command waiting for robocopy to be done.

File for drive online (.bat):

diskpart /s C:\Users\USERNAME\Desktop\FestplatteDiskpartON.txt
list disk
select disk 1
online disk
diskpart > exit

File for copy (.bat) / file :

robocopy D:\Tastaturen\ E:\ /MIR

File for drive offline (.bat):

diskpart /s C:\Users\USERNAME\Desktop\FestplatteDiskpartOFF.txt
"C:\Program Files\HDDScan\HDDScan.exe" "\\?\scsi#disk&ven_&prod_st2000dm008-2fr1#4&39693902&0&020000#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}" -APM 1 -PM 0 -Z
list disk
select disk 1
offline disk

I tried:

@ echo off
start /wait "" "C:\Users\USERNAME\Desktop\Festplatte online.bat"

:: Start BackupVorgang
start /wait "" "robocopy D:\Tastaturen\ E:\ /MIR"

diskpart /s C:\Users\USERNAME\Desktop\FestplatteDiskpartOFF.txt
"C:\Program Files\HDDScan\HDDScan.exe" "\\?\scsi#disk&ven_&prod_st2000dm008-2fr1#4&39693902&0&020000#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}" -APM 1 -PM 0 -Z

echo done

In this szenario backup doesnt work. "Cant find robocopy".

I also tried with timeout /x after setting online.

I dont know how I can get the batch file setting the drive online, THAN copy, wait for the copying done and than set the drive offline.

Joeykenoy
  • 3
  • 3
  • Modify the first line to `@echo off` without the space. Replace the second line by `%SystemRoot%\System32\diskpart.exe /s "%UserProfile%\Desktop\FestplatteDiskpartON.txt"`. Replace `:: Start BackupVorgang` by `rem Run backup process` and replace the next line by `%SystemRoot%\System32\robocopy.exe D:\Tastaturen E:\ /MIR /NDL /NFL /NJH /NJS /R:3 /W:5`. The fifth non-empty line should be `%SystemRoot%\System32\diskpart.exe /s "%UserProfile%\Desktop\FestplatteDiskpartOFF.txt"`. The command line with `HHDDScan.exe` is 100% as also the last line with `echo done`. – Mofi Nov 27 '21 at 20:45
  • I recommend to move the batch file for example to directory `%UserProfile%\Scripts` which of course must be first created by you. Cut and paste into that directory also `FestplatteDiskpartON.txt` and `FestplatteDiskpartOFF.txt` and reference these two text files with `"%~dp0FestplatteDiskpartON.txt"` and `"%~dp0FestplatteDiskpartOFF.txt"` instead of `"%UserProfile%\Desktop\"`. `"%~dp0"` expands to drive and full path of argument 0 which is always the batch file. The full batch file path ends always with a backslash. Run in a command prompt window `call /?` for help on this syntax. – Mofi Nov 27 '21 at 20:52
  • I try the tipp with the .txt files. Is there a possibility to write the things in the txt in the batch? Wow Mofi.. Just wow. I googled hours and you fixed it so fast. Thx a lot. – Joeykenoy Nov 27 '21 at 20:53
  • A hint for the future. If the Windows command processor `cmd.exe` processing a batch file starts an executable like `diskpart.exe`, `robocopy.exe` or `HDDScan.exe`, it always waits for the self-termination of the started executable before it reads the next line from batch file, processes and executes it. It does not matter if the executable is a Windows console or Windows GUI program. The command `start` is for starting an executable as a separate process to be able to continue batch file processing while the executable is running, except the option `/wait` is used. – Mofi Nov 27 '21 at 20:56
  • Every [Windows command](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands) can be executed in a command prompt window with `/?` as argument to get displayed the usage help. Run `diskpart /?` and there can be read that only two options are supported: `/?` for showing the usage help and `/s` with the name of a script file to execute several __DISKPART__ commands in a sequence. So it is not really possible to use __DISKPART__ without using the text files. – Mofi Nov 27 '21 at 20:59
  • It would be possible to dynamically create the two text files by the batch file itself, i.e. use `(echo select disk 1&echo online disk)>"%TEMP%\%~n0.tmp"`. Then use `%SystemRoot%\System32\diskpart.exe /s "%TEMP%\%~n0.tmp"`. For setting the drive back offline can be used `(echo select disk 1&echo offline disk)>"%TEMP%\%~n0.tmp"` and running once again `%SystemRoot%\System32\diskpart.exe /s "%TEMP%\%~n0.tmp"`. The next line should be `del "%TEMP%\%~n0.tmp"` to delete the temporary created script file for __DISKPART__. (This suggestion was not tested by me.) – Mofi Nov 27 '21 at 21:09
  • The dynamically creation Ill try tomorrow. All solutions worked. Thx so much for your help. – Joeykenoy Nov 27 '21 at 23:03

0 Answers0