I have a list of filenames without their extension and a folder and sub-folders of those files with their extensions. I am trying to use the list to copy those files to a different location. I tried to use a script I found here and modified it:
#echo off
FOR /R "P:\Case\MyCase\Productions" %%G in (.) do (
for /F "delims=" %%i in (UniqueFileList.txt) do (
if exist %%G\%%i.* xcopy %%G\%%i.* "C:\Temp\CopiedFiles" /D /Y
)
)
My file names are TIN00001.msg, TIN00002.txt, TIN00003.jpg, etc...
and the names in my file list is TIN00001, TIN00002, TIN00003, etc...
How can I use a script to copy the files ?
Any help is appreciated.
Thanks !!
Mustu
I'm not sure if I fully understand your intention but my best guess is that something like this would suit you.
#Echo Off
Set "rootDir=P:\Case\MyCase\Productions"
Set "destDir=C:\Temp\CopiedFiles"
Set "listTxt=%~dp0UniqueFileList.txt"
For /F "Delims=" %%A In ('Where/R "%rootDir%" *.*^|FindStr/LIG:"%listTxt%"'
) Do FindStr/LIX "%%~nA" "%listTxt%">Nul 2>&1 && XCopy "%%A" "%dstDir%" /D /Y
If %listTxt% isn't in the same location as the script then change it's location to a full path too.
Related
I have 100+ sub-directories all under the same folder that I'm looking to copy the newest file to backup location with the directory structure intact.
\data\sub1\newest.file -> \backup\sub1\newest.file
\data\sub1\older.file1.ignore
\data\sub1\older.file2.ignore
\data\sub2\newest.file -> \backup\sub2\newest.file
\data\sub2\older.file1.ignore
etc....
Here's what I have so far, and i can't seem to piece it together. Any help would be greatly appreciated.
#echo off
set source="c:\data"
set dest="n:\backup"
if not exist %dest% md %dest%
cd /d %source%
for /d %%x in ("%source%"/*.*) do (
if not exist "%dest%\%%x" md "%dest%\%%x"
FOR /F %%I IN ('DIR *.* /A-D /B /O-D') DO COPY %%I "%DEST%\%%X" & #ECHO %%I COPIED TO "%DEST%\%%X"
)
I would try to do this with robocopy if I were you, because this will most likely be a more robust solution.
Windows' built-in xcopy program provides a flag to include empty directories.
C:\>xcopy "%source%" "%dest%" /E
But, it sounds like you may want to only copy newer/missing files. If that is the case, then #Marged has it right. You should use robocopy.
C:\>robocopy "%source%" "%dest%" /E
Check out robocopy /? for all the details and additional commands.
The metavariable %%x in your for statement is CaSe-SeNsItIvE so you must use %%x throughout the loop, but you are using %%X in the copy statement.
Since you only want to copy the first file, you should append &goto alabelotsideoftheforloop which terminates the for..x.. after the first file has been copied.
I am looking to write a script to copy all files but those contained in a list. I am a new at this but found the opposite of what I want...
#echo off
set src_folder=c:\Source\
set dst_folder=c:\Destination
for /f "tokens=*" %%i in (list.txt) Do xcopy /S/E/U "%src_folder%\%%i" "%dst_folder%"
I am backing up a folder full of files but need to skip a few as they are unimportant to me.
I thought something like this would work. but I must have made an error...
#echo off
set src_folder=c:\Source\
set dst_folder=c:\Destination\
xcopy /S/E/U "%src_folder%\%%i" "%dst_folder%" /exclude for /f "tokens=*" %%i in (c:\list.txt)
Try
xcopy .... /exclude:list.txt
see
xcopy /?
from the prompt for documentation
Thanks to Mangoo I got it figured out so for anyone needing it...
#echo off
set src_folder=c:\source
set dst_folder=c:\destination
xcopy "%src_folder%" "%dst_folder%" /exclude:c:\List.txt
This also looks for only certain aspects in the list so if you want to exclude all copies, you can type just (2) and it will not include any files with that entry. (This caused me a problem at first.
I created a windows batch file to copy only files with specific extensions into a different folder. Here is the line of code I used:
for /R "%cd%" %%f in (*.htm) do copy "%%f" "%cd%\myfolder"
The issue is that this will copy any extension that starts with .htm, i.e. .html, which I do not want; only .htm. How is this copy prevented?
I've tried
"(*.htm)"
("*.htm")
(*".htm")
(*."htm")
(*.htm*)
Thanks
Solution:
for /R %%f in (*.htm) do if /I "%%~xf" == ".htm" copy "%%f" "myfolder"
Thanks #Aacini and #Monacraft
This should work:
for /R %%f in (*.htm) do if /I "%%~xf" == ".htm" copy "%%f" "myfolder"
A couple comments unrelated to your problem:
%cd% is a variable that is always replaced by the current folder. If you give any name without a previous path, the name is assumed to be in the current folder. This way name and %cd%\name is exactly the same and the second one is customarily never used.
In for /R [path] %%f ... command, if the path is not given, the current folder is assumed.
You could check using an if statement:
for /R "%cd%" %%f in (*.*) do if /i "%%~xf"==".htm" copy "%%f" "%cd%\myfolder"
And that is the logical way to do this in batch.
Mona
The reason *.htm matches .html files is because of short 8.3 file names. A file with .html extension will have a short name with .htm extension.
Monacraft and Aacini have provided working solutions using IF statemnts within the body of the loop.
Here is a solution that uses DIR /B piped to FINDSTR within a FOR /F IN() clause.
for /f "eol=: delims=" %%F in ('dir /b /s /a-d-h-s *.html ^| findstr /lie .htm') do copy "%%F" "myfolder"
There's probably a better way, but (*.ht?) should do it.
If you are working with a windows vista or later OS, you can use robocopy and exclude not needed files
robocopy "%cd%" "%cd%\myFolder" *.htm /xf *.html
I have a text file containing a list of files and folders. What I want to do is use xcopy to replicate what is written in the text file. My text file looks like this:
"C:\FOLDER"
"C:\FOLDER\FILE1.TXT"
"C:\FOLDER\FILE2.TXT"
"C:\FOLDER\FOLDER2"
"C:\FOLDER\FOLDER2\FILE3.TXT"
For a given output directory "C:\OUTPUT" I would like to replicate the entire structure, so:
"C:\OUTPUT\FOLDER"
"C:\OUTPUT\FOLDER\FILE1.TXT"
"C:\OUTPUT\FOLDER\FILE2.TXT"
"C:\OUTPUT\FOLDER\FOLDER2"
"C:\OUTPUT\FOLDER\FOLDER2\FILE3.TXT"
How can I accomplish this? So far I have written a for loop that reads in each line of the file, but it copies all files if the line is a folder. What I want to do is only copy and create the files and folders that are mentioned in the text file.
#echo off
for /f "delims=] tokens=1*" %%a in (textfile.txt) do (
XCOPY /S /E %%a "C:\OUTPUT"
)
Am I on the right track?
Thank you and best regards,
Andrew
Yes, you are close. Just need to use the existing path as the appended destination path.
Update
#echo off
for /f "delims=" %%A in (textfile.txt) do if exist "%%~fA\*" (
md "C:\Output\%%~pA"
copy /y "%%~fA" "C:\Output\%%~pnxA"
)
Original
If %%A = "C:\Folder\Folder2\File3.txt", then %%~pA = Folder\Folder2
#echo off
for /f "delims=" %%A in (textfile.txt) do (
md "C:\Output\%%~pA"
if not exist "%%~fA\*" echo f | xcopy "%%~fA" "C:\Output\%%~pnxA" /y
)
The if not exist "%%~fA\*" makes sure to only copy the entry if it is not a directory. See Reference for more Techniques and Comments
Type in for /? at the command line to view a list of the variable modifiers. %%~A will remove the surrounding quotations (if any) from the variable.
Post about xcopy prompting issue. and fix #2.
Alternate Setup, since you most likely will not need the xcopy abilities.
#echo off
for /f "delims=" %%A in (textfile.txt) do (
md "C:\Output\%%~pA"
if not exist "%%~fA\*" copy /y "%%~fA" "C:\Output\%%~pnxA"
)
I've seen some scripts examples over SO, but none of them seems to provide examples of how to read filenames from a .txt list.
This example is good, so as to copy all files from A to B folder
xcopy c:\olddir\*.java c:\newdir /D /E /Q /Y
But I need something like the next, where I can fill actually the source and destination folder:
#echo off
set src_folder = c:\whatever\*.*
set dst_folder = c:\foo
xcopy /S/E/U %src_folder% %dst_folder%
And instead of src_folder = c:\whatever\*.*, those *.* need to be list of files read from a txt file.
File-list.txt (example)
file1.pds
filex.pbd
blah1.xls
Could someone suggest me how to do it?
Given your list of file names in a file called File-list.txt, the following lines should do what you want:
#echo off
set src_folder=c:\whatever
set dst_folder=c:\target
for /f "tokens=*" %%i in (File-list.txt) DO (
xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)
I just tried to use Frank Bollack and sparrowt's answer, but without success because it included a /U switch for xcopy. It's my understanding that /U means that the files will only be copied if they already exist in the destination which wasn't the case for me and doesn't appear to be the case for the original questioner. It may have meant to have been a /V for verify, which would make more sense.
Removing the /U switch fixed the problem.
#echo off
set src_folder=c:\whatever
set dst_folder=c:\target
for /f "tokens=*" %%i in (File-list.txt) DO (
xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)
This will do it:
#echo off
set src_folder=c:\batch
set dst_folder=c:\batch\destination
set file_list=c:\batch\file_list.txt
if not exist "%dst_folder%" mkdir "%dst_folder%"
for /f "delims=" %%f in (%file_list%) do (
xcopy "%src_folder%\%%f" "%dst_folder%\"
)
The following will copy files from a list and preserve the directory structure. Useful when you need to compress files which have been changed in a range of Git/SVN commits¹, for example. It will also deal with spaces in the directory/file names, and works with both relative and absolute paths:
(based on this question: How to expand two local variables inside a for loop in a batch file)
#echo off
setlocal enabledelayedexpansion
set "source=input dir"
set "target=output dir"
for /f "tokens=* usebackq" %%A in ("file_list.txt") do (
set "FILE=%%A"
set "dest_file_full=%target%\!FILE:%source%=!"
set "dest_file_filename=%%~nxA"
call set "dest_file_dir=%%dest_file_full:!dest_file_filename!=%%"
if not exist "!dest_file_dir!" (
md "!dest_file_dir!"
)
set "source_file_full=%source%\!FILE:%source%=!"
copy "!source_file_full!" "!dest_file_dir!"
)
pause
Note that if your file list has absolute paths, you must set source as an absolute path as well.
[¹] if using Git, see: Export only modified and added files with folder structure in Git
This will also keep the files original file directory:
#echo off
set src_folder=c:\whatever
set dst_folder=c:\target
set file_list=C:\file_list.txt
for /f "tokens=*" %%i in (%file_list%) DO (
echo f | xcopy /E /C /R /Y "%src_folder%\%%i" "%dst_folder%\%%i"
)
Also can use robocopy and Not use for loop with xcopy - can parse list of files in argument.
robocopy Source_folder Destination_folder [files_to_copy] [options]
Files to copy it's string with Space delimiter.
For example:
robocopy . "d:\my folder" *.txt "my file one.cpp" file2.cpp
robocopy "d:\F 15" "d:\backup\F 15" /E