Computing.Net > Forums > Programming > test if filename meets rules ?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

test if filename meets rules ?

Reply to Message Icon

Name: accipiter
Date: March 12, 2009 at 15:24:04 Pacific
OS: Windows XP
CPU/Ram: 2gb
Product: N/a / N/A
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: March 13, 2009 at 01:55:31 Pacific
Reply:

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



0

Response Number 2
Name: reno
Date: March 13, 2009 at 03:20:17 Pacific
Reply:

great stuff


0

Response Number 3
Name: accipiter
Date: March 13, 2009 at 15:09:11 Pacific
Reply:

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


0

Response Number 4
Name: Judago
Date: March 13, 2009 at 15:41:54 Pacific
Reply:

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


0

Response Number 5
Name: accipiter
Date: March 13, 2009 at 16:46:32 Pacific
Reply:

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?



0

Related Posts

See More



Response Number 6
Name: Judago
Date: March 13, 2009 at 18:26:01 Pacific
Reply:

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


0

Response Number 7
Name: accipiter
Date: March 14, 2009 at 03:10:54 Pacific
Reply:

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


0

Response Number 8
Name: Judago
Date: March 14, 2009 at 03:57:18 Pacific
Reply:

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=~".


0

Response Number 9
Name: accipiter
Date: March 14, 2009 at 06:37:00 Pacific
Reply:

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


0

Response Number 10
Name: accipiter
Date: March 15, 2009 at 03:24:16 Pacific
Reply:

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


0

Response Number 11
Name: Judago
Date: March 15, 2009 at 04:01:25 Pacific
Reply:

<untested>

for /f "tokens=1 delims=." %%g in ("%name%") do if not exist "%%g\*" md "%%g"


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: test if filename meets rules ?

Batch file, if filename exists www.computing.net/answers/programming/batch-file-if-filename-exists/17368.html

Help! Test if a number is Prime in C++ www.computing.net/answers/programming/help-test-if-a-number-is-prime-in-c/2794.html

if filename exist, append it www.computing.net/answers/programming/if-filename-exist-append-it/18634.html