how to copy directory of specific files in batch file - windows

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.

Related

Batch program to copy file when there is a change in source folder

I need a batch program that will copy files from source folder to destination folder whenever there is a change(new files added or files modified) in source folder and restart a specific service.
Refer to the help of xcopy /? using the switch /D
/D : Copy files changed on or after the specified date.
If no date is given, copy only files whose
source date/time is newer than the destination time.
Syntax :
XCOPY source [destination] [options]
Remark :
So, in this batch file below, if you have any spaces in your directory names, you need to enclose them in double quotes.
#echo off
Set SourceDir="C\My Source\stuff"
Set TargetDir="D:\My Backup\stuff"
xcopy %SourceDir% %TargetDir% /i /d /y /e
/D : copy only files whose source date/time is newer than the destination time.
/E : To copy everything, including new directories, you should add the /e switch
/Y : Suppress prompt to confirm overwriting a file.
/I : If in doubt always assume the destination is a folder e.g. when the destination does not exist.

Batch File Copy and Rename

Currently looking to copy and rename a file from a C:Drive source to a G:Drive destination by using a batch file. Currently have the following code:
robocopy C:\Users\abronk04\Desktop\Test G:\Users\abronk04\Desktop\Backup /e /purge
ren G:\Users\abronk04\Desktop\Backup accessbackup.jpg backup.jpg
The file will successfully copy, but there is no rename. Do I need to specify a file I am looking to rename? My batch file will copy an entire folder to another destination, unsure if that is the problem here...
I think you ren syntax is not correct.
Maybe try the following after copying the directory
pushd G:\Users\abronk04\Desktop\Backup
ren accessbackup.jpg backup.jpg
popd
This way you will temporarily change your working directory to the sink-path and then change it back

Batch: Copy File from different Subfolders with certain file name to other destination

I am total beginner and need help with copying of files in windows. I have xlsx files in in different subfolders. The name of the files that I want to copy contain (but are not limited) to "Reporting Assessment Tool". I would like to copy all files containing that name and bring them to flat source destination (don't need the folder structure).
source: c:\users\name\desktop\upload
destination: c:\users\source\desktop\tool
So far I have tried the following
cd /D "c:\users\jubalkheimer\desktop\upload" #for /r %%a in ("*Reporting Assessment Tool.xlsx") do (
copy "%%a" "c:\users\jubalkheimer\desktop\tool\"%%~a")
I am not receiving a error message, but files don't get copied....
Can you help?
Many thanks
The batch file code for this task could be:
#echo off
for /R "%USERPROFILE%\desktop\upload" %%I in ("*Reporting Assessment Tool.xlsx") do (
copy /-Y "%%I" "%USERPROFILE%\desktop\tool\%%~nxI"
)
This could be done also with a single command line in batch file:
#for /R "%USERPROFILE%\desktop\upload" %%I in ("*Reporting Assessment Tool.xlsx") do #copy /-Y "%%I" "%USERPROFILE%\desktop\tool\%%~nxI"
The case-sensitive loop variable I holds on each loop run the name of a file with full path without surrounding double quotes found in the specified directory or any subdirectory of that directory matching the file name pattern *Reporting Assessment Tool.xlsx.
For copying the found file with prompting the user of the batch file for overwriting an already existing file with same name in target directory it is necessary to specify as first parameter the string assigned to the loop variable and as second parameter the target path and just file name and file extension of the found file without path. This explains the usage of "%%I" (file name with extension and full path enclosed in double quotes) and "Target Path\%%~nxI" (just file name and file extension with fixed destination path enclosed in double quotes) on COPY command line.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
copy /?
echo /?
for /?
%USERPROFILE% references the predefined environment variable USERPROFILE which holds the path to profile directory of the current user account.
It would be very easily possible to do this task also without the usage of a batch file using Windows Explorer.
Navigate to folder %USERPROFILE%\desktop\upload
Run in that folder a search for *Reporting Assessment Tool.xlsx
Windows Explorer automatically searches recursive in that folder and all subfolders for files matching this file name pattern.
Select all found files with Ctrl+A (select All).
Prepare for copying all selected files with Ctrl+C (Copy).
Navigate to folder %USERPROFILE%\desktop\tool
Paste the files prepared for copying with Ctrl+V (Paste) which results in copying the files now.

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 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%

Resources