Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Ih ave a list of jpg files, that are named as in teh following example:
09-01556.A2.12.jpgUsing a batch file, I am copying them to a destination directory but its really important that I dont do this if there is more than 8 characters before the first period.
does anyone know of a way of incorporating this logic into the following loop:
for /f %%F in ('dir/b/a-d *.jpg') do call :sub1 %%F
goto :eof:sub1
set name=%1
md %name:~0,8%
move %* %name:~0,8%where some warning could be given if there was more or less characters in before the period ?
thanks

set test=%name:~0,9% if not "%test:~-1%"=="." echo problem set test=%test:~0,-1% if not "%test%"=="%test:.=%" echo problem

I can understand the logic flow, but I'm not having much luck incorporating it into the sub above, any pointers would be appreciated,
thanks

This is untested.....
:sub1 set name=%* set test=%name:~0,8% if "%name:~8,1%"=="." ( if "%test%"=="%test:.=%" ( md %name:~0,8% move %* %name:~0,8% goto :eof ) ) 2>&1 Echo The name "%name%" was inconsistant to expected parameters, the move was aborted. goto :eof

Oh to be able to write untested code and have it work :)
That's great, many thanks Judago I prefer the move to IF rather than IF NOT, its easier to read.
If a file to be copied already exists in the destination folder, I would like to copy it with an incremented string_number combo attached (eg filename~pic_1.jpg, filename~~pic_2.jpg etc)
is this possible to do?

I think this should do the job.
:sub1 for %%a in (n sufx pic) do set %%a= set name=%* set test=%name:~0,8% if "%name:~8,1%"=="." ( if "%test%"=="%test:.=%" ( if not exist "%name:~0,8%\*" md "%name:~0,8%" :loop if exist "%name:~0,8%\%name:~0,-4%%sufx%%pic%%n%%name:~-4%" ( set /a n+=1 set sufx=%sufx%~ set pic=pic_ goto loop ) else ( move "%name%" "%name:~0,8%\%name:~0,-4%%sufx%%pic%%n%%name:~-4%" ) goto :eof ) ) 2>&1 Echo The name "%name%" was inconsistant to expected parameters, the move was aborted. goto :eof

So I have this that works great:
@echo off
for /f %%F in ('dir/b/a-d *.jpg') do call :sub1 %%F
goto :eof:sub1
for %%a in (n sufx pic) do set %%a=
set name=%*
set test=%name:~0,8%
if "%name:~8,1%"=="." (
if "%test%"=="%test:.=%" (
if not exist "%name:~0,8%\*" md "%name:~0,8%"
:loop
if exist "%name:~0,8%\%name:~0,-4%%sufx%%pic%%n%%name:~-4%" (
set /a n+=1
set sufx=%sufx%~
set pic=pic_
goto loop
) else (
move "%name%" "%name:~0,8%\%name:~0,-4%%sufx%%pic%%n%%name:~-4%"
)
goto :eof
)
)
2>&1 Echo The image "%name%" is incorrectly named and was not moved.
goto :eof
but additional files of the same name get copied asfilename~pic1.jpg
filename~~pic2.jpg
filename~~~pic3.jpgcould the string be tweaked so that it went as:
filename~pic1.jpg
filename~pic2.jpg
filename~pic3.jpgthanks

Yep I added the extra tides in because I though you wanted them from your example:
"filename~pic_1.jpg, filename~~pic_2.jpg"
The modification is easy just change "set sufx=%sufx%~" to "set sufx=~".

oh yes, I can see that's what I asked for ;-).
Many thanks for this.
The purpose of this is to move image files from an 'export' folder from a medical imaging software package and put them into folders representing each clinical case (the 1st 8 characters and the need for some validation).
I supposed that getting a batch file to do it was more reliable than getting a person to do it as manually moving an image to an incorrect folder could lead to the wrong image getting assigned to the wrong case = potential disaster!
Having it run every few seconds would be good with some visual indication to the user (who is exporting the images) that case folders have been created would be helpful, but I'll have a tinker later.
Many thanks again

I was thinking about validating the files prior to import. The code to make the directory prior to import is as follows:
if not exist "%name:~0,8%\*" md "%name:~0,8%"
for an example filename: 09-01556.A2.12.jpg
this names the folder with the first 8 characters.
How could I get it name the folder by the whole of the filename string before the first period, and move image files based on that logic ?
so: 09-01556.A2.12.jpg goes to 09-01156
and 09-015566.A2.12.jpg goes to 09-011566Thanks

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |