Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 ?

No more or less? Just four?
=====================================
If at first you don't succeed, you're about average.M2

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

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.

This is wired for ????.txt; I think you can figure out how to make it suit your rig.
==============================
@echo off & setLocal enableDELAYedexpansionfor /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

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.

@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

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.

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

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>

That seems to work.
=====================================
If at first you don't succeed, you're about average.M2

![]() |
Writing a code for proble...
|
java programk code needed...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |