user2086192
user2086192

Reputation: 21

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.

Upvotes: 2

Views: 4195

Answers (3)

Suraj Kasar
Suraj Kasar

Reputation: 1

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.

Upvotes: 0

Andriy M
Andriy M

Reputation: 77687

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

Upvotes: 1

rojo
rojo

Reputation: 24476

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.

Upvotes: 1

Related Questions