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.
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 two folders in C: drive.
I want to copy top 15 txt files from one folder to another folder. It is a daily repetitive task so I want to automate the process.
How can I copy those txt using batch script?
This is what I have but it does not work.
xcopy /s "C:\Documents" "C:\research"
#echo off
setlocal EnableDelayedExpansion
set "i=0"
for /F "delims=" %%a in ('dir "C:\Documents" /O:-D') do if !i! lss 15 (
copy "%%a" "C:\research"
set /A i+=1
)
You may also add the /T switch in dir command to select the specific date used (creation, last access or last mod).
I'd check this out:
Batch file to copy files from one folder to another folder
I think this is what you're looking for but I'm not sure it'll guarantee the first 15 files.
Since you don't have spaces the "" aren't required.
xcopy /s C:\Documents\*.txt C:\research\*.txt
I'm having trouble getting my bat file to work correctly. I run two hard drives on my computer and my desktop is on my E: drive. So %Userprofile%\Desktop navigates me to my desktop on E: , but when put into my bat file It tries to create another folder on my C: Drive called Desktop. Exactly the same spelling. But I will be using this on another computer That may or may not have their desktop on their C: drive.
I am copying a file to another folder
xcopy /s "%~dp0\Folder\Folder\Folder\Folder\File.exe" "%USERPROFILE%\Desktop\WorkFiles" /Y
This will attempt to create a Folder called Desktop and drop the exe into that.
The bat is ran on a USB hence the "%~dp0"
Your xcopy command has a trailing backslash included in %~dp0 so there is one too much. Replace with:
xcopy /s /Y "%~dp0Folder\Folder\Folder\Folder\File.exe" "%USERPROFILE%\Desktop\WorkFiles\"
If you relocated your Desktop from the normal position you have to lookup the actual location in the registry (or PowerShell/vbscript user shell folders)
If the Desktop is not relocated, this batch will nevertheless get the proper location:
Set "Key=HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
Set "Val=Desktop"
For /F "Tokens=2*" %%A in (
'Reg Query "%Key%" /v "%Val%" 2^>Nul ^| Find "%Val%" '
) Do Set "Desktop=%%B"
Echo 'Desktop' location: %Desktop%
The better way to evaluate the Desktop on varying windows systems is to use eryksuns version:
#Echo off
for /f "usebackq delims=" %%a in (
`powershell -c "[environment]::GetFolderPath('Desktop')"`
) do set "desktop=%%a"
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
I tried to make a script following this topic
batch script to find drive letter of a mounted device
but there is no real success.
This is the script:
SETLOCAL EnableDelayedExpansion
for /f "usebackq tokens=1,*" %%i in (`fsutil fsinfo drives`) do (
if exist %%j dir /S /D "%%j*hurrdurr.txt" >> fud_india.com_%computername%.txt
)
pause
Problem is i get the result, but only for 1 drive letter...
Anyone maybe can help here?
#echo off
setlocal enableextensions disabledelayedexpansion
>"fud_india.com_%computername%.txt" 2>nul (
for /f "delims=: tokens=1,*" %%a in ('fsutil fsinfo drives') do (
for %%c in (%%b) do vol %%~dc && dir /s /d "%%~dc\hurrdurr.txt"
)
)
The for /f just processes lines, in this case the output of the fsutil command, and is able to split those lines in tokens using the indicated set of delimiters. As the fsutil returns all the drives in one line, something like
Drives: C:\ D:\ E:\
and without knowing how many drives can be in the line, we can not (more or less) indicate how many tokens we want. So, we need to get the line, separate the header, grab the list and iterate over the elements in the list (the drives)
The first step is to separate the header from the list of drives. This is directly done in the for /f %%a via the tokens and delims clauses. We will use the colon as a delimiter to split the line in tokens.
We are requesting two tokens: ther first token (%%a) will store the text up to the first delimiter (the header) and the second token (%%b) will store the rest of the line (this is what the * means in the tokens clause)
With the list of drives stored in %%b we need a way to iterate over it. This is the reason for for %%c in (%%b) .... When %%b is expanded, the resulting command will be
for %%c in (C:\ D:\ E:\) do ...
For each element in the list a vol command is used to determine if the drive is available and if no errorlevel is set then the dir command is executed.
As the vol command only accepts a drive reference, without path, we need to remove the ending backslash from the elements in the list, so instead of directly using %%c, we use %%~dc, that is, the drive of the element being referenced in %%c.
The full for loop is enclosed in parenthesis and redirected so the output file only needs to be opened/closed once.
your %%jcontains C:\ D:\ E:\ F:\ G: (for me), this makes your if:
if exist C:\ D:\ E:\ F:\ G:\ dir ...
obviously wrong syntax.
I suggest another method (wmic):
for /f "skip=1" %%i in ('wmic logicaldisk get caption') do (
dir /s /d %%i\*hurrdurr.txt" >>fud_india.com_%computername%.txt 2>nul
)
By getting rid of the if exist, you avoid the GUI-Box "no media"; >nul just sends the dir-error to nirvana.