Computing.Net > Forums > Programming > Search specific string in filename and copy

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.

Search specific string in filename and copy

Reply to Message Icon

Name: Napster.bugs
Date: June 16, 2009 at 19:29:44 Pacific
OS: Windows Vista
CPU/Ram: 4gb
Subcategory: Batch
Comment:

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 ?



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: June 16, 2009 at 19:44:55 Pacific
Reply:

No more or less? Just four?


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

M2


0

Response Number 2
Name: Napster.bugs
Date: June 16, 2009 at 19:49:03 Pacific
Reply:

Yes. I just want to search a file having only 4 digits in its filename.


0

Response Number 3
Name: ghostdog
Date: June 16, 2009 at 20:32:06 Pacific
Reply:

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


0

Response Number 4
Name: Napster.bugs
Date: June 16, 2009 at 20:39:47 Pacific
Reply:

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.


0

Response Number 5
Name: Mechanix2Go
Date: June 16, 2009 at 22:28:41 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: Napster.bugs
Date: June 16, 2009 at 22:52:11 Pacific
Reply:

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.


0

Response Number 7
Name: Mechanix2Go
Date: June 16, 2009 at 23:24:24 Pacific
Reply:

@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


0

Response Number 8
Name: ghostdog
Date: June 16, 2009 at 23:40:43 Pacific
Reply:

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


0

Response Number 9
Name: Mechanix2Go
Date: June 17, 2009 at 01:38:58 Pacific
Reply:

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


0

Response Number 10
Name: ghostdog
Date: June 17, 2009 at 04:40:02 Pacific
Reply:

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


0

Response Number 11
Name: Mechanix2Go
Date: June 17, 2009 at 07:07:29 Pacific
Reply:

That seems to work.


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

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon

Writing a code for proble... java programk code needed...



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: Search specific string in filename and copy

search for string in batch www.computing.net/answers/programming/search-for-string-in-batch/16124.html

Copying String to String In C www.computing.net/answers/programming/copying-string-to-string-in-c/699.html

ShellExecute() and string problem www.computing.net/answers/programming/shellexecute-and-string-problem/6560.html