Copy same root directory with files to multiple wildcard directories including files - xcopy

I am trying to copy one folder and its contents to multiple user directories which vary depending on the username. The directory under each user will remain constant.
Here is an example of what I am trying to achieve:
xcopy "C:\OF" "C:\Users\*\AppData\Roaming" /O /X /E /H /K /S
I am trying to use a wildcard because the username is different, but xcopy apparently cannot use wildcards any longer?
The directory of "C:\OF" will have files and other nested directories and I want to place those under the "Roaming" directory.
Thank you for any help and explanation of what I am doing wrong.

Put this in a batch file:
#ECHO OFF
FOR /d %%I IN (C:\Users\*) DO (
XCOPY "C:\OF" "%%I\AppData\Roaming"/O /X /E /H /K /S
)
That should do want you want. I didn't validate your XCOPY switches because I stopped using XCOPY years ago in favor of ROBOCOPY.

This will do the trick.
#echo off
for /d %%x in (C:\Users\*) do xcopy "C:\OF" "%%x\AppData\Roaming\OF\*" /d /e
pause
EXIT

Related

How to use xcopy to add the date in the destination file?

This is my current code
xcopy "C:\Users\Asus\Desktop\Test\Test.MDB" "C:\Users\Asus\Google Drive\" /Y /H /E /F /I
exit
I need the code to do something like:
xcopy "C:\Users\Asus\Desktop\Test\Test.MDB" "C:\Users\Asus\Google Drive\Test (4-21-18).MDB" /Y /H /E /F /I
exit
I need to back up the files every 2 weeks in the task scheduler and I need the script to automatically add the date of the back-up. Also, I have looked at the list of commands (e.g. /Y /H /E) and I cannot find one that describes non-overwriting in the destination folder. I need the back-ups to pile up and not get deleted every time the code runs.
You can add %date%
If you want to create folders with the date and put the file in it,
use like this to join the date to a foldername (D:\myFolder15-04-2020):
xcopy /y /q /s "c:\myFolder\*" "D:\myFolder"%date%"\"
or a folder name with just the date: (D:\15-05-2020)
xcopy /y /q /s "c:\myFolder\*" "D:\"%date%"\"
If you want to put the files in the same folder and change the file name use:
xcopy /y /q /s "c:\myFolder\*" "D:\myFolder\"%date%".MDB*"
The trick is:
"\" at the end of the command means a folder name
"*" at the end of the command means a file name
You can do this. Maybe exist better solutions but it will be working and Additionally, this is an approach for more than one file.
XCOPY /Y /H /E /F /I C:\Users\Asus\Desktop\Test\*.MDB
rem get date, make if file name friendly
FOR /F "tokens=1-4 delims=/ " %%i in ('date/t') do set d=%%i-%%j-%%k-%%l
set MDB=*.%d%.MDB
ren *.MDB %mdb%
move C:\Users\Asus\Desktop\Test\*.MDB C:\Users\Asus\Google Drive\Test\
Hope this help.
You can create a bat file, get the current date in a variable and have this variable as part of the file name.
This bat file works:
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate
set MyDate=%%x
set today=%MyDate:~4,2%-%MyDate:~6,2%-%MyDate:~2,2%
mkdir "C:\Users\Asus\Google Drive\Test (%today%).MDB"
xcopy "C:\Users\Asus\Desktop\Test\Test.MDB" "C:\Users\Asus\Google Drive\Test (%today%).MDB" /Y /H /E /F /I
exit
This code first saves the current date in "MyDate" variable.
Then the desired date format is saved in "today" variable.
Finally the content of the "today" variable is used as part of the file name that is passed in "xcopy" as an argument.
Mkdir makes sure that the directory is first created before xcopy is used.
This prevents the xcopy question <F = file, D= directory>? that pops out.
If a path refers to a file or directory that does not exist, xcopy considers it reasonable to first ask you what it is. Alternatively you could add a '\' in the end of the directory path to indicate that it is a directory.
It works! "echo F|" to auto confirm that you copy a file in the cmd prompt.
call set currentDate=%date:/=-%
call set currentDate=%currentDate:~-10%
echo F|xcopy "C:\Users\Asus\Desktop\Test\Test.MDB" "C:\Users\Asus\Google Drive\Test (%currentDate%).MDB" /Y /H /E /F
exit

Xcopy should copy all files older then a specific date

I am working first time with xcopy and I have to copy all files that are older then a specific date.
The parameter /d copys all newer files then the specific date.
Is there any way to copy older files?
And it has to be with xcopy, robocopy is not an option.
Thanks
The xcopy command unfortunately does not have an option for selecting files older than a certain date, so, also unfortunatly, robocopy is the only option.
The robocopy command offers a /minage:MM-DD-YYYY parameter (that's what i'm calling it anyway) that selects files with a minimum age of MM-DD-YYYY.
Your syntax should look something like this:
ROBOCOPY Source_folder Destination_folder /minage:MM-DD-YYYY [options]
Here is more information if it is needed:
https://ss64.com/nt/robocopy.html
To add to ivyomni's answer of combining forfiles and xcopy, I did this:
set startDate=10-4-2021
for /d %%i in (*.*) do (
pushd %%i
Echo now in %%i
Rem: The following is sort of a cheat. The forfiles sets the maxdate and the xcopy sets the min date
echo on
forfiles /M *.zip /D -10/09/2021 /C "cmd /c xcopy #file C:\ZipFilesForLogAnalysis_Temp /D:%startDate% "
echo off
popd
)
I solved it with a forfiles infront of the the xcopy. Thanks for the replies.

Copying backup files from a network drive daily, need different dated copies for the end of each day

I am trying to backup files from a network drive to my C: drive. for the copy I am using:
xcopy "\MY_SERVER_IP\SharedDRIVE\TEST*" "C:\Test_Folder" /D /E /C /I /H /Y
Is there any way I can have the date added onto this and the information not replace but instead compound and have a daily record of changes made to the files but not lose older copies? This will work for copying the most current information, but I need many months of records not just the most current copy.
Any suggestions?
Thanks in advance
This is a way of getting a DateTime string independent of localization:
for /f "tokens=1 delims=." %%i in ('wmic os get localdatetime^|find "."') do set dt=%%i
This is format YYYYMMDDHHMMSScc
You can shorten this string to your needs. for example:
set dt=%dt:~0,8%
will set it to YYYYMMDD only.
set dt=%dt:~0,12%
will set it to YYYYMMDDHHMM
Then you can copy like this:
xcopy "\MY_SERVER_IP\SharedDRIVE\TEST*" "C:\Test_Folder_%dt%" /D /E /C /I /H /Y

Batch File XCopy Command

I have a batch file which loops through a content of a text file and copies a specific file using xcopy command.
here's the snippet.
for /f %%a in (FilesToCopy.txt) do (
xcopy ..\..\Common\%%a Common\%%a /i /d /c /v /s /y /f
xcopy Common\%%a ..\..\Common\%%a /i /d /C /v /s /y /f
)
%%a contains values like
Images\image1.jpg
Images\image2.jpg
so when xcopy is executed it would look like
xcopy ..\..\Common\Images\image1.jpg Common\Images\image1.jpg /i /d /c /v /s /y
upon execute it would then prompt this message
Does Common\Images\image1.png specify a file name
or directory name on the target
(F = file, D = directory)?
it seems that the /i command doesn' work or i am missing something here to suppress the message above.
Well, you left out the second statement the help gives about /I:
/I If destination does not exist and copying more than one file,
assumes that destination must be a directory.
You are only ever copying one file at a time, so /I doesn't apply.
You can probably hack-solving this by piping F into the command and suppressing output:
echo F|xcopy ..\..\Common\%%a Common\%%a /i /d /c /v /s /y /f >nul
(Won't work on non-English versions of Windows; but probably that's the least of your problems, given that the batch already fails for file names with spaces :-))
You could try building a single long list of file names to copy:
setlocal enabledelayedexpansion enableextensions
set LIST=
for /f %%a in (FilesToCopy.txt) do set LIST=!LIST! "..\..\Common\%%a"
xcopy %LIST% Common /i /d /c /v /s /y /f
This requires two passes over the initial file, though. And it fails when the list of file names gets longer than 8190 characters.
The destination should be a path, then it won't ask:
xcopy ..\..\Common\Images\image1.jpg Common\Images\ /i /d /c /v /s /y
In your case, you can use path extraction with %~p on the destination since you may want to preserve that:
xcopy ..\..\Common\%%a Common\%%~pa /i /d /c /v /s /y

Number of files deleted from batch file

REM Detect how many files are on the C: drive
dir /s /b C:\ |find /c "\" > NUMfiles.###
set /p count1=<NUMfiles.###
##### TEMP FILES DELETED HERE, RUN CCLEANER, RUN MBAM, ETC #####
REM Calculate Total Files Deleted
dir /s /b C:\ |find /c "\" > NUMfiles.###
set /p count2=<NUMfiles.###
set /a count3=%count1% - %count2%
echo Number of files removed: %count3%
This doesn't seem to be giving me an accurate reading. Can anyone help?
I do a manual check via command line using the 'dir /s /b C:\ |find /c "\"' before the script, and at the end. And the output from '%count3% isn't accurate from my subtraction from the manual checks. Hope you understand my question.
Yes, as snemarch montined, the fact that you list everything and temporary files could as well be added/deleted by another process meanwhile invalidate the entire effort.
On a side note, adding "/a-d" to the "dir" command would remove the directories from being listed, thus not needing VonC's "find /v "" addition to the process, if you insist on checking files only.
Could you not check file while they get deleted instead? Not sure what you use this for but you definately need to rethink the way from source, the deleting part.
My suggestion.
If you must iterate on the all content, this command line might be more precise to list the number of files (files, not directories):
dir /a /s /OG C:\ |find /v "<DIR>" | find /c "M "
Off course, this assume a dir does display 'AM ' or 'PM '.
If it does not, the following should works better:
dir /a /s /OG C:\ |find /v "<DIR>" | find /c "/"