I've recently watched a video on youtube that runs a batch file from his flashdrive while on boot mode in the computer, though the quality was bad, I could see he was doing something like e:\ and then the file but when I try to do this, even through cmd on the desktop, it kept showing me
C:\Users\username>e:\hack //the command
File not found - files //the error
Here's the code I was trying to run:
@echo off
:: variables
SET odrive=%odrive:~0,2%
set backupcmd=xcopy /s /c /d /e /h /i /r /y
%backupcmd% "%drive%\files" "C:\Users\%USERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
pause
the pause
is just to see whether there are any errors or not.
The code in batch file is not working because of referencing with %odrive:~0,2%
the first two characters of the environment variable odrive
which is not defined at all, at least not in the batch file itself. And "%drive%\files"
references the value of environment variable drive
which is also not defined in the batch file.
The entire not working batch file as posted can be replaced by:
@%SystemRoot%\System32\xcopy.exe "%~d0\files" "%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup\" /C /D /E /H /I /Q /R /Y >nul
Some comments on this single command line:
%~d0
is replaced during execution by drive letter and colon of the batch file containing this single command line.
There must be a subdirectory files
with the files and folders to copy in root directory of the drive containing the batch file.
It does not really make sense to copy files and folders from a USB storage media into the user's Windows Start menu in folder Startup
for automatic start on Windows start after user login which should contain only *.lnk files. Such an approach is used by malware writers, but not by reliable programmers and their applications.
The options /E
and /S
should be used never at the same time. /S
is for copying all files with subfolders, but without empty subfolders. /E
is for copying all files with all subfolders including empty subfolders. So it does not make sense to specify both at the same time. /E
is used in this case by XCOPY.
On destination being a directory the destination path should end with a backslash making it 100% clear for XCOPY that the destination is a directory. The option /I
is not really needed on specifying destination directory with \
at end.
@
at beginning of the single command line prevents the display of this command line before executing it by cmd.exe
which executes the batch file like @echo off
does for all commands in a batch file.
For even better understanding the used command line and how it works, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
... explains %~d0
xcopy /?
See also the list of predefined Windows Environment Variables.