Batch Script to Convert Filename to Folder and rename filename

Hello Experts

I was hoping you could create a batch script that would convert over 5,000 individual patient charts (PDFs) located in a folder called “Scans” with the file name in the format “LNAME, FNAME 10-02-1984.PDF” into a folder with the same name (less the PDF extension) and under that folder have the original PDF file renamed “CHART.PDF”

Also within that folder I’d like to have another folder created called “SESNOTES"

The transformation is shown in the attached JPG.

Thank you for your help.

Regards,
Mike
photo-folders.jpg
jumptohighAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve KnightIT ConsultancyCommented:
@echo off
cd /d "x:\scans"
for /f "tokens=*" %%d in ('dir /b /a-d *.pdf') do (
  ECHO md "%%~nd"
  ECHO move "%%~d" "%%~nd\CHART.PDF"
  ECHO md "%%~nd\SESNOTES"
)
PAUSE

Open in new window


That should do it... try it on a dir with a few files in to start with and the output should show you the commands.  if that looks OK then remove the three "ECHO" lines and try on a few again.

We can put some error checking in there if wanted, maybe not for one-off.

Steve
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
@echo off
setlocal EnableDelayedExpansion
pushd C:\Scans
for %%F in (*.pdf) do (
  md "%%~pnF\SESNOTES"
  move "%%~F" "%%~pnF\CHART.PDF"
)
popd

Open in new window

should do that.
0
jumptohighAuthor Commented:
Hi Steve,

Thanks for your help.

I apologize for my ignorance, but I don't know much about programming. Do I copy and paste the code into a file and place that file in the directory with the Scans folder?  What type of file do I create.

Thanks for your patience.

Mike
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Steve KnightIT ConsultancyCommented:
Sure.  I would do something roughly like this:

1. Create a new dir.  Take a copy of a dozen or so PDF files into that directory
2. Open Notepad, paste the code in.
3. edit the cd /d "c:\whatever" line to show the path you want it to look in of these temporary files for now.
4. File / Save As, change "type" from "Text files" to "All files".
5. Give it a name of myscript.cmd or whatever somewhere you can get to it
6. Find the file you just saved and run it.  It will show a load of commands on the screen such as MD pdfname, MOVE pdfname.pdf pdfname\pdfname.pdf etc.
7. If that looks sensible then edit the batch file (right click edit to open in notepad)
8. Remove the three ECHO words so it runs the commands instead of showing them
9. Check it works as you want on your test files
10. Make sure you have a backup of your main dir.
11. Edit the file again and change to your real path
12. Go for it...
0
jumptohighAuthor Commented:
Steve,

Thanks for the detailed steps!  It worked perfectly on the test PDFs in the test directory!!!

Later I will attempt it on all 5000 or so files.

Thank you so much!

If we decide later we want to remove the SESNOTES sub folder from some of the Folders what script can I run?

Thanks,
Mike
0
Steve KnightIT ConsultancyCommented:
Something similar can soon reverse... out at the moment but post here if you need to do it and will sort it out
Steve
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I assume you won't want to enumerate the folders where SESNOTES should be removed from, so you'll need a different criterium. Something like "if no files in SESNOTES".
0
jumptohighAuthor Commented:
Exactly, if no files in SESNOTES, remove SESNOTES folder.

Thanks again!

Mike
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
@echo off
pushd c:\Scans
for /D %%D in (*.pdf) do ^
for /F "tokens=*" %%F in ('dir /b "%~D\SESNOTES\" ^| find /c /v " "') do ^
if %%F == 0 rd %D\SESNOTES
popd

Open in new window

Another approach is "brute force" - remove all SESNOTES folders, and ignore the error message if there are files contained (which prevents rd from deleting the folder):
@echo off
pushd c:\Scans
for /D %%D in (*.pdf) do rd %%D\SESNOTES 2>nul
popd

Open in new window

0
Steve KnightIT ConsultancyCommented:
I think Qlemo has covered your second question there.  Have you run the main script over your 5000 files yet and any issues?

Steve
0
jumptohighAuthor Commented:
Steve, Yes I was able to run the main script on the 5000+ files and it worked without any errors!!

Again thank you!

Qlemo, thank you for the SESNOTES removal!

All the best,
Mike
0
Steve KnightIT ConsultancyCommented:
No problem, glad it helped.

Steve
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.