Copy PDF's to Folder using batch - file

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"
)

Related

Copy list of files to specific folder locations

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

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

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

Windows batch move (or copy) PDF file by name to corresponding folder

I am trying to find a way to create a Windows batch script that will look at a target folder full of .pdf files and move (or copy) them to another directory with existing subfolders based on the filename.
The files and folders are names of actual people. I want to be able to get that person's pdf into their existing folder using a script.
Say I have 2 files in my folder; smithJohn015.pdf and thomasBill030.pdf.
I would like to be able to put smithJohn015.pdf into folder SmithJohn and thomasBill030.pdf into folder ThomasBill.
I don't want the script to create new folders or overwrite existing files if there's a duplicate filename.
I'm not necessarily looking for anyone to write a script for me, but if anyone can just get me started in the right direction it would be appreciated.
Try modifying this answer for your evil purposes.
#echo off
setlocal
pushd "c:\path\to\PDFs"
for /d %%I in (c:\Path\To\People\*) do (
for %%F in (*) do (
for /f %%A in ('echo %%~nF ^| find /i "%%~nI"') do (
set /p a="Moving %%F to %%I... "<NUL
move "%%F" "%%I" >NUL
echo Done.
)
)
)
popd
You'll need to add a check for if not exist pdffile before the move, but there's a starting direction for you anyway.
The following assumes the target subfolders' location contains only the subfolders where the PDFs may go and that every PDF that you want to move has a name formatted as the corresponding subfolder's name followed by exactly three characters (same as in your examples):
#ECHO OFF
FOR /D %%D IN ("D:\path\to\subfolders\*") DO (
MOVE "D:\path\to\PDFs\%%~nD???.pdf" "%%D"
)
Or as a one-liner to execute directly at the command prompt:
FOR /D %D IN ("D:\path\to\subfolders\*") DO (MOVE "D:\path\to\PDFs\%~nD???.pdf" "%D")
folderlist.txt contains all names of folders in which you want to copy respective PDFs.
xcopy is copy command. format xcopy "source" "destination" /parameters.
pause is just to keep command window on
for /F "tokens=*" %%A in (folderlist.txt) do (
xcopy "E:\path\to\source folder\<prefix>%%A<suffix>.pdf" "E:\path\to\destination folder\<prefix>%%A<suffix>\" /s)
pause
You can use wildcards in source & destination paths.

Resources