Search specific string in filename and copy

June 16, 2009 at 19:29:44
Specs: Windows Vista, 4gb
Hi,
I am trying to write a batch file wherein I am
looking for a specific file having, only 4 digits in
the file name and then copy it to the dest. For
e.g., 1234.xls. The file name should only
contain 4 digits and nothing else.
Can anyone please help ?

See More: Search specific string in filename and copy

Report •


#1
June 16, 2009 at 19:44:55
No more or less? Just four?


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
June 16, 2009 at 19:49:03
Yes. I just want to search a file having only 4 digits in its filename.

Report •

#3
June 16, 2009 at 20:32:06
if you can download gawk for windows(see my sig)
BEGIN{
 FS="\\"	
 dest = "c:\\tmp" #destination directory
 q="\042" 
}
{
	split($NF,f,".")
	file=f[1];	ext=f[2]	
	if ( file ~ /^[0-9][0-9][0-9][0-9]$/){		
		cmd="copy "q $0 q" "q dest"\\"file"."ext q
		system(cmd)
		close(cmd)
	}
}

save the above as myscript.awk , and on command line

c:\test> dir /b/s | gawk -f myscript.awk

GNU win32 packages | Gawk


Report •

Related Solutions

#4
June 16, 2009 at 20:39:47
Hi ghostdog,
Thanks for replying back. I was actually looking this code in
batch b'coz there is another code linked with this. This is just a
part of it.
But thanks again.

Report •

#5
June 16, 2009 at 22:28:41
This is wired for ????.txt; I think you can figure out how to make it suit your rig.

==============================
@echo off & setLocal enableDELAYedexpansion

for /f "tokens=* delims= " %%a in ('dir/b/a-d ????.txt') do (
set name=%%~Na
if "!name:~3,1!" equ "" (
echo reject %%a not 4 chars
) else (
call :sub1
if !chk! equ OK (
echo copy %%a c:\files
) else (
echo reject %%a contains non-numeric char
)
)
)
)

goto :eof

:sub1

set chk=NG
for /L %%d in (0 1 9999) do (
if !name! equ %%d (
set chk=OK
)
)

goto :eof


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#6
June 16, 2009 at 22:52:11
Hi mechanix2go,
Basically, there is an .csv file i need to copy from source to
destination. The only issue is the source is having too many .csv
files. My job is to automate this, by copy only that file having a 4
digit job code named .csv file.
By the way, I am not sure where do i need to change the souce
and destination.

Report •

#7
June 16, 2009 at 23:24:24
@echo off & setLocal enableDELAYedexpansion

set /p src=src ?
set /p dest=dest ?
pushd !src!

for /f "tokens=* delims= " %%a in ('dir/b/a-d ????.txt') do (
set name=%%~Na
if "!name:~3,1!" equ "" (
echo reject %%a not 4 chars
) else (
call :sub1
if !chk! equ OK (
copy %%a !dest!
) else (
echo reject %%a contains non-numeric char
)
)
)
)

goto :eof

:sub1

set chk=NG
for /L %%d in (0 1 9999) do (
if !name! equ %%d (
set chk=OK
)
)

goto :eof


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#8
June 16, 2009 at 23:40:43
just find the 4 digits this way
dir /b | findstr "^[0-9][0-9][0-9][0-9].*$"

use a for loop to get the results.

GNU win32 packages | Gawk


Report •

#9
June 17, 2009 at 01:38:58
That finds a 4 d and 7d.

C:\files>dir /b | findstr "^[0-9][0-9][0-9][0-9].*$"
5191766.txt
1235.txt


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#10
June 17, 2009 at 04:40:02
C:\test>dir /b 123*
1234.mkv
1234555.mkv
1234555sdfdsf.mkv
1234sfsdf.txt

C:\test>dir /b | findstr "^[0-9][0-9][0-9][0-9][.].*$"
1234.mkv

C:\test>

GNU win32 packages | Gawk


Report •

#11
June 17, 2009 at 07:07:29
That seems to work.


=====================================
If at first you don't succeed, you're about average.

M2


Report •


Ask Question