Finding files inside directory and its sub directories

karunya

Reputable
Apr 7, 2014
5
0
4,510
Hi

Using windows batch script:

I would like to find filenames which has 2012 and 2013 in it.
eg: hsbc12012abc.txt
hsbc22012efg.txt
hsbc32013abc.csv

I have to loop through the main directory and its sub directories for it. and get all the list and copy the

filenames to a named directory(archive)

Its really urgent and not able to get answer from google search. please help

thanks
vijay
 
Solution
I'll give you some pseudocode to get you started:

Code:
For each $filename in $directory 
  do
    If $filename contains "*201302*.csv" or $filename contains "*2012*.txt" then copy $filename $Archivedir
  enddo
endfor

Note: this code is not going to run as is. You will have to make the appropriate changes to get it working on your system. It is only intended as a guide.


karunya

Reputable
Apr 7, 2014
5
0
4,510


@setlocal enabledelayedexpansion
REM @echo off


REM set _my_datetime=%date%_%time%
set _my_datetime=%date%
set _my_datetime=%_my_datetime: =_%
set _my_datetime=%_my_datetime::=%
set _my_datetime=%_my_datetime:/=_%
set _my_datetime=%_my_datetime:.=_%

set result=false

set ArPathname=C:\JDS\Archive

pushd %ArPathname%

mkdir %_my_datetime%

echo %ArPathname%

set FPathname=C:\JDS
pushd %FPathname%

echo %FPathname%
if exist for /D %f in (*2012*.txt) goto txtfileexists ELSE :txtfilenotexists
if exist for /d /r %FPathname% %%a in ('*201302*.csv') goto csvfileexists ELSE :csvfilenotexists

:txtfileexists
echo | copy file to %ArPathname%

:csvfileexists
echo | copy file to %ArPathname%

:txtfilenotexists
echo "No txt files"

:csvfilenotexists
echo "No csv files"

zip D:\Data\Archive\%_my_datetime% D:\Data\Archive\%_my_datetime%.zip
del %ArPathname%\*.txt
del %ArPathname%\*.csv
del D:\Data\Archive\%_my_datetime%
 

karunya

Reputable
Apr 7, 2014
5
0
4,510


This line of code doesn't work. I haven't gone till end.

if exist for /D %f in (*2012*.txt) goto txtfileexists ELSE :txtfilenotexists

Trying to loop through directories and sub directories to search for a file name which has 2012 in it. (*2012*.*) and copy all of them into separate dated folder and zip them
 

karunya

Reputable
Apr 7, 2014
5
0
4,510


It will be nice if you could provide me the commands. I am not aware of windows scripting and also power shell

Thanks
 
I'll give you some pseudocode to get you started:

Code:
For each $filename in $directory 
  do
    If $filename contains "*201302*.csv" or $filename contains "*2012*.txt" then copy $filename $Archivedir
  enddo
endfor

Note: this code is not going to run as is. You will have to make the appropriate changes to get it working on your system. It is only intended as a guide.


 
Solution

karunya

Reputable
Apr 7, 2014
5
0
4,510