Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Batch script to move files based on file regex.

1,678 views
Skip to first unread message

priyanka singhal

unread,
Jan 28, 2011, 9:43:31 AM1/28/11
to
Hi There,

I want to create a batch script which moves the files residing in
directory to some other directory based on the file names. And the
fileregex and destination directory are stored in config.txt which
looks like thi
ABC*.*;C:\TEST\123
XYZ*.*;C:\TEST\345


SO the batch script should read the files in directory say c:\Receive
and match the pattern in config file and accordingly move it to
destination directory as mentioned in config file.

I am very new to scripting . Kindly help!!

PS


foxidrive

unread,
Jan 28, 2011, 10:02:32 AM1/28/11
to

My first thought is that the semicolon you use as a delimiter can occur
in a filename and a folder - and so semicolon is a poor choice of
delimiter.

We would also need to know if there will be any filename clashes and
what to do if a file already exists. Overwrite by default?


In essence you can do what you require with this code (untested) when
executed in the c:\Receive folder:

@echo off
for /f "tokens=1,2 delims=;" %%a in ("c\:config.txt") do (
md "%%b" 2>nul
move /y "%%a" "%%b"
)


--
Regards,
Mic

priyanka singhal

unread,
Jan 28, 2011, 10:15:16 AM1/28/11
to
> Mic- Hide quoted text -
>
> - Show quoted text -

Hi, Thanks for the snippet.
well if not semicolon, i would like to use "," as the delimiter.
and the assumption is if file already exists then it will be over
written.

Regards
PS

priyanka singhal

unread,
Jan 28, 2011, 10:24:39 AM1/28/11
to
On Jan 28, 8:15 pm, priyanka singhal <singhal.priyank...@gmail.com>
wrote:
> PS- Hide quoted text -

>
> - Show quoted text -

Also, the file names in config.txt is regex and not the complete file
name. The files in the recieve folder should match to that regex and
should be moved to respective directory.
e.g. if in config i have ABC*.*;c:\TEST\123 then the files ABC1.txt
and ABC2.txt should be moved to c:\TEST\123

Regards
PS

foxidrive

unread,
Jan 28, 2011, 10:41:49 AM1/28/11
to
On 29/01/2011 02:24, priyanka singhal wrote:

>>>> ABC*.*;C:\TEST\123
>>>> XYZ*.*;C:\TEST\345

>> well if not semicolon, i would like to use "," as the delimiter.

A comma is also a legal character in a file and foldername. I would
suggest that you use the pipe character "|"

>> and the assumption is if file already exists then it will be over
>> written.

The /Y in the move command does that.

> Also, the file names in config.txt is regex and not the complete file
> name. The files in the recieve folder should match to that regex and
> should be moved to respective directory.
> e.g. if in config i have ABC*.*;c:\TEST\123 then the files ABC1.txt
> and ABC2.txt should be moved to c:\TEST\123

ABC*.* can be used in a move command and so can something like ABC??DEF*.??E


If you mean regular expressions that are supported by SED and FINDSTR
and GAWK then you need to show examples.


--
Regards,
Mic

priyanka singhal

unread,
Jan 28, 2011, 11:47:06 AM1/28/11
to

I am looking for the kind of pattern matching which is available in
unix, perl etc. where we use grep.. I hope you got wht I mean

Regards
PS

foxidrive

unread,
Jan 28, 2011, 12:03:56 PM1/28/11
to
On 29/01/2011 03:47, priyanka singhal wrote:
> On Jan 28, 8:41 pm, foxidrive<foxidr...@gotcha.woohoo.invalid> wrote:
>> On 29/01/2011 02:24, priyanka singhal wrote:
>>
>>>>>> ABC*.*;C:\TEST\123
>>>>>> XYZ*.*;C:\TEST\345

> I am looking for the kind of pattern matching which is available in


> unix, perl etc. where we use grep.. I hope you got wht I mean

This might work - You can use a port of grep

It should merely echo the MD commands and the move commands to the
screen. Remove the echos and pause if it works as you expect it to.


@echo off
for /f "tokens=1,2 delims=;" %%a in ("c\:config.txt") do (

for /f "delims=" %%c in ('dir /b /a-d ^|grep "%%a"') do (
echo md "%%b" 2>nul
echo move /y "%%c" "%%b"
pause
)
)

--
Regards,
Mic

priyanka singhal

unread,
Feb 1, 2011, 3:08:21 AM2/1/11
to
On Jan 28, 10:03 pm, foxidrive <foxidr...@gotcha.woohoo.invalid>

Hi,

This code dint work for me. it is moving the entire config file and
not the files in the directory that i want to move . your script the
one with grep gives and error as theres no grep command in the batch
script.

kindly help

Regards
PS

foxidrive

unread,
Feb 1, 2011, 4:13:26 AM2/1/11
to
On 1/02/2011 19:08, priyanka singhal wrote:
> On Jan 28, 10:03 pm, foxidrive<foxidr...@gotcha.woohoo.invalid>
> wrote:
>> On 29/01/2011 03:47, priyanka singhal wrote:
>>
>>> On Jan 28, 8:41 pm, foxidrive<foxidr...@gotcha.woohoo.invalid> wrote:
>>>> On 29/01/2011 02:24, priyanka singhal wrote:
>>
>>>>>>>> ABC*.*;C:\TEST\123
>>>>>>>> XYZ*.*;C:\TEST\345
>>> I am looking for the kind of pattern matching which is available in
>>> unix, perl etc. where we use grep.. I hope you got wht I mean
>>
>> It should merely echo the MD commands and the move commands to the
>> screen. Remove the echos and pause if it works as you expect it to.


I made the same mistake elsewhere recently. Try this.

@echo off
for /f "tokens=1,2 delims=;" %%a in ('type "c\:config.txt"') do (


for /f "delims=" %%c in ('dir /b /a-d ^|grep "%%a"') do (
echo md "%%b" 2>nul
echo move /y "%%c" "%%b"
pause
)
)


The proper grep syntax is for you to work out.

--
Regards,
Mic

foxidrive

unread,
Feb 1, 2011, 5:51:26 AM2/1/11
to

I tested it and it appears to work here. Change this

"c\:config.txt" to "c:\config.txt"

--
Regards,
Mic

0 new messages