Page 1 of 1

read file names and call bat files [SOLVED]

Posted: 16 Apr 2012 13:21
by dostips2012
I need help in coding for below logic. I've some interaction with dos coding:

Do till all files processed in "Folder1"
If prefix of file name = prefix1, then goto prefix1_tag
If prefix of file name = prefix2, then goto prefix2_tag
If prefix of file name = prefix3, then goto prefix3_tag
Enddo

tag prefix1_tag:
call prefix1_tag_sftp.bat
Move to Folder1\Archive
wait for 5 min

tag prefix2_tag:
call prefix2_tag_sftp.bat
Move to Folder1\Archive
wait for 5 min

tag prefix3_tag:
call prefix3_tag_sftp.bat
Move to Folder1\Archive
wait for 5 min

Re: newbie needs help - read file names and call bat files

Posted: 16 Apr 2012 16:01
by Fawers
I don't know if I understood it correctly, but here it goes.
Untested:
@echo off
set "prefix=prefix1 prefix2 prefix3"
::set "prefix" to all your file prefixes; each one must be separated by a blank space
::i'll use prefix1, prefix2 and prefix3 as an example
for %%a in (%prefix%) do call :%%a_tag "%%a"
goto eof

:prefix1_tag
call "%~1_tag_sftp.bat"
::move (see out of code box)
>nul ping -n 301 localhost
exit /b

:prefix2_tag
call "%~1_tag_sftp.bat"
::move (see out of code box)
>nul ping -n 301 localhost
exit /b

:prefix3_tag
call "%~1_tag_sftp.bat"
::move (see out of code box)
>nul ping -n 301 localhost
exit /b

:eof

About the "move" part, you have to say what exactly you want to move to Folder1\Archive.

Re: newbie needs help - read file names and call bat files

Posted: 16 Apr 2012 16:12
by Squashman
And if you are using Windows Vista or 7 you could substitute the PING commands with the TIMEOUT command.

Re: newbie needs help - read file names and call bat files

Posted: 16 Apr 2012 16:23
by Fawers
Squashman wrote:And if you are using Windows Vista or 7 you could substitute the PING commands with the TIMEOUT command.

Didn't know of that one. Will try it on my 7.

Re: newbie needs help - read file names and call bat files

Posted: 16 Apr 2012 16:56
by foxidrive
How do you define the prefix in a filename? Is it the first word of a filename separated by a space or underscore?

If so then this could work (untested):

@echo off
for %%a in (*.*) do (
for /f "delims=_ " %%b in ("%%a") do (
call "%%b_tag_sftp.bat"
Move "%%a" "Folder1\Archive"
ping -n 301 localhost >nul
)
)

Re: read file names and call bat files [SOLVED]

Posted: 18 Apr 2012 06:09
by dostips2012
I took foxidrive's idea since my filename has underscore. This way the code is simple and we don't have to update it if a new prefix is found, we just add a new _tag_sftp.bat file.

Thank you all for quick response. You guys are awesome!