my bat file unhide the USB data when i double click on it.
i want this file to run in background and execute that unhide code only once when USB is connected as running the unhide code in a loop for the same USB drive is useless. how to improve the code ?
#echo off
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype
2^>NUL`) do (
if %%l equ 2 (
echo %%i is a USB drive.
attrib -a -r -h -s /s /d %%i/*.*
)
)
as for part about running once, i would make a file, that would signal whether the operation was done. e.g.
rem check file on USB
....
rem if not there
...
rem do operation
rem and create file on USB
Related
I want to create a For loop to look if a program is on a flash drive, and copy a text file if it is. Since the drive letter varies on every PC, I want it to look at every drive letter.
if exist "D:\Test.exe" (
copy "%FileName%.txt" "D:\" >nul
)
if exist "E:\Test.exe" (
copy "%FileName%.txt" "E:\" >nul
)
if exist "F:\Test.exe" (
copy "%FileName%.txt" "F:\" >nul
)
if exist "G:\Test.exe" (
copy "%FileName%.txt" "G:\" >nul
)
Rem ...Continues possibly until drive Z or once it finds the file
Is there a way to create a For loop to increment the drive letter so I don't have to make an If statement each time?
Here is a community wiki with all methods combined from the comments, per user.
Squashman
#echo off
FOR %%G IN (A B C D etc..Z) DO IF EXIST "%%G:\test.exe" copy
then, a very intuitive method using the exit codes 65 to 90 in ascii format, by Aacini.
#echo off
setlocal enabledelayedexpansion
for /L %%i in (65,1,90) do cmd /C exit %%i & if exist "!=ExitCodeAscii!:\test.exe" copy...
Then my suggestion of using wmic to find all the actual drive letters available on the device:
#echo off
for /f "tokens=2*delims==" %%i in ('wmic logicaldisk get caption /value') do for /f "delims=" %%d in ("%%i") do if exist "%%d\test.exe" copy ...
I have a PC in which I insert a USB Drive and execute a batch script, but the USB Drive interacts with the computer, so I need to know in which Drive Letter it is mounted.
I tried this
for /f "tokens=2,3 delims= " %%A in ('WMIC logicaldisk where "DriveType=2" get /value | find "Caption="') do (
set drive=%%A
)
But I get an error stating Did not expect %%A at this moment.
I know this works
#echo off
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype
2^>NUL`) do (
if %%l equ 2 (
echo %%i is a USB drive.
)
)
But it implies having to save it in a .bat file, and I don't want to do that.
Ideally, what I want to do is to be able to switch to the USB Drive without needing to launch it from a .bat file, so this command should allow me to get the Drive Letter. Afterwards, the script continues.
Thanks to the #aschipfl I managed to find out the right command:
for /F "usebackq tokens=1,2,3,4 " %i in (`WMIC logicaldisk where "DriveType=2" get /value ^| find "Caption="`) do (set drive=%i)
set drive=%drive:~8,9%
And now %drive% is the letter in which the drive is mounted. If there are more than one mounted letters, it will be the last one.
I need to include an "else echo No USB detect" into this .bat (not add "if %%l NEQ 2")
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (
if %%l equ 2 (
echo USB detect in %%i
)
)
In simple words: if USB detected, then "echo OK". But if it does not detect the USB, then "echo No" and exit. Thanks
Solved by Tim
Important Note:
This script (by Stephan) in my opinion, is much higher to detect drives
#echo off
setlocal enabledelayedexpansion
REM get removable loaded drives:
for /f %%a in ('"wmic logicaldisk where (drivetype=2 and size is not null) get caption,size 2>nul|find ":""') do set usb=!usb! %%a
REM show a overview:
if defined usb (echo removable drives found in: %usb%) else echo no removable drive found
REM show them one-by-one:
for %%a in (%usb%) do echo removable drive found in %%a
wmic has more power than you think:
#echo off
setlocal enabledelayedexpansion
REM get removable loaded drives:
for /f %%a in ('"wmic logicaldisk where (drivetype=2 and size is not null) get caption,size 2>nul|find ":""') do set usb=!usb! %%a
REM show a overview:
if defined usb (echo removable drives found in: %usb%) else echo no removable drive found
REM show them one-by-one:
for %%a in (%usb%) do echo removable drive found in %%a
Your question was adding an ELSE clause to your IF statement. I recommended adding ELSE between your two close parenthesis. This code does exactly that:
#echo off
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (
if %%l equ 2 (echo USB detect in %%i) ELSE (echo No USB Detect)
)
This is my output with no USB drives plugged in:
C:\Users\tim\Documents\Scripting Tools>usbdetect.bat
No USB Detect
No USB Detect
No USB Detect
This is my output with a thumbdrive plugged in:
C:\Users\tim\Documents\Scripting Tools>usbdetect.bat
No USB Detect
No USB Detect
USB detect in D:
No USB Detect
Here is my result of the WMIC command you used:
C:\Users\tim\Documents\Scripting Tools>wmic logicaldisk get caption,description,drivetype
Caption Description DriveType
C: Local Fixed Disk 3
D: Removable Disk 2
You might notice it echos No USB Detect 3 times. This is correct because the FOR loop you have constructed loops through each line of output. WMIC outouts one line with the headers (Caption, Description, and DriveType), a line for each drive (and C: is not DriveType 2), and then a blank line.
Your last line, In simple words: if USB detected, then "echo OK". But if it does not detect the USB, then "echo No" and exit. Seems to imply that you only want a simple Installed/Not Installed, not a yes/no for each line of output from your WMIC query. If that is the case, you don't need (or want) an ELSE. Try this instead:
#echo off
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (
if %%l equ 2 (
echo USB detect in %%i
goto RestOfBatch
)
)
echo USB not detected
:RestOfBatch
Without a usb drive plugged in:
C:\Users\tim\Documents\Scripting Tools>usbdetect.bat
USB not detected
And with a usb drive plugged in:
C:\Users\tim\Documents\Scripting Tools>usbdetect.bat
USB detect in D:
If neither of these produce the desired result, please define your issue more clearly. http://ss64.com/nt/ has a comprehensive list of batch commands and their syntax. You can also add /? to the end of a command to get examples. EG: IF /?
This code works for me. Save it to test.bat and run from open Cmd Prompt. It prints only disk related info, no garbage:
#echo off
setlocal enabledelayedexpansion
for /F "usebackq tokens=* skip=1" %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (set "dsk=%%i"
if "!dsk:~-12,1!" equ "2" (echo USB disk detected in !dsk:~0,2!
) else if "!dsk:~-12,1!" gtr "2" (echo No USB disk detected in !dsk:~0,2!) )
exit /b
I on windows 8.1. I get USB with hidden file/folders. To unhide them I use attrib command . I want to run the commands by simply inserting the USB. Pl help.
echo off
echo Please have patience!!! Wait or Minimise the window!!!
rem c:\script\unhide.bat
#echo off
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (
if %%l equ 2 (
echo %%i is a USB drive.
)
)
Pause
Here I got the drive letter H. I am unable to use drive letter and use the following commands in USB. How can is I run below commands in USB. I mean Change Drive, run attrib command in USB, delete unwanted files from USB and see USB's contents.
cd\
attrib -s -h -r /s /d
del *.lnk
del thumbs.db
del desktop.ini
del autorun.inf
echo Your Folders has been recovered!!! Check your folders and files
dir
pause
exit
#ECHO OFF
SETLOCAL enableextensions
echo Please have patience!!! Wait or Minimise the window!!!
rem c:\script\unhide.bat
for /F "skip=1 tokens=1-3" %%i in ('
wmic logicaldisk where "drivetype=2" get caption^,drivetype^,SystemName
') do (
if "%%j"=="2" (
echo "%%i" is a USB drive ^(DriveType=%%j^).
pushd "%%i\"
SETLOCAL enabledelayedexpansion
echo current folder !CD!
ENDLOCAL
echo attrib -H -S -T /S /D /L >NUL 2>&1
echo del *.lnk 2>NUL
echo del thumbs.db 2>NUL
echo del desktop.ini 2>NUL
echo del autorun.inf 2>NUL
echo Your Folders has been recovered!!! Check your folders and files
dir
pause
popd
)
)
ENDLOCAL
goto :eof
Note:
wmic command changed as follows:
where clause;
description omitted as this property vary in word number and therefore breaks the tokenization;
always non-empty property SystemName appended to pass over the ending carriage return in the line returned: wmic behaviour = each output line ends with 0x0D0D0A (<CR><CR><LF>) instead of common 0x0D0A (<CR><LF>). For another (general) approach see Dave Benham's WMIC and FOR /F: A fix for the trailing <CR> problem;
for /F loop adapted according to altered wmic command (and skip=1);
operational attrib -H -S -T /S /D /L is merely echoed for debugging purposes; remove echo no sooner than debugged (the same for all del commands);
used pushd - popd pair: PUSHD changes the current directory/folder and stores the previous folder/path for use by the POPD command;
folder System Volume Information should keep attributes System & Hidden if present.
I want to do following in batch file:
I want to open pdf, if it exist on usb. If this file don't exist, start pdf from another location, if this exist.
e.g.
:: Searchs USB Drive Letter
For %%I In (G,D,E,F,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z) Do If Exist "%%I:\Document1.pdf" (
xcopy /H /Y /C /R "%%I:\Document1.pdf" "%tmp%"
start %tmp%\Document1.pdf
)
I want to to open pdf document (from USB) if inserted. Once this .bat launched, and usb will inserted, launch pdf from temp folder. I want this without any error messages on batch.
From what you have posted, I can only guess you are trying to run your batch file when a USB drive is inserted?
I'm pretty sure there is no native way to do this in batch, so I can only suggest you use an infinite loop on your batch and keep it running all the time, to detect any that get plugged in.
:LOOP
:: Searchs USB Drive Letter
For %%I In (G,D,E,F,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z) Do (
If Exist "%%I:\Document1.pdf" (
xcopy /H /Y /C /R "%%I:\Document1.pdf" "%tmp%"
start %tmp%\Document1.pdf
)
)
REM Use timeout /t 5 instead of ping if you have Vista or above
ping 127.0.0.1 -n 5
goto :LOOP
and rather than loop all possible drive letters, I would suggest changing your for loop to this instead, so it only looks at drives that currently exist
for /f "skip=2 delims=" %%I in ('wmic logicaldisk get caption') do (
I have skipped the first 2 lines, as the first line is the column title and the second is 99% sure to be the C:\ drive which you didn't include in your script.