0

I have researched the similarly titled questions, but they don't appear to be quite what I need. In addition, on my first day, I had found a post (I apologize, I can't find it now, and even though I bookmarked it, the bookmark only took me to the home page for this forum), that was very similar to what I was looking for, but was unsuccessful (not batch file savvy enough), to modify it for my purposes.

What I have is a directory with several thousand pdf files.

They're named something like this.

12345.pdf,
12345.pdf,
12347.pdf,
(and so on).

In the source directory are several thousand matching folders which are named something like this:

12345_eventnumber_eventprovider,
12346_eventnumber_eventprovider,
12347_eventnumber_REF alt#_eventprovider,
(And so on).

What I want to do is have all of the pdfs magically go to their associated folders based on their filename. The whole file name for the pdfs, the first set of numbers up to the first underscore for the folders.

At any rate, here is what I ended up with (with my pathetically limited batch file skills) after modifying the other poster's file.

@Echo off
cd /d "c:\My source Directory"
setlocal enabledelayedexpansion
for %%a in (*_*.*) do (
 for /f "delims=_" %%b in ("%%a") do (
 set "f=%%b"
   if /i "%%~xa"==".pdf" (
     md "c:\My source directory" 2>nul
     move "%%a" "C:\My destination directory!f:~0,-1!" >nul
   )

It does understand my source directory, at least based on the results. But it was a fail as all it did was create new folders (and not all of them) in my source directory. So I'm hoping someone can help me fix it and get it to do what I want?

If it were up to me, we wouldn't be tracking file sequence by putting them each in their own little folder, but would list them in a database or some such (yes, the boss wants each file in its own little folder so he can look in the MS Window and see if any numbers are missing. Lord have mercy).

But this is not up to me. Also, though there appear to be some pretty handy little shareware or even cheap software out there, our network allows little to no outside applications. At least not without an act of congress (almost literally).

But I have great hope, seeing that the batch file seems to be going in the direction I want, so thanks in advance if anyone can straighten me out and help me get the right language.

Thanks in advance!

1 Answer 1

1
@echo off
setlocal enableextensions disabledelayedexpansion
for %%a in (*.pdf) do for /d %%d in ("%%~na_*") do move "%%a" "%%d\%%a" >nul 2>&1

For each pdf file, search a folder that starts with the name of the pdf followed by an underscore and move the file to the folder

edited again to adapt to comments

@echo off
setlocal enableextensions disabledelayedexpansion

set "sourcePDFs=C:\Users\lwilso01\Desktop\Test1"
set "targetFolders=c:\users\lwilson\desktop\test2"

for %%a in ("%sourcePDFs%\*.pdf") do (
    for /d %%d in ("%targetFolders%\%%~na_*") do (
        move "%%~fa" "%%~fd\%%~nxa" >nul 2>&1
    )
    if exist "%%~fa" (
        md "%targetFolders%\%%~na_"
        move "%%~fa" "%targetFolders%\%%~nxa_"
    )
)
8
  • Thanks @MC ND I'm still doing something wrong however (per my first post, I'm fairly new at this). Where do I insert the file and folder pathways? Within the two parentheses sets? Also, am I completely replacing the previous language with yours above? Or am I inserting it somewhere? (I've tried a few different ways, none seem to work). Thank you!
    – LWilson
    Nov 4, 2014 at 18:45
  • @LWilson, see updated answer. In both cases, this does what you indicate in the question. Have i missed something?
    – MC ND
    Nov 4, 2014 at 19:05
  • No, I'm sure it works, if I understood how to modify it correctly (put my folder names in etc). I think it's more that I'm not understanding the process, and that I'm doing something wrong. Could it be that the pathways are too long? Both sets of instructions above seem to only set the pathway to the folder where the pdf files are located, none of the files move to their associated folders.
    – LWilson
    Nov 4, 2014 at 19:40
  • I created some test folders so as to make my items anonymous. Here is what I understood I needed to do to the batch you provided above., (I only used the second one, no sense in getting this more complicated). Thanks again, for all your help and patience @Echo off set "folder=C:\Users\lwilso01\Desktop\Test1" for %%a in ("%c:\users\lwilson\desktop\test1%*.pdf") do for /d %%d in ("%%c:\users\lwilson\desktop\test2\%%~na_*") do (move "%%~fa" "%%~fd\%%~nxa" >nul 2>&1 )
    – LWilson
    Nov 4, 2014 at 20:16
  • @LWilson, I understood the pdfs and the folders where stored under the same folder and that the target folders exist. Code changed to remove both asumptions.
    – MC ND
    Nov 4, 2014 at 20:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.