Copy list of files to specific folder locations - file

I have ~10,000 files in one folder. I want to copy specific files to specific folders based on a text file. Is it possible to use a semicolon delimited text file where the first part of each line is the source path & file name and the second part is the destination path?
C:\Files\File1.txt;C:\Folder1
C:\Files\File2.txt;C:\Folder2
C:\Files\File3.txt;C:\FolderN\
What would the code look like? Is there a better way to achieve the same result?
I have an existing bat file I use to copy all of the files listed in a text file to one specific folder location (below) but in this case I need to send different files to different folders and I would rather not run my bat file 50 times, changing the destination path in the bat file each time...
for /f %%f in (%1) do (
copy %%f G:\Files\PutFilesHere
)

It looks like this:
#echo off
setlocal enableextensions enabledelayedexpansion
for /f "usebackq delims=; tokens=1,2" %%I in (`type filelist.txt`) do (
copy /y %%I %%J
)
goto :eof
The for loop is told to use the result of a command (usebackq, for command type filelist.txt), to split at each ;, and to take elements #1 and #2. The fist is named as the variable (%%I, take care, case sensitive), and the second is the next letter, so %%J.
Then, the copy is trivial.

for /f "tokens=1,2delims=;" %%f in (%1) do ( copy "%%f" "%%g" )
see for /? from the prompt for documentation

Related

Copy list of files from directory+subfolders to another folder

First of all, I am a total beginner. I was trying an ultimate script bat file solution for a game. To not annoy you with details, let me tell you what I tried to do.
Example:
I have 17 files. 10 of them are .jpg and 7 of them are .obj files.
The images are in path \mods\images and the objects are in path \mods\models.
I want to list all 17 missing files in a list.txt
The bat file will read that list, search for the files and paste them into the TTSmissing folder
and here are my problems:
The bat script only looks exactly into the source path, but not into subfolders (that's why I wrote \mods\images\, to test if it works) so
what I basically want is: \Tabletop Simulator\Mods\ as source path and
the script shall look into all subfolders, too.
The list.txt only works, when the filenames also have their extensions. is it possible to change the script so i don't need the extension? so it will only look for names and copy the files? (example: the names in the list have to be like: hello123.jpg. It's not working when its only hello123.)
How do I need to change the bat script if i don't want a list.txt but just put the list of files right into the bat file?
#echo off
mkdir %USERPROFILE%\Desktop\TTSmissing
set src_folder=%USERPROFILE%\Documents\My Games\Tabletop Simulator\Mods\Images
set dst_folder=%USERPROFILE%\Desktop\TTSmissing
set file_list=%USERPROFILE%\Desktop\list.txt
for /f "tokens=*" %%i in (%file_list%) DO (
xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)
pause
#echo off
setlocal
set "src_folder=%USERPROFILE%\Documents\My Games\Tabletop Simulator\Mods"
set "dst_folder=%USERPROFILE%\Desktop\TTSmissing"
set "file_list=%USERPROFILE%\Desktop\list.txt"
set "ext_list=.gif .jpeg .jpg .mp4 .obj .pdf .png .webm"
if not exist "%dst_folder%" md "%dst_folder%"
for /d /r "%src_folder%\" %%A in (*) do (
pushd "%%~A" && (
for /f "usebackq delims=" %%B in ("%file_list%") do (
for %%C in (%ext_list%) do (
if exist "%%~B%%~C" (
echo copy /y "%%~B%%~C" "%dst_folder%\"
)
)
)
popd
)
)
You just want to copy files so copy is easier to use than xcopy. The code will echo the copy command to test whether it is working how you want it. If satisfied, remove the echo in front of copy and run the code again to do the actual copy process.
A for /d /r loop will recursively iterate the subdirectories in %src_folder%. pushd will change the current directory to each subdirectory so as can work relative to the source files.
The for /f loop will iterate each line from %file_list%. The simple for loop will iterate each of %ext_list%. If current "name.extension" exists, it will be copied to %dst_folder%.
If you set variables names in a script, it is usually a good idea to use setlocal to keep the variables defined local to the script.
To view help for a command, use command /?. This will work for many of commands used in the code.
View command /? help for copy, for, if, setlocal ...

Script batch copy from subfolder to main folder filtering extension

I have this schema
SOURCE
FOLDER_A
---FOLDERA1
------file1.abc
------file2.abc
------file2.txt
---FOLDERB1
------file3.abc
------file4.abc
------file.txt
I want to create a batch script which copies in a new folder only
DESTINATION
FOLDER_A1
---file1.abc
---file2.abc
FOLDERB1
---file3.abc
---file4.abc
putting in the destination only the second level (FOLDER_A should be deleted) and filtering only files with .abc extension
I wrote this code
#echo off
set SOURCE_DIR=C:\Users\%username%\Desktop\SCRIPT\source2
set DEST_DIR=C:\Users\%username%\Desktop\SCRIPT\dest
pause
setlocal enabledelayedexpansion
for /f "delims=" %%a In ('dir /ad/b %SOURCE_DIR% ') do (
set current_folder=%SOURCE_DIR%\%%a\
mkdir "dest\%%a"
for /r %SOURCE_DIR% %%f in (*.abc) do (
#copy "%%f" "dest\%%a"
)
pause
)
#pause
The problem is that in the destination I have the folder with the right name but inside of them everytime the 4 files file1.abc, file2.abc, file3.abc and file4.abc.
The goal is to have inside the first folder only file1.abc and file2.abc, and in the second folder file3.abc and file4.abc.
Where is the mistake?
Why are you using batchfiles and for-loops for this? Both xcopy and robocopy commands have exclusion features. Just type xcopy /? and robocopy /? for more information, and on the internet, you might find plenty of examples on how to do this.
Edit after first comment
It's indeed not that simple to work with the /Exclude switch, as you can see in following example:
C:\Temp_Folder\Folder_A>echo .txt>patterns.txt
// in this file, I mention that filenames, containing .txt, should not be copied
C:\Temp_Folder\Folder_A>xcopy /F /S C:\Temp_Folder\Folder_A\*.* C:\Temp_Folder\Destination\ /Exclude:C:\Temp_Folder\Folder_A\patterns.txt
// here I refer to the file, containing the patterns, not to copy
C:\Temp_Folder\Folder_A\FolderA1\file1.abc -> C:\Temp_Folder\Destination\FolderA1\file1.abc
C:\Temp_Folder\Folder_A\FolderA1\file2.abc -> C:\Temp_Folder\Destination\FolderA1\file2.abc
C:\Temp_Folder\Folder_A\FolderB1\file3.abc -> C:\Temp_Folder\Destination\FolderB1\file3.abc
C:\Temp_Folder\Folder_A\FolderB1\file4.abc -> C:\Temp_Folder\Destination\FolderB1\file4.abc
4 File(s) copied

Recursively append folder name to the files in Windows batch file

I would like to append my folder name to all the available .txt files inside a subfolder. Below is the file/directory structure. I need to achieve this in Windows BATCH script.
C:\Source\Source1\1\a.txt C:\Source\Source1\1\b.txt
C:\Source\Source1\2\a.txt C:\Source\Source1\2\b.txt
C:\Source\Source2\3\a.txt C:\Source\Source2\3\b.txt
The above files should be renamed like below:
C:\Source\Source1\1\1_a.txt C:\Source\Source1\1\1_b.txt
C:\Source\Source1\2\2_a.txt C:\Source\Source1\2\2_b.txt
C:\Source\Source2\3\3_a.txt C:\Source\Source2\3\3_b.txt
Similary, I have Source1...Source30 and under each source directory, I will have multiple folders with different numbers. I need to rename all the files under these directories and append the number(directory name) to the file name.
So far below is what I wrote:
for %%* in (.) do set CurrDirName=%%~nx*
echo %CurrDirName%
for /r %%x in (*.txt) do ren "%%x" "%CurrDirName%_%%x"
With this, I am able to achieve it in a single directory. I couldn't make it recursive. Could you guys please help me with this.
#echo OFF
SETLOCAL EnableExtensions
for /F "delims=" %%G in ('dir /B /S "C:\Source\*.txt"') do (
for %%g in ("%%~dpG.") do ECHO rename "%%~fG" "%%~nxg_%%~nxG"
)
pause
where the FOR loops are:
outer %%G loop creates a static list of .txt files (recursively), and
inner %%g loop gets the parent folder of every particular file.
The rename command is merely displayed using ECHO for debugging purposes. To make it operational, remove word ECHO (no sooner than debugged).
Moreover, I'd consider checking whether a particular file is already renamed…

ROBOCOPY - Copy folders content to a single folder WITH file list

Since I cannot add a comment, I am asking a related question.
The original posting found here works quite well.
Is there a way to use this with a list of file names? I've seen where a file list can be passed to the ROBOCOPY command but I have been unable to get it to work.
Taking a step back, I have a series of folders and there are specific files inside of them that I want to copy out to a single folder. I have a text file which lists the names of these files.
I am looking for a batch routine that will look for each of the files in the text file in each of the folders and then copy the files out to a new folder.
Thank you!
Test this - the file.txt has a filename on each line.
It doesn't handle filename conflicts.
#echo off
cd /d "c:\base\folder"
for /f "usebackq delims=" %%a in ("file.txt") do (
for /f "delims=" %%b in ('dir "%%a" /b /s /a-d ') do copy "%%b" "d:\target\folder"
)
I recently had to tackle this problem, and many files that I wanted to move to from the hierarchy to a single folder had the same name as each other, and I wanted to still flatten the hierarchy without them to being over-written.
What I did was write a script that moves the file, but renames it with the old hierarchy path in the name
for example:
source files:
C:\files\somefiles\file.txt
C:\files\otherfiles\file.txt
destination is C:\newdir\
files are created as
C:\newdir\somefiles-file.txt
C:\newdir\otherfiles-file.txt
here is the code, batch file 1 goes thru the files, batch file 2 renames and moves them (could also copy instead, if you want to preserve the source:
#echo off
for /r %%f in (*.*pr) do #renameandmovefilespart2.bat "%%f" "%%~ff" "%%~xf"
renameandmovefilespart2.bat
#echo off
Setlocal EnableDelayedExpansion
rem set the whole file path
set origWhole=%1
set origPathOnly=%2
set extension=%3
rem here you can set where the directory to hold the flattened hierarchy is
set destDir=c:\destinationDir\
rem set the directory to do a string replace
rem make this the starting directory, that you dont want in the newly renamed files
set startingDir=C:\starting\directory\
set nothing=
set slash=\
rem here you can set what the character to represent the directory indicator \ in the new files
set reaplcementDirectoryCharacter=--
set quote="
rem cut out the starting part of the directory
call set newname=%%origWhole:!startingDir!=!nothing!%%
rem replace slashes with new character
call set newname=%%newname:!slash!=!reaplcementDirectoryCharacter!%%
rem remove quotes
call set newname=%%newname:!quote!=!nothing!%%
rem #echo shortened: %newname%
rem #echo source path: %origPathOnly% newPath: %startingDir%
rem #echo extension: %extension%
rem rename the files
ren %origWhole% %newname%
rem prepare to move the file, clean up the source path
call set origPathOnly=%%origPathOnly:!quote!=!nothing!%%
move "%origPathOnly%%newname%" "%destDir%"

Copy PDF's to Folder using batch

I want to copy several unique PDF files to a unique folder.
The folders already exists.
For example
C:\Document\240C03881_10.pdf Copy this one to : C:\Endresult\240C03881\240C03881_10.pdf
C:\Document\240C03882_10.pdf Copy this one to : C:\Endresult\240C03882\240C03882_10.pdf
C:\Document\240C03883_10.pdf Copy this one to : C:\Endresult\240C03883\240C03883_10.pdf
The script should only read the first 9 digits. The script may not read _10.
Example,
Script sees 240C03881_10. But read it as 240C03881. The script is going to look if the 240C03881 folder exists. If not, the script ends/ignores it. If it does exists, it places the .pdf to its corresponding location.
This is the script i currently have, but nothing happens.. anyone? :
#echo off
setlocal EnableDelayedExpansion
rem Process all .pdf files
for %%a in (*.pdf) do (
rem Get just the file name, ie: "888123AA"
set fileName=%%~Na
rem Using the file name minus two last chars, ie: "888123"
rem get the default folder with that name
for /D %%b in (*-!fileName:~0,-3!-*) do (
rem And copy the file to that folder
copy "%%a" "%%b"
)
)
for %%p in (*.pdf) do for /f "tokens=1 delims=_" %%n in ("%%~np") do (
copy "%%~fp" "c:\endresult\%%~n\%%~nxp"
)

Resources