XCOPY with pattern matching - windows

I am very new to Windows batch scripting so please ignore if it is stupid question, I have a requirement of copying set of files from source to destination only if source file is modified after certain date_time, I managed to do it using XCOPY command.
XCOPY C:\Src\*.txt C:\Target /D /S /Y
This runs fine for first run, now the twist is once destination folder file is processed it will be renamed to some other name and extension, so when next time my script runs it does not find same file name as source in the destination folder because it is processed and renamed. So is there a way in XCOPY or any other Windows command to do the pattern match for the file name int the destination folder and if match found then proceed with date_time check and copy or else ignore?
Example Source Dir Files:
a.txt
b.txt
c.txt
Destination Dir Files After first script run:
a.txt
b.txt
c.txt
Destination Dir Files after processing and before running the script second time:
a_201603071130.ok
b_201603071130.ok
c_201603071130.ok
For second run of the script with XCOPY it does not find the file a.txt in destination as it is processed but the requirement is to copy only if a.txt file is modified after last run.
I can do this by storing the last run time and checking that for next run etc. but I wanted to find out if there is any other way of doing it.

XCOPY C:\Src\*.txt C:\Target /D /S /Y /M
The /M switch changes the A flag for the file.

Related

Need batch program that moves files without copying then deleting them

Every month I have a very large number of files (in many subfolders) in a particular folder. I need to move them all into a different folder. In an attempt to automate the process of moving them I used robocopy in a batch file. It works fine, but takes HOURS to run. (It is many many GB).
Now, if I do it manually in Windows Explorer, by opening said folder, selecting all, and right-dragging to destination folder, and choosing "Move Here", it moves INSTANTLY. (Windows XP must be pruning and grafting the directory entries, without ever making a second copy of the files. ... and yes, source and destination are on same partition.)
So, QUESTION IS: Does anyone know of a program I can run from a batch file to move files in this instantaneous way? (need to move entire sub-folder tree)
You can use MOVE for this:
C:\>MOVE /?
Moves files and renames files and directories.
To move one or more files:
MOVE [/Y | /-Y] [drive:][path]filename1[,...] destination
To rename a directory:
MOVE [/Y | /-Y] [drive:][path]dirname1 dirname2
[drive:][path]filename1 Specifies the location and name of the file
or files you want to move.
destination Specifies the new location of the file. Destination
can consist of a drive letter and colon, a
directory name, or a combination. If you are moving
only one file, you can also include a filename if
you want to rename the file when you move it.
[drive:][path]dirname1 Specifies the directory you want to rename.
dirname2 Specifies the new name of the directory.
/Y Suppresses prompting to confirm you want to
overwrite an existing destination file.
/-Y Causes prompting to confirm you want to overwrite
an existing destination file.
The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line. Default is
to prompt on overwrites unless MOVE command is being executed from
within a batch script.
For example:
C:\Users\test>mkdir to
C:\Users\test>move from\*.txt to
C:\Users\test\from\1.txt
C:\Users\test\from\2.txt
2 file(s) moved.
With a point in the right direction from #psmears, and a lot of googling, I found the (or a) solution:
#echo off
setlocal ENABLEDELAYEDEXPANSION
REM ** Change to source dir
L:
cd "L:\Backups\Source"
REM ** Move files recursively
for /R %%G in (.) do (
set mydir=%%G
set mynewdir=!mydir:~18!
md "L:\DEST\!mynewdir!"
cd "L:\DEST\!mynewdir!"
move "%%G\*.*" .
)
REM ** Remove now-empty sub-dir structure inside source
L:
cd "L:\Backups\Source"
for /f "usebackq delims=" %%d in (`"dir /ad/b/s | sort /R"`) do rd "%%d"

how to copy directory of specific files in batch file

c:/--> folder1-->
folder2->
img001.png
img002.jpg
img003.png
I having kind of folder structure.
I need to copy a single file from this folder to Destination folder.
source : "c:\folder1\folder2\imgoo1.png"
Destination:"D:\folder1\folder2\imgoo1.png"
need output:
D:/--> folder1-->
folder2->
img001.png
Note:I need batch file format
robocopy "c:\folder1\folder2" "d:\folder1\folder2" "img0001.jpg"
Since robocopy is not included in windows XP, this can be done with plain xcopy
xcopy "c:\folder1\folder2\img0001.jpg" "d:\folder1\folder2\"
for %%f in (img001.png img002.jpg img003.png) do copy /b "c:\folder1\folder2\%%f" "d:\folder1\folder2\"
Note that thw directory separator in windows is \, not /. / is used as a command-switch - /b in the above case means "copy in binary mode'.
Note that you do not say whether the batch should check whether the destination directory exists or whether the destination filename already exists.
md "d:\folder1\folder2" 2>nul
will force the destination filename to exist (2>nul suppresses the 'already exists ' message)
You can add an extra switch /y to the copy command to force overwrite in the case that the destination file already exists.
You can add >nul to the copy command to suppress the 1 file copied message.
This will copy that one file. The target filename is not required but can be left in.
copy "c:\folder1\folder2\imgoo1.png" "D:\folder1\folder2\imgoo1.png"
This assumes that the folders already exist.

Windows cmd shell xcopy to network directory doesn't work

Im trying to make a batch file that will copy all new files and folders from a source folder to an network directory. All the new subdirectories and new files should be copied (backup).
My code:
xcopy "C:\Source" "T:\Backup" /d/i/s/q
(/d for only new files, /i because source is a dir, /s for all the subdirs and files, /q just to supress the copy text)
Source contains both subdirectories and files (.txt).
The first run it copies Everything as it should. When I add a new .txt file to one of the existing subdirectories and run it again I get the message:
"An error occured when the file The directory is not empty. was being created.
The folder "T:\Backup" could not be created.
0 files copied.
(Translated from Swedish so not 100% original)
The thing is when I try this command to a local source like e.g. "C:\test" and do the same procedure it works.
Anyone who can understand why this doesn't work for the network drive?
Should I try Another command such as robocopy?
Skip xcopy and use robocopy with the /E flag instead. It's built into all recent versions of Windows. Free download for XP.
Example:
robocopy c:\source T:\backup /E
That will copy all the files in the "source" folder to the "backup" folder that haven't been copied already.
And if you don't want to have the output shown on the console (equivalent to the /Q option in xcopy):
robocopy c:\source T:\backup /E /LOG:nul
Robocopy must be better because it should create directories with the \E switch. No overwrites for files, just adds a file with extra letters or extension <> command. Still must defrag.
XCOPY "DRIVE LETTER:\windows.old\USERS" "\computername\D\NAME\" /D /E /C /R /I /K /Y /f

How can I do a batch file to copy and rename a file?

I am trying to create a batch file to copy a "folder" as a backup to a new destination. However, I need to add a date stamp of the file name.
for example folder is myFolder when I copy it I want to to be named myFolder_todays_date
I want to be able to copy the folder and it's content.
I found the xCopy but I am not sure how to write it correctly.
now I tried xCopy /e/i dir newDir
How can I append the date to the folder name?
I appreciated your help on how to write this batch file correctly.
Thanks
#echo off
mkdir %1%DATE%
xcopy /s %1 %1%DATE%
This accepts the name of the folder as a command line argument.
For example if the above batch file is called bkup.bat and you want to make a backup of a directory called work, you run it as
bkup.bat work
It copies it into the current directory. You can accept a 2nd command line argument (%2) & also use that if you want to copy to a different directory
xcopy /s %1 %2\%1%DATE%

Batch script to copy file with a changing name

Our build produces an archive with the name app-component-x.x.x.x-SNAPSHOT.zip where x.x.x.x is a version number (ie: 1.6.2.8). Directory is c:\buildresults\app
We want to write a batch script that a) copies the file to another directory with a fixed name such as build-results.zip and then b) extracts the file.
I am not sure how to do part A. This doesn't seem to work: copy c:\buildresults\app\*.zip c:\xxx\build-results.zip
Any ideas?
Update:
File is being copied BUT the size is dramatically less. Ie: the file seems to be getting corrupted.
This seems to work but not ideal:
cd buildresults\app
for %%f in (*component*) do (
echo %%~nf
7za.exe -oC:\buildresults\app x "%%~nf.zip"
)
You could try:
xcopy /Y /Q /C /H /R c:\buildresults\app\*.zip c:\xxx\build-results.zip
Cannot reproduce:
D:\>mkdir xxx
D:\>echo test > test-1.2.3.zip
D:\>copy test-*.zip xxx\test-current.zip
test-1.2.3.zip
1 Datei(en) kopiert.
D:\>type xxx\test-current.zip
test
D:\>
Are you sure the target doesn't exist? Btw, are you sure your * matches exactly one file in every case? Because copying multiple files to one destination is a valid operation and will end up with an invalid zip file.

Resources