Reading file name/making folder w/ batch file

November 18, 2010 at 11:21:21
Specs: Windows XP
I am working on an off-site dictation/transcription hosting system for a doctor's office. It works correctly now, but I am trying to clean up the folder tree so the information is saved in folders by date rather than just one folder containing everything.

The doctor will plug in their digital voice recorder, which will sync the voice files and automatically begin running scripts. Each voice file has an accompanying xml file with info about the voice file. They are named in the following scheme: YYYY MM DD ## ##.DSS and YYYY MM DD ## ##.xml. I would like to make/append my batch file(s) to read the year, month, and day in the filename and make appropriate folders, then move the files to the correct folder. I am sure that this will utilize the FOR command and some options but I am having trouble understanding the syntax well enough to write it from scratch. (Also, I cannot use the creation/modified date because it will be whatever day the file was synced with the computer and not necessarily the date the recorder created it.)

Example file .DSS filenames:
2010 11 15 16 56.DSS
2010 11 15 17 09.DSS
2010 11 15 17 10.DSS
2010 11 14 12 10_001.DSS
2010 11 12 14 38.DSS
2010 11 16 11 38.DSS

Example .xml file names:
2010 11 15 16 56.xml
2010 11 15 17 09.xml
2010 11 15 17 10.xml
2010 11 14 12 10_001.xml
2010 11 12 14 38.xml
2010 11 16 11 38.xml

This script would ideally create the following folders and move the proper files to them:

This will get all of the files in the proper folder by date, and the remainder of my script will then upload it to the server. The transcriptionist will run another batch file which will download all of the folders containing the voice files (this already exists). I would like to make a similar batch script to duplicate the directory tree from the Dictation folder inside of the Transcription folder so they have a place to save their files appropriately. So using the previous example:

Batch script will read C:\Hosting\Dictation which contains:

and will make the following (without any files inside the folders):

As a precaution, I need to make sure that if the directory already exists, the files are simply moved to the right place rather than creating duplicate directories or potentially overwriting or deleting a file in both scripts I need help with. I have looked for days for a solution to this, and have found several examples of similar requests, but not close enough examples for me to use in this application. Thank you for your help and please feel free to contact me with any questions.

Thank you!
Joey Phillips

See More: Reading file name/making folder w/ batch file

Report •

November 19, 2010 at 09:59:24
Joy , I have one question, WIl the file name contains space as separtor. I mean to say
" 2010 11 12 14 38.DSS " will this file really contain a space after YYYY MM and DD. IF so then this task can be accomplished. Space or any seprator should work, Don't worry if space is not there. It can be done but will take extra time to create a long batch.

Subhash Chandra.

Report •

November 19, 2010 at 16:56:35
Yes, the filename will be exactly as listed, with spaces in between. I can change it to using a hyphen or underscore or something else, but it is currently set up as a space to separate digits which I would prefer to use as it is already in place. I am assuming that it really doesnt matter if it is a space or a character as I believe the script will just look for the space or hyphen as one in the same.(Whichever it is set to seek out)

Thank you for your help!
Joey Phillips

Report •

November 22, 2010 at 11:18:03
hello ..
I guess it will work as you said..I tested in the scenario you mentioned and it does the job.

@echo off
dir /b *.dss *.xml>filelist.txt

for /f "tokens=1* delims=" %%g in (filelist.txt) do (

for /f "tokens=1,2,3 delims= " %%a in ("%%g") do md C:\Hosting\Dictation\%%a
for /f "tokens=1,2,3 delims= " %%a in ("%%g") do md C:\Hosting\Dictation\%%a\%%b
for /f "tokens=1,2,3 delims= " %%a in ("%%g") do md C:\Hosting\Dictation\%%a\%%b\%%c
for /f "tokens=1,2,3 delims= " %%a in ("%%g") do move "%%g" C:\Hosting\Dictation\%%a\%%b\%%c

) >log.txt

echo done..


BY the way this is not a good looking, I mean does not echo anything on screen while running but I believe you can take care of that...

Let me know if works..and make sure to test it in a temp directory properly before using on production..

Subhash Chandra.

Report •

Related Solutions

November 22, 2010 at 11:23:14
seems the code word wraped..It will not work if you just copy paste.

i uploaded it to my server,Download or view in browser.

Subhash Chandra.

Report •

November 22, 2010 at 17:56:23

Thank you, It did work correctly as written. I ran into a problem when changing the directory in the FOR section though, specifically using a dir with a space in it. My software installer and all other scripts written to access C:\ Hosting\..., with a space between .com and Hosting. For my post for help, I just called it C:\Hosting. After I remembered what I posted, I figured I would just change it to the correct folder and put it in quotes later when I implemented it. (Change all C:/Hosting/... to "C:/ Hosting/..."). This is apparently a problem when using the FOR command as I cannot seem to come up with any modification of your script to facilitate getting the files into the right folder. I get syntax errors, read errors, etc.

I have tried:

set dict=C:\ Hosting\Dictation
for /f "tokens=1,2,3 delims= " %%a in ("%%g") do md "%dict%\%%a"

set dict=C:\ Hosting\Dictation
for /f "tokens=1,2,3 delims= " %%a in ("%%g") do md "%dict%"\%%a

set dict="C:\ Hosting\Dictation"
for /f "tokens=1,2,3 delims= " %%a in ("%%g") do md %dict%\%%a

for /f "tokens=1,2,3 delims= " %%a in ("%%g") do md "C:\ Hosting"\%%a

for /f "tokens=1,2,3 delims= " %%a in ("%%g") do md C:\ Hosting\%%a
(which I knew wouldn't work but I felt I had to try it anyway)

This is also a problem with the MOVE command as well.

Is there anything you can think of to modify this to get them into the directory structure already set up, or am I going to need to modify my installer and other scripts to a non-spaced folder name like C:\Joey-Phillips.comHosting?

Thank you again, and if my head was screwed on correctly, this would be working already thanks to you!

Much appreciation...
Joey Phillips

Report •

November 23, 2010 at 06:04:33
dont worry,I made required changes and now It will work..(tested it) Download the code from the same location again and let me know.


While running it will pop out lot of things on screen, Either bear with it or redirect to a nul value.

Subhash Chandra.

Report •

November 23, 2010 at 19:40:15
I seem to be encountering the same problem I had when I used the variable dict rather than _dir, which is what it appears that you used. I am also appearing to have an issue getting it to read the filelist.txt file. I will post the code and results...

@echo off
set _dir=C:\ Hosting
dir "%_dir%\Dictation" /b *.dss *.xml>"%_dir%\filelist.txt"
echo 1
for /f "tokens=1* delims=" %%g in ("%_dir%\filelist.txt") do (
for /f "tokens=1,2,3 delims= " %%a in ("%%g") do md "%_dir%\Dictation\%%a"
for /f "tokens=1,2,3 delims= " %%a in ("%%g") do md "%_dir%\Dictation\%%a\%%b"
for /f "tokens=1,2,3 delims= " %%a in ("%%g") do md "%_dir%\Dictation\%%a\%%b\%%c"
for /f "tokens=1,2,3 delims= " %%a in ("%%g") do move "%%g" "%_dir%\Dictation\%%a\%%b\%%c"
echo done..

File Not Found
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
Press any key to continue . . .

Thanks again for your help, sorry to be a problem child lol.

Report •

November 24, 2010 at 07:00:48
dir "%_dir%\Dictation" /b *.dss *.xml>"%_dir%\filelist.txt"

this line seems problematic.

Put the batch file in same folder where you have your files..try not to modify the code.
it will work

Subhash Chandra.

Report •

Ask Question