问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/38591147/create-a-simple-batch-file-that-extract-install-and-more