Batch File XCopy Command - command-line

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

Related

Update file in each subdirectory with a specific name

I have a batch-file which pulls a file from a url using powershell and then outputs/updates the file in a specific directory. But I have many of these directories, the only thing that changes about the path is numbers between \command\ and \setup\. How would I get it to put the file in every folder automatically?
Essentially I would like to output the downloaded text file in each of the install subdirectories of that path.
Also how could I make it happen silently?
#echo off
echo !!! PRESS ANY KEY TO CONTINUE AND UPDATE!!!
pause
powershell -Command "Invoke-WebRequest http://example.com/log/read.txt" -OutFile C:\Users\Administrator\AppData\Roaming\base\command\234235234\setup\install\read.txt 2>NUL >NUL
echo !!! DONE NOW !!!
echo !!! YOU CAN RE-OPEN NOW !!!
for /d /r "dirname" %%a in (*) do if /i "%%~nxa"=="install" echo %%a
may be useful to you.
Your requirement is unclear. Do you want to copy the file to the install subdirectories of ...\234235234\.. only, or of ...\*\... ?
Replace dirname with the name of the starting directory, be it ...\234235234\.. or C:\Users\Administrator\AppData\Roaming\base\command and the command I have shown will report all of the install directories contained under dirname. All you need then do is to change the echo to an appropriate copy command - see copy /? from the prompt. You can suppress copy's responses by appending >nul 2>nul (suppress messages and suppress error messages)
for /d /r with * as the list element will process a list of all subdirectories starting at the nominated directory. The if command selects only the leaf directories that match install in either case (/i)
Since
for /d /r ...
does nor detect hidden directories, another approach is
for /f "delims=" %%a in ('dir /s /b /ad "dirname" ') do if /i "%%~nxa"=="sub1" echo %%a
Which in this case should be
for /f "delims=" %%a in ('dir /s /b /ad "C:\Users\Administrator\AppData\Roaming\base\command" ') do if /i "%%~nxa"=="sub1" echo %%a
The dir command produces a list in /b basic (name-only) form, /s including subdirectories, /ad of directories only (names with the directory attribute set). This list is processed line-by-line by for /f without delimiters so the entire line (including spaces, if any) is assigned to %%a and displayed.

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

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

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

Batch File to display file name and then certain lines in the files

I have a folder of .log files where the content of each file has multiple lines of the following format:
yyyy/mm/dd, hh:mm:ss, ComputerName, IPAddress, stuff, stuff
I would like to create a batch file to parse through the .log files and create the following output for any line in a file where ComputerName starts with "XPLT":
filename,yyyy/mm/dd,ComputerName,IPAddress
And preferably, I'd like to only look at files with a modified date within the last 30 days.
So far, I've only gotten the following code which doesn't even work and doesn't even include the file modified date and parsing by ComputerName. Looking for help because I've just not done this very much, and I can't find a good example online.
Echo EID,Date,PCName,IPAdd>CitrixLogs.csv
setlocal enabledelayedexpansion
for /f "tokens=1,3,4" %%i in ('dir /b "C:\LogFiles\*.log"') do (
echo %%i,%%j,%%k,%%l>>CitrixLogs.csv
)
I'd use a bit of FINDSTR magic:
set LOG_DIR=c:\logfiles
for /f "tokens=1-7 delims=:," %%L in ('%SystemRoot%\System32\findstr.exe /r /c:"^[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9],[0-9][0-9]:[0-9][0-9]:[0-9][0-9],xplt.*," %LOG_DIR%\*.log') do #echo %%L,%%M,%%N:%%O:%%P,%%Q,%%R,
`
REM
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "tokens=2-8delims=:, " %%a IN ('findstr /l /i /c:", XPLT" "%sourcedir%\*.log"') DO (
ECHO %%~nxa,%%b,%%c:%%d:%%e,%%f,%%g
)
GOTO :eof
redirect to your .csv as you will...
#echo off
setlocal enableextensions disabledelayedexpansion
:: configuration
set "logFolder=%cd%"
set "logFiles=*.log"
set "maxFileAge=30"
set "computerName=XPLT"
set "outputFile=CitrixLogs.csv"
:: adjust commands to execute according to configuration
set "ageFilter=robocopy "%logFolder%" "%logFolder%" "%logFiles%" /l /is /njh /njs /nc /ns /ndl /maxage:%maxFileAge%"
set "contentFilter=findstr /f:/ /i /r /c:"^^[^^,]*, [^^,]*, %computerName%" "
:: Generate output file
( echo(EID,Date,PCName,IPAdd
for /f "usebackq tokens=2,* delims=:" %%a in (
`cmd /q /c "for /f tokens^=* %%a in ('%ageFilter%') do echo(%%a" ^| %contentFilter% `
) do for /f "tokens=1,3,4 delims=," %%c in ("%%b") do echo(%%~nxa,%%c,%%d,%%e
) > "%outputFile%"
endlocal
This will use robocopy (or you can change it with forfiles) to search for files with a max age of 30 days in the indicated folder. Files will not be copied (/l) but the list will be echoed (the rest of the switch configure the output). This list of files is piped into findstr (/f:/) indicating where to search for the lines that match the indicated condition. This will generate an output with each line in the input file matching the condition, prefixed with the name (full name) of the file. This line is then splitted to output only the required fields.

Appending a txt file from multiple CSVs in subdirectories

I am trying to write a batch file which will append all *.csv files in the immediate subdirectories to a single text file in the current directory.
From various sources I have managed to piece together this code which works fine for files in the current dir but not sub-dirs
for %%a in (*.csv) do (type %%a >> csvreport.txt)
If anybody could help me with this I would be extremely grateful as I have tried various approaches with wildcards but without success.
Yet another option...
for /f usebackq %%a in (`dir /s /b *.csv`) do (type %%a >> csvreport.txt)
EDIT: Reading your details a bit more ... you want just the immediate directories, you can do this:
for /f usebackq %%a in (`dir /b /ad`) do for %%b in ("%%a"\*.csv) do (type "%%b" >> csvreport.txt)
for /R .\ %%a in (*.csv) do (type %%a >> csvreport.txt)
The /R indicates recursive and the parameter afterward is the folder in which to start (.\ is the current directory).
You can find up more if you run for /?
dir /ad /b > dirs.txt
for /f "tokens=1*" %%i in (dirs.txt) do cd %%i & for %%b in (*.csv) do (type %%b >> c:\csvreport.txt) & cd ..
Using the /R flag will traverse all subdirectory trees. You can nest the 'for' statements to only work with the immediate subdirectories but not their subdirectories.