Loop through drive letters and move file to USB - loops

I have a back up batch file that will backup my files and create a zip file out of them, then move this zip file to a USB drive that will be plugged into the PC, the USB drive could be allocated a drive letter between E to H, dependant on what else is using the ports, the below code works on a Windows 7 PC but not on a Windows XP machine, which the Batch File is going to be used on
The USB drive letter changes depending on which machine I'm backing up and /or how many devices are connected and using drive letters, I don't want to get a choice, just move the file to whatever drive is plugged in as there will only ever be one drive plugged in at a time as it is only used for back up purposes
How can I make this work on an XP machine, it will only be used on an XP machine, also can I throw up and error if a USB drive is not found and delete the zip file, as this is all running from a CF card, embedded system, so space is limited to save any files
REM ------ Creation of a date stamp for the ZIP file on the USB and set the File Name Structure------
set DT=%DATE:/=-%
set timestamp=%DT:~7,3%%DT:~4,3%%DT:~10,4%
Set FileStamp=%BackupName%-%timeStamp%
REM ------ Creation of the ZIP file ------
%SupervisorPath%\7-ZipPortable\App\7-Zip\7z a -tzip %BackupPath%\Backup\%FileStamp%.zip %BackupPath%\Backup\
REM ------ Move the backup file to a USB drive with File Name and Date Stamp ------
for %%D in (E F G H) do if exist %%D: (
echo Moving files to USB drive %%D:
move /y "%BackupPath%\Backup\%FileStamp%.zip" %%D: >nul && (
echo Files moved to USB drive successfully
goto :break
)
)
:break

Put on your USB drive in root a file for example with name BackupDrive.txt. The content does not matter, but I suggest to store in the file something like
File to identify the USB backup drive. Never delete this file.
Set the hidden file attribute on this file.
In your batch file use:
for %%D in (E F G H) do if exist %%D:\BackupDrive.txt (
echo Moving files to USB drive %%D:
move /y "%BackupPath%\Backup\%FileStamp%.zip" %%D: >nul && (
echo Files moved to USB drive successfully
goto :break
)
)
Or most likely better, you give your USB drive a unique label and find the drive by label, see
How to reference a volume/drive by label?
How to refer to/select a drive based only on its label (i.e. not the drive letter)?

Related

trouble with letters in .bat command to copy files from a dvd

I created a .bat file to replace 3 files from a DVD burned into a directory on my PC, the directory in all others the path is fixed, however it has a complication in case of my driver cd player and dvd the letter is j. But in other drivers when inserted the dvd of course the letter will change to f or g or i for example, how to solve this? How exists? because the Type the folder where it will be copied this defined in problem, but how to get and copy all files of this folder specify in or add variants and search type let's assume that the driver is with the letter n as I put in bat to find this folder with those Files and paste inside the folder that I specified? Is it possible?
I have the following .bat: copy j:\dvd folder\*.* "c:\file of destination in the pc. This .bat file recognizes the drive J as if it were in any other PC in a universal way let's say, regardless of which PC the bat is fired it will go into that DVD folder will copy and paste in the specified folder in c: windows windows folder Is not the problem, the problem is fixed the problem is this other unit of dvd in which the letters of the unit change from there will not work when clicking this bat because it is in my pc for example in another it will be i, this is the problem.
You may be able to leverage the WMI command line:
#Echo Off
Set "CDROM="
For /F "Skip=1 Delims=" %%A In (
'"WMIC CDROM Where (MediaLoaded='TRUE') Get Drive 2>NUL"'
) Do For %%B In (%%A) Do If Exist "%CDROM%\DVD Folder\" Set "CDROM=%%B"
If Not Defined CDROM GoTo :EOF
Copy "%CDROM%\DVD Folder\*.*" "%SystemDrive%\Somewhere Existing"
Timeout -1
Think from another perspective.
#echo off
copy "%~Dp0Dvd Folder\*.*" "C:\Folder\"
rem We don't need \ in between %~Dp0 and Dvd Folder because %~Dp0 comes with a \
Place this batch file in your DVD root folder.
Explanation:
%~Dp0

Determine if specific Drive letter is available via batch file

We use batch files to back up files or folders to external Media (such as a flash drive, or Rewritable CD-ROM or DVR disk). I will start with USB flash drive.
We set the desired drive letter in our startup so it's part of the environment: Set ZDRIVE1=F
So, want to be able to check if %ZDRIVE1%: is available, and if not, prompt user to insert the flash drive, retest, them move to the part where we start copying files to the designated flash drive.
We had a utility called dskquery that basically did this, but no one can find the source so we can recompile for 32/64 bit compatibility.
#echo off
Set "ZDRIVE1=F"
:test
pushd %ZDRIVE1%:
if "%errorlevel%" == "0" (
echo the drive is aviable.
pause
goto :eof
) else (
echo please insert the flash drive.
pause
)
goto test
replace goto :eof on line 8 with goto yourlabel
also the drive of a USB flash drive can be G:,H: and so on, not necessary F: ...

Batch Script - Detect Drive letter and Execute a File Transfer

I am trying to create a simple batch script to test the Data transfer between two Thumb Drives connected to a Test PC. This is how the test must proceed:
Detect if the USB Drives are connected to the PC.
If connected, detect the drive letters and assign them as Drive 1 and Drive 2
Test the Data Transfer by moving a file from the PC's Hard Drive to Drive 1. Move the same file from Drive 1 to Drive 2. Move the same file from Drive 2 back to the PC.
Repeat the same test by moving a file from the PC's Hard Drive to Drive 2. Move the same file from Drive 2 to Drive 1. Move the same file from Drive 1 back to the PC.
So far have managed to create the script (Script1) to detect the drive letters. I also have the script (Script2) to transfer the file as described in Points 3 and 4 (But here, the drive letters are fixed). However, I am not able to use the detected drive letters to transfer my file.
I am looking for a way to combine Script1 and Script2. Is there a way to do this? Is there a way to use the detected drive letters and execute the data transfer between the two?
It would be great if you could suggest an alternative solution as well.
Script1 to detect the drive:
#echo off
for /F "tokens=1*" %%a in ('fsutil fsinfo drives') do (
for %%c in (%%b) do (
for /F "tokens=3" %%d in ('fsutil fsinfo drivetype %%c') do (
if %%d equ Removable (
echo Drive %%c is Removable (USB^)
)
)
)
)
Script2 is a simple script to move a file (fixed name) from one drive to the other:
#echo off
Echo Moving File from PC to USBDrive1
move E:\Test1.txt N:\
Echo Moving File from USBDrive1 to USBDrive2
move N:\Test1.txt D:\
Echo Moving File from USBDrive2 to PC
move D:\Test1.txt E:\

Adding if exist to an existing batch file

I am trying to add additional functionality to an existing batch file, the below snippet of code copies my data from one location to another and then deletes the zip file that was created, all works well.
what I want to do is if the file already exists on the USB drive to add an incremental number to the end of the file eg. FileName-FileDate(1) or FileName-FileDate(2) I could add time to the FileStamp which at the moment adds the date but this could make the file name a bit long.
I am copying the file to a USB drive as a backup, this is why I am using drives E, F, G, H, dependant in what Windows sees the drive as, it is also important that the created file on the C: drive is deleted due to space.
I am using the /y to reduce user input as it will be done by people with limited technology skills so I don't really want to use /-y. also the machine does not have a keyboard, only a touch screen, so renaming the file is not an easy thing to do.
REM ------ Creation of the ZIP file ------
%SupervisorPath%\7-ZipPortable\App\7-Zip\7z a -tzip %BackupPath%\Backup\%FileStamp%.zip %BackupPath%\Backup\
REM ------ Copy the backup file to a USB drive with File Name and Date Stamp ------
IF EXIST E: (echo copying files to USB drive E:
copy %BackupPath%\Backup\%FileStamp%.zip E: /y )
IF EXIST F: (echo copying files to USB drive F:
copy %BackupPath%\Backup\%FileStamp%.zip F: /y )
IF EXIST G: (echo copying files to USB drive G:
copy %BackupPath%\Backup\%FileStamp%.zip G: /y )
IF EXIST H: (echo copying files to USB drive H:
copy %BackupPath%\Backup\%FileStamp%.zip H: /y )
REM ------ Delete the temporary zip file from the backup folder ------
echo Deleting temporary zip file from the backup folder
Del %BackupPath%\Backup\%FileStamp%.zip
IF EXIST does not work on drive letters and directories, but fortunately, there is an obscure workaround:
C:\> IF NOT EXIST D:\NUL ECHO D:\ not present.
D:\ not present.
C:\> IF EXIST C:\NUL ECHO C:\ exists.
C:\ exists.
It turns out that to support constructs like appending >NUL on command statements, there is a sort of virtual file named "NUL" in every directory including the root directory of a drive. Checking for its existence is equivalent to a check for the directory or drive's existence.
This behavior is documented in a Microsoft knowledge base article ( https://support.microsoft.com/en-us/kb/65994 ) and I have confirmed its behavior on FreeDOS 1.1 and in a Windows 7 command shell.
Though the KB article addresses checking for directory existance, it also indicates the technique can be used to see if a drive is present. In the case of checking for drive existence, however, caveats exist:
An Abort, Retry, Fail? error occurs if the drive is not formatted.
Using this technique to check for drive existence depends on device driver implementation and may not always work.

Move file to a changeable drive location if fitted "for a in range" in XP

How to move a file to a USB drive that has a variable drive letter range dependant on the machine it could be drive E, F, G or H, in Windows Embedded XP, there is only ever one USB drive installed at a time, so it can move only if it is fitted, I can create the file and it moves in Windows 7 but not in Windows Embedded XP, what are the differences in the options available for this in XP, the script will only be used on XP machines.
REM ------ Creation of the ZIP file ------
%SupervisorPath%\7-ZipPortable\App\7-Zip\7z a -tzip %BackupPath%\Backup\%FileStamp%.zip %BackupPath%\Backup\
REM ------ Move the backup file to a USB drive with File Name and Date Stamp ------
for %%A in (E F G H) do if exist %%A: (
echo Moving files to USB drive %%A:
move /y "%BackupPath%\Backup\%FileStamp%.zip" %%A: >nul && (
echo Files moved to USB drive successfully
goto :break
)
)
:break
Can I also create an error message if the file is not moved and then delete the file, as it takes up valuable space on the drive?
Here is a solution I use. There is a requirement that the USB drive has been named and you know it. So lets say your USB is named "8GB"
If you run the following command:
wmic logicaldisk list brief
You get a list of your drives including the VolumeName.
Using this list you can pipe it to the Find command like so:
wmic logicaldisk list brief | find "8GB"
Which will return all information about your drive with the VolumeName 8GB. It will look something like this.
C:\>wmic LOGICALDISK LIST BRIEF | FIND "8GB"
F: 2 3080192 8082407424 8GB
Now with this command we can take it further and redirect its output to a file. Like so.
wmic logicaldisk list brief | find "8GB" > C:\tmp\usbdriveinfo.txt
After the information we want has been stored we can read it back into a variable using:
set /p driveLetter=C:\tmp\usbdriveinfo.txt
Now that variable has the whole string but we only want the drive letter so we shorten it like so:
set driveLetter=%driveLetter:~-,2%
Now the variable driveLetter contains just your drive letter "F:"
So if you want it all together:
wmic logicaldisk list brief | find "8GB" > C:\tmp\usbdriveinfo.txt
set /p driveLetter=C:\tmp\usbdriveinfo.txt
set driveLetter=%driveLetter:~-,2%
As for checking if the move command fails. If any command fails move included they set the variable errorlevel to some value other than 0 (0 is for successful execution) Therefore all you need to do is add an if statement after the move command like:
if %errorlevel% GTR 0 del %BackupPath%\Backup\%FileStamp%.zip

Resources