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
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
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
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
>>>> 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
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
> 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
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
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
I tested it and it appears to work here. Change this
"c\:config.txt" to "c:\config.txt"
--
Regards,
Mic