Determine if specific Drive letter is available via batch file - 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: ...

Related

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

Create a simple batch file that extract, install and more

One of my daily task at work, is to extract the content from a downloaded ZIP FILE into a USB STICK drive that must be quickly formatted in FAT32.
After I extract the ZIP File into the USB STICK, there is one file in particular (called setup.bat) that I need to right click and select Run as Administrator.
The final step is to eject the USB STICK from the computer and give the USB STICK to a sales person.
Now my question is, can I create a batch file that does this job for me? I insert the USB STICK drive into the machine and I run the BATCH FILE, everything can be done automatically?
Step #1
Insert the USB Stick (Of course this is a manual step from me.)
Step #2
Run the batch file that we create.
Step #3
The batch file executes: Quick format the USB Stick in FAT32
I do not need to give a name to the volume. It can be empty.
Step #4
The batch file executes: Extract the content from the ZIP FILE located into the downloads folder into the USB Stick.
(The name of the zip file is: plc-stick_8.3)
Step #5
The batch file executes: Run as Administrator a specific file: setup.bat
This is a file located within the package that has been extracted from the zip file into the USB Stick.
Step #6
The batch file executes: Unmount/Eject the USB Stick
Thank you so much in advance for your help and have a beautiful day
sambul35
thank you so much for your help. It is almost done and perfect. I only received a couple of warning message. I am pretty sure this is something that I am missing from your conditions. Any suggestion what I am doing wrong? You can read the execution below. Thank you so much again.
The type of the file system is FAT32.
QuickFormatting 1.9 GB
Initializing the File Allocation Table (FAT)...
Format complete.
1.9 GB total disk space.
1.9 GB are available.
4,096 bytes in each allocation unit.
490,432 allocation units available on disk.
32 bits in each FAT entry.
Volume Serial Number is 2495-4F8F
Waiting for 0 seconds, press a key to continue ...
Unpack completed. Running setup...
The system cannot find the path specified.
'bootinst.bat' is not recognized as an internal or external command,
operable program or batch file.
Install completed
'removedrive' is not recognized as an internal or external command,
operable program or batch file.
All tasks done. Remove Flash Drive.
The batch below performs all the tasks you need. It makes several assumptions:
the USB Thumb drives you process are preformatted at factory with one existing partition
thumbs are always automounted upon hookup to the same drive letter D: . You can change
the drive letter in the script
you downloaded, unpacked and copied to this batch directory
RemoveDrive tool. Its the best way to eject a USB flash drive
from Cmd
you didn't move your user account Downloads folder from its default
location
save this AutoFlash.bat in your user account accessible directory,
create a desktop shortcut to it, select in its
Properties-Shortcut-Advanced option "Run as Administrator"
If these conditioned are met, hook up a USB Thumb, wait until its accessible in Windows Explorer, then run this script by the shortcut to auto perform all your tasks:
<!-- : Begin batch script
#echo off
setlocal EnableExtensions EnableDelayedExpansion
cd /d %~dp0 & set "drive=D:" & set "mount=0" & echo/
set "file=%USERPROFILE%\Downloads\plc-stick_8.3.zip"
if exist %drive%\nul (format %drive% /q /fs:FAT32 /Y /v:USBFlash
) else (set "mount=1" & echo Flash drive is not mounted)
timeout 3
if not %mount% equ 1 (echo/
cscript //nologo "%~f0?.wsf" "%file%" "%drive%\"
echo Unpack completed. Running setup... & echo/
call %drive%\setup.bat
timeout 3 & echo Install completed & echo/
removedrive %drive% -L
echo All tasks done. Remove Flash Drive.
) else (echo/ & echo Reinsert the drive or open Disk Management)
timeout /t 3 /nobreak >nul
exit /b
----- Begin wsf script --->
<job><script language="VBScript">
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set DestFldr=objShell.NameSpace(Ag(1))
Set SrcFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
</script></job>
Let me know if there're any problems running the script.

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

Loop through drive letters and move file to USB

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)?

Compare file creation dates from the command prompt

At my university, we use VMware quite extensively to allow experimentation of admin tasks on otherwise heavily locked down computers. I use the same VM's at home so I don't have to worry about the cluttered environment of my native PC, by copying it to and from my USB flash drive.
The VM I use is snapshot'd with all the settings I like, and set to revert to snapshot on shutdown, so ideally it never changes. As I use multiple computers, I have a script that checks if the VM is present, and if not, copies it over and schedules an auto-open on next login.
My current script looks like this:
#echo off
if exist "C:\Users\[Username]\Desktop\Win7x86\Win7x86.vmx" goto open
if not exist "C:\Users\[Username]\Desktop\Win7x86\Win7x86.vmx" echo VM files not found, preparing to copy from USB
goto usb
:usb
if exist "F:" goto copy
if not exist "F:" echo Please insert USB drive.
pause
goto usb
:copy
robocopy "F:\Virtual Machines\Win7x86" "C:\Users\[Username]\Desktop\Win7x86" /mir /eta
schtasks /create /ru [Username] /rp [Password] /xml "%cd%\Win7x86 Opener.xml" /tn "Win7x86 Opener"
goto open
:open
PATH "%PROGRAMFILES%\VMware\VMware Workstation\"
START vmware.exe -x "C:\Users\[Username]\Desktop\Win7x86\Win7x86.vmx"
My problem is this; sometimes I make change to the VM, and update the snapshot.
I want my script to compare the creation dates of the source files (USB) to the local files (desktop). If the local are newer, then obviously it's been copied since the last update and is fine to run, but if the source files are newer, then the local needs updating.
I plan on using vmware.log as the comparison file as it's small and retains it's creation date (some VM files are deleted and re-created during the VM running process).
I've scoured the internet and the best I found compared dates and listed the newest; but with my very limited batch scripting knowledge, I have no idea how to pipe a robocopy command off that output.
This should do it.
#echo off
for /f "tokens=* usebackq" %%d in (`wmic datafile where Name^="C:\\Users\\[Username]\\Desktop\\Win7x86\\vmware.log" get creationdate ^| findstr ^"[0-9]^"`) do ( set tmpd=%%d )
set vmlocaldate=%tmpd:~0,8%
for /f "tokens=* usebackq" %%d in (`wmic datafile where Name^="F:\\Virtual Machines\\Win7x86\\vmware.log" get creationdate ^| findstr ^"[0-9]^"`) do ( set tmpd=%%d )
set vmusbdate=%tmpd:~0,8%
if %vmusbdate% gtr %vmlocaldate% goto copy
I don't know how this will fit into your script as it will need the USB drive plugged in; at the moment you can launch your and it will straight away open the local virtual machine if it exists, you have to decide if you always need to plug the USB drive in and check the creation date, if you only want to check when if the USB happens to be present, or change your USB prompt to allow you to skip plugging the drive in.
NB. It is significantly easier in both VBScript and PowerShell.

Resources