I need to copy a list of files in a text file to a new directory, while preserving the directory structure. My file looks like this:
F326819.B88
F326819.B89
F326819.B90
F326731.B44
F326733.B61
F326733.B62
I need a batch command that will "pick" the ones listed in the text file and copy them over to a new directory, preserving the directory structure. I tried this code but it says invalid number of parameters:
for /f "delims=" %%i in (W:\GasImages\ServiceCards\WindLake.txt) do echo D|xcopy %%i "W:\GasImages\ServiceCards" "D:\Marc\WindLake" /i /z /y /e
Any help would be appreciated.
xcopy takes in a list of source files, followed by a destination directory. When there are multiple directories passed in, it doesn't know what to do with them all.
Try this (note that this code assumes the files to copy are in C:\GasImages\ServiceCards)
#echo off
for /f "delims=" %%I in (C:\GasImages\ServiceCards\WindLake.txt) do (
xcopy "C:\GasImages\ServiceCards\%%I" "D:\Marc\Windlake\" /I /Z /Y /E
)
pause
Also, the echo D| is unnecessary with the /I flag.
Related
I have a folder named Demos1 with files inside that all have a .replay extension.
I have a batch script in parent folder of Demos1 as well as a file that has the names of the files i ONLY want to keep in a text file named namediff.txt.
I wants to recursiveley scan each file name in namediff.txt against the files found inside of the directory and if the file names (one per line) in namediff.txt does not match a file found in the directory, delete that file in Demos1 and leave only the file names that did match in the txt file.
This is what I have so far.
PUSHD "%~dp0Demos1"
:--------------------------------------------------------------------------------------------
FOR /F "TOKENS=*" %%A IN ('DIR /B "*.replay"') DO (
FOR /F "TOKENS=*" %%G IN ('%%~nxA') DO (
IF NOT "%%~nxA"=="%%~nxG" ECHO DEL "%%~nxG"
)
)
FOR /F "TOKENS=*" %%A IN ('DIR /B /a-d "*.replay" ^|findstr /x /i /v /g:namediff.txt') DO echo del "%%A"
Should do that. Findstr finds lines that /x exactly match /i case-insensitive /g:filecontainingstringstomatch and /v produces non-matches to the list of files generated by the dir.
The ^ is required to escape the pipe, which tells cmd that the pipe is part of the command to be executed, not the for.
The /a-d excludes directory-names matching - just a belt-and-braces approach.
The del is simply echoed for testing purposes. Remove the echo keyword when verified to delete the files.
I'm trying to write a script that exclusively moves files from a source folder if they're in a subfolder (IE dayfolders for images) and NOT if the file is just sitting in the root directory. The subdirectories also need to be moved.
So far I just have a basic xcopy script but that's copying the files that don't have a folder as well.
Code below:
Set ImageDirectory=C:\Images
Set SMBRootAddress=<Server>
Set SMBImageDestinationFolder=Images
xcopy "%ImageDirectory%" "\\%SMBRootAddress%\%SMBImageDestinationFolder%" /d /i /s /y /r
#ECHO OFF
SETLOCAL
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files\t w o"
SET "destdir=u:\your results"
FOR /f "delims=" %%a IN (
'dir /b /ad "%sourcedir%"'
) DO XCOPY /s /d /y "%sourcedir%\%%a" "%destdir%\%%a\" >nul
GOTO :eof
That is, simply perform a directory-scan for all required subdirectory names directly under the "local root" directory and then xcopy the names found. I haven't preserved your xcopy options in my testing.
I try to write a script that can monitor a folder H:\Start and copy a new subfolder with its files in H:\Start to new location H:\Target. The script should store the copied folder and files in a text file.
Each time the script starts new and monitors H:\Start, it should check the text file and copy only those subfolders which are not yet included in the text file because copied already before.
I was searching the world wide web for examples, but could not really find a starting point. Any help would be nice.
I have so far not working good :)
#echo off
setlocal EnableDelayedExpansion
pushd %1
for /D %%d in (“H:\Start\*.*”) do set n=!n!;%%d
if defined n echo %n:~1% > C:\Desktop\list.txt
popd
endlocal
for /f %%i in (C:\Desktop\list.txt) do not (
xcopy /d /s H:\Start H:\Target > C:\Desktop\list.txt >nul 2>&1
)
I suggest using:
%SystemRoot%\System32\xcopy.exe H:\Start H:\Target /C /H /I /K /M /Q /R /S /Y >nul
For information about all those parameters of xcopy open a command prompt window and run there xcopy /? to get help of this command displayed which explains all those parameters.
The important one is /M which selects for copying process just files with archive attribute set and which removes the archive attribute on each file in H:\Start after copying the file. This avoids that a once copied file is copied once more as long as not modified in H:\Start since last copy.
If you want to log all copied files into a text file, I suggest using:
%SystemRoot%\System32\xcopy.exe H:\Start H:\Target /C /F /H /I /K /M /R /S /Y >>C:\Desktop\list.txt
The names of the copied files are with this command line appended to text file C:\Desktop\list.txt
The following commented batch code works with directory lists as asked for.
#echo off
rem Define source and destination directory as well as
rem the names of the used list files each with full path.
setlocal EnableExtensions
set "Source=H:\Start"
set "Destination=H:\Target"
set "MainDirList=C:\Desktop\list.txt"
set "CurrentList=%Temp%\CurrentList.tmp"
set "ExcludeList=%Temp%\ExcludeList.tmp"
set "FinalDoList=%Temp%\FinalDoList.tmp"
rem Write the names of all subdirectories found in source
rem directory into a list file in directory for temporary files.
dir /AD /B "%Source%">"%CurrentList%"
rem Check if list file is not empty because of no subdirectories.
call :CheckEmpty "%CurrentList%"
if %FileIsEmpty% == 1 (
echo No directories in %Source%
goto EndBatch
)
rem Start copying the directories if there is no main list file
rem from a previous execution of this batch file or the main list
rem file was deleted intentionally to force copying all again.
if not exist "%MainDirList%" goto CopyDirectories
rem Start copying also if main list file is an empty file.
call :CheckEmpty "%MainDirList%"
if %FileIsEmpty% == 1 del "%MainDirList%" & goto CopyDirectories
rem Search in main list for lines matching completely lines in current
rem list with ignoring case and write the found lines into an exclude
rem list file as those directories were copied already before.
%SystemRoot%\System32\findstr.exe /I /L /X /G:"%CurrentList%" "%MainDirList%" >"%ExcludeList%"
rem Copy all directories if no line in current list is found in main list.
if errorlevel 1 goto CopyDirectories
rem Get all lines from current list not listed also in exclude list.
%SystemRoot%\System32\findstr.exe /B /I /L /V /G:"%ExcludeList%" "%CurrentList%" >"%FinalDoList%"
rem Replace the current list with the reduced final list.
move /Y "%FinalDoList%" "%CurrentList%"
rem Check if remaining current list is not empty because
rem all subdirectories copied already before.
call :CheckEmpty "%CurrentList%"
if %FileIsEmpty% == 1 (
echo Copied already before all directories in %Source%
goto EndBatch
)
:CopyDirectories
rem Copy each directory in remaining current list file.
for /F "usebackq delims=" %%D in ("%CurrentList%") do (
echo Coping %Source%\%%D
%SystemRoot%\System32\xcopy.exe "%Source%\%%D" "%Destination%\%%D" /C /H /I /K /Q /R /S /Y >nul
)
rem Append the list of copied directories to main list file.
type "%CurrentList%">>"%MainDirList%"
goto EndBatch
:CheckEmpty
rem This little subroutine just checks if size of a list file is 0.
if %~z1 == 0 ( set "FileIsEmpty=1" ) else ( set "FileIsEmpty=0" )
goto:EOF
:EndBatch
rem Delete all not further needed listed files and environment variables.
del "%ExcludeList%" 2>nul
del "%CurrentList%"
endlocal
This batch file should work for the FTP folder mounted as drive on Windows. It does not depend on attributes or timestamps. It uses explicitly only the names of the directories in H:\Start. It also does not check which directories exist already in H:\Target. Therefore it is possible to delete a directory in H:\Target if not interested in and the deleted directory will be nevertheless not copied once again from H:\Start as long as the deleted directory is not also removed from main list file.
For details on parameters used on findstr run in a command prompt window findstr /? and read the entire help output into the window.
Thanks for this question as this was really an interesting batch coding task.
You don't need to store anything, you can use
xcopy /d /s h:\start h:\target
/D:mm-dd-yyyy
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.
but if you need a list of the files you can just use a redirection :
xcopy /d /s h:\start h:\target > logfile.txt
I need to copy a folder matching a wildcard, for instance FOLDER_*. That folder will be in the presence of other files, so I need the command to segregate it from everything else. Also, the command needs to recursively search through the directory, and return only the FOLDER that matches the wildcard, with its contents intact. Then it needs to copy it to another folder. Any ideas? I've tried quite a few variants - here is the last thing I tried.
for /D /R %%f in (FOLDER_*) do xcopy %%f %~dp0\TestResults
Next code snippet should copy all found FOLDER_* folders including their subfolders but omits all their parent folder(s) like upfolder\ in upfolder\FOLDER_* (however could be improved to include it, of course).
for /F loop against dir /b /s /ad (a static list of subfolders) is used instead of for /D /R as the option /d /r is undocumented.
Pay your attention to recursion like FOLDER_main\FOLDER_sub1\FOLDER_sub11 etc.
Operational mkdir and xcopy commands are merely echoed for debugging purposes only:
#echo OFF
for /F "delims=" %%f in ('dir /B /S /AD FOLDER_*') do (
echo mkdir "%~dp0\TestResults\%%~nxf" 2>NUL
echo xcopy /S /E /C "%%~ff" "%~dp0\TestResults\%%~nxf\"
)
Consider adding more switches to xcopy, for instance
/H Copies hidden and system files also.
/R Overwrites read-only files.
/Y Suppresses prompting to confirm you want to overwrite an existing destination file.
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"
)