test if filename meets rules ?

N/a / N/A
March 12, 2009 at 15:24:04
Specs: Windows XP, 2gb
Ih ave a list of jpg files, that are named as in teh following example:
09-01556.A2.12.jpg

Using 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


See More: test if filename meets rules ?

Report •


#1
March 13, 2009 at 01:55:31
set test=%name:~0,9%
if not "%test:~-1%"=="." echo problem
set test=%test:~0,-1%
if not "%test%"=="%test:.=%" echo problem



Report •

#2
March 13, 2009 at 03:20:17
great stuff

Report •

#3
March 13, 2009 at 15:09:11
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


Report •

Related Solutions

#4
March 13, 2009 at 15:41:54
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


Report •

#5
March 13, 2009 at 16:46:32
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?



Report •

#6
March 13, 2009 at 18:26:01
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


Report •

#7
March 14, 2009 at 03:10:54
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 as

filename~pic1.jpg
filename~~pic2.jpg
filename~~~pic3.jpg

could the string be tweaked so that it went as:

filename~pic1.jpg
filename~pic2.jpg
filename~pic3.jpg

thanks


Report •

#8
March 14, 2009 at 03:57:18
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=~".


Report •

#9
March 14, 2009 at 06:37:00
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


Report •

#10
March 15, 2009 at 03:24:16
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-011566

Thanks


Report •

#11
March 15, 2009 at 04:01:25
<untested>
for /f "tokens=1 delims=." %%g in ("%name%") do if not exist "%%g\*" md "%%g"


Report •


Ask Question