Solved Search files in a folder and store it in a list

March 18, 2013 at 20:26:49
Specs: Windows 7
Hi, I want to create a Windows batch script (.bat file) to search some files inside a folder, put it in a list, and then read the list, and for each files in the list, copy it to other folder one by one. Kindly help, many thanks

See More: Search files in a folder and store it in a list

Report •

✔ Best Answer
March 21, 2013 at 21:47:22
You can "escape" the (, like this:
::==== begin snippet - the rest is same as yours
for /f "delims=" %%a in ('dir /s /b "C:\temp\*%TDate%*"') do (
echo %%a
set num=%random%
echo building send_%num%.inp

:: Build up the Connect:Direct process statements
echo SUBMIT MAXDELAY=01:00:00 > send_%num%.inp
echo SEND PROCESS >> send_%num%.inp
echo SNODE=%snode% >> send_%num%.inp
rem echo CLASS=1 >> send_%num%.inp
rem echo PRTY=1 >> send_%num%.inp
echo SENDIT COPY >> send_%num%.inp
echo FROM ( >> send_%num%.inp
echo FILE="%srcpath%%%a" >> send_%num%.inp
echo SYSOPTS="DATATYPE(BINARY) STRIP.BLANKS(NO) XLATE(NO)" >> send_%num%.inp
echo ^) TO ^( >> send_%num%.inp
echo FILE="%chissdstpath%%%a" >> send_%num%.inp
echo DISP=RPL >> send_%num%.inp
::echo SYSOPTS="DATATYPE(BINARY) STRIP.BLANKS(NO) XLATE(NO)" >> send_%num%.inp
echo ^) >> send_%num%.inp
echo COMPRESS Extended=^(CMPrlevel=4,WINdowsize=13,MEMlevel=4^) >> send_%num%.inp
:: this line is garbled, I know it's a conditional, but.. I rem'ed it out
echo IF %SENDIT% EQ 0 (
echo MOVETOSCS RUN TASK PNODE ^(PGM=WindowsNT/95^) >> send_%num%.inp
echo SYSOPTS ="cmd(copy '%srcpath%%%a' %scspath%)" >> send_%num%.inp
echo EIF >> send_%num%.inp
echo PEND; >> send_%num%.inp
echo quit; >> send_%num%.inp
::===== end snippet

Here is what came out the "back" end:
SUBMIT MAXDELAY=01:00:00
SEND PROCESS
SNODE=CDNODE
SENDIT COPY
FROM (
FILE="C:\temp\a20130322.txt"
SYSOPTS="DATATYPE(BINARY) STRIP.BLANKS(NO) XLATE(NO)"
) TO (
FILE="C:\jpg\temp\a20130322.txt"
DISP=RPL
)
COMPRESS Extended=(CMPrlevel=4,WINdowsize=13,MEMlevel=4)
MOVETOSCS RUN TASK PNODE (PGM=WindowsNT/95)
SYSOPTS ="cmd(copy 'C:\temp\a20130322.txt' C:\temp\success\)"
EIF
PEND;
quit;

note that the EIF will probably not work correctly since I had to alter the IF statement
to what i thought was intended, but probably wrong interp.



#1
March 19, 2013 at 00:41:51
You need to give more detail.

=====================
M2 Golden-Triangle


Report •

#2
March 19, 2013 at 21:05:16
Let's say I need to find all files in C:\temp folder with a specific criteria. The files I'm searching have a date in YYYYMMDD format in it's filename, and I need to find those files with today's date. So for example today is 20-Mar-2013, and I have a_20130319.txt, asdf_20130320.txt, gegedd_20130320_12345.txt in C:\temp folder. This batch script will then pick asdf_20130320.txt and gegedd_20130320_12345.txt.

After I found these 2 files, I need to transfer them to some other server. So I guess a loop for each files found is necessary. The file transfer method is using Connect Direct, so it would be a plus also if someone knows how to put the Connect Direct file transfer instruction inside the batch script


Report •

#3
March 21, 2013 at 01:26:42
Dunno about the xfer but this will get the list.

:: =====  script starts here  ===============
:: 
:: LISTTODA.bat  2013-03-21 15:17:19.70
@echo off > newfile & setLocal enableDELAYedeXpansioN

call :YMD20
for /f "tokens=* delims= " %%a in ('dir/b ^| find "%YYYYMMDD%"') do (
echo.%%a
)>> newfile
goto :eof

:YMD20

@echo off > d.d

>> d.d echo E 0100 B4 2A CD 21 B4 4C CD 21
>> d.d echo N DAY.COM
>> d.d echo RCX
>> d.d echo 8
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 88 F0 B4 4C CD 21
>> d.d echo N MONTH.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 89 C8 B4 4C CD 21
>> d.d echo N YEAR.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 88 D0 B4 4C CD 21
>> d.d echo N DAT.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo Q

debug < d.d > nul
del d.d

dat
for %%a in (1 2 3 4 5 6 7 8 9                                                                  ) do if errorlevel %%a set DD=0%%a
for %%a in (                  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31) do if errorlevel %%a set DD=%%a
set D=%errorlevel%

day
if errorlevel 0 set day=Sunday
if errorlevel 1 set day=Monday
if errorlevel 2 set day=Tuesday
if errorlevel 3 set day=Wednesday
if errorlevel 4 set day=Thursday
if errorlevel 5 set day=Friday
if errorlevel 6 set day=Saturday

month
for %%a in (1 2 3 4 5 6 7 8 9         ) do if errorlevel %%a set MM=0%%a
for %%a in (                  10 11 12) do if errorlevel %%a set MM=%%a

if %MM%==01 set Mon=Jan
if %MM%==02 set Mon=Feb
if %MM%==03 set Mon=Mar
if %MM%==04 set Mon=Apr
if %MM%==05 set Mon=May
if %MM%==06 set Mon=Jun
if %MM%==07 set Mon=Jul
if %MM%==08 set Mon=Aug
if %MM%==09 set Mon=Sep
if %MM%==10 set Mon=Oct
if %MM%==11 set Mon=Nov
if %MM%==12 set Mon=Dec

year
if errorlevel 219 set YYYY=2011
if errorlevel 220 set YYYY=2012
if errorlevel 221 set YYYY=2013

set   YYYYMMDD=%YYYY%%MM%%DD%

del day.com
del month.com
del year.com
del dat.com
goto :eof
::============================ DONE YMD20 ==========================

::======  script ends here  =================

=====================
M2 Golden-Triangle


Report •

Related Solutions

#4
March 21, 2013 at 19:35:49
Hi, for this I have created the below script. Actually it is working if I didn't put this line : for /f "delims=" %%a in ('dir/s/b "C:\temp\*%TDate%*"') do (

which mean the script was originally used to transfer 1 file only. Then there is a change in the requirement, and there might be multiple files to be sent, so I put a looping in the script. However, I was hit by an error stated like this :

TO was unexpected at this time.
C:\jpg>echo ) TO ( >> send_.inp

I believe it was caused by the ')' symbol. The script might think of it as the closure bracket of the loop. It did work when I add double quote : ") TO (" but then the C:D script is the one that is not working because it didn't recognize the double quote sign. Kindly help...


Script :


@echo off

FOR /F "tokens=2 delims= " %%i IN ('date/T') DO set sysdate=%%i
FOR /F "tokens=1,2,3 delims=/" %%i IN ("%sysdate%") DO set TDate=%%k%%i%%j
FOR /F "tokens=1,2,3 delims=/" %%i IN ("%sysdate%") DO set DDate=%%j/%%i/%%k
FOR /F "tokens=1,2,3 delims=/" %%i IN ("%sysdate%") DO set SDate=%%i/%%j/%%k

Set Now=%Time%
set DNow=%Time:~0,8%
set snode=CDNODE
set srcpath=C:\temp\
set filename1=*%TDate%*
set chissdstpath=C:\jpg\temp\
set scspath=C:\temp\success\

:: FOR FILENAME1

for /f "delims=" %%a in ('dir/s/b "C:\temp\*%TDate%*"') do (
set num=%random%

:: Build up the Connect:Direct process statements
echo SUBMIT MAXDELAY=01:00:00 > send_%num%.inp
echo SEND PROCESS >> send_%num%.inp
echo SNODE=%snode% >> send_%num%.inp
rem echo CLASS=1 >> send_%num%.inp
rem echo PRTY=1 >> send_%num%.inp
echo SENDIT COPY >> send_%num%.inp
echo FROM ( >> send_%num%.inp
echo FILE="%srcpath%%%a" >> send_%num%.inp
::echo SYSOPTS="DATATYPE(BINARY) STRIP.BLANKS(NO) XLATE(NO)" >> send_%num%.inp
echo ) TO ( >> send_%num%.inp
echo FILE="%chissdstpath%%%a" >> send_%num%.inp
echo DISP=RPL >> send_%num%.inp
::echo SYSOPTS="DATATYPE(BINARY) STRIP.BLANKS(NO) XLATE(NO)" >> send_%num%.inp
echo ) >> send_%num%.inp
echo COMPRESS Extended=(CMPrlevel=4,WINdowsize=13,MEMlevel=4) >> send_%num%.inp
echo IF (SENDIT EQ 0) THEN >> send_%num%.inp
echo MOVETOSCS RUN TASK PNODE (PGM=WindowsNT/95) >> send_%num%.inp
echo SYSOPTS ="cmd(copy '%srcpath%%%a' %scspath%)" >> send_%num%.inp
echo EIF >> send_%num%.inp
echo PEND; >> send_%num%.inp
echo quit; >> send_%num%.inp

:: Submit the Connect:Direct process
"C:\Program Files\Sterling Commerce\Connect Direct v4.6.00\Common Utilities\direct" < send_%num%.inp > send_%num%.out
:: Find the line in the output that contains the process number
find "SEND" send_%num%.out | find /V "-----" > send_%num%.lin
:: Build up the command to get the result of the process
echo select statistics > cdstat_%num%.inp
FOR /F "tokens=2" %%i in (send_%num%.lin) do @echo pnumber=%%i; >> cdstat_%num%.inp
echo quit; >> cdstat_%num%.inp
:: Submit the command to get the result of the process/transfer
"C:\Program Files\Sterling Commerce\Connect Direct v4.6.00\Common Utilities\direct" -x < cdstat_%num%.inp

:: Clean up temporary files
::del /Q send_%num%.inp send_%num%.out send_%num%.lin cdstat_%num%.inp

)


Report •

#5
March 21, 2013 at 21:47:22
✔ Best Answer
You can "escape" the (, like this:
::==== begin snippet - the rest is same as yours
for /f "delims=" %%a in ('dir /s /b "C:\temp\*%TDate%*"') do (
echo %%a
set num=%random%
echo building send_%num%.inp

:: Build up the Connect:Direct process statements
echo SUBMIT MAXDELAY=01:00:00 > send_%num%.inp
echo SEND PROCESS >> send_%num%.inp
echo SNODE=%snode% >> send_%num%.inp
rem echo CLASS=1 >> send_%num%.inp
rem echo PRTY=1 >> send_%num%.inp
echo SENDIT COPY >> send_%num%.inp
echo FROM ( >> send_%num%.inp
echo FILE="%srcpath%%%a" >> send_%num%.inp
echo SYSOPTS="DATATYPE(BINARY) STRIP.BLANKS(NO) XLATE(NO)" >> send_%num%.inp
echo ^) TO ^( >> send_%num%.inp
echo FILE="%chissdstpath%%%a" >> send_%num%.inp
echo DISP=RPL >> send_%num%.inp
::echo SYSOPTS="DATATYPE(BINARY) STRIP.BLANKS(NO) XLATE(NO)" >> send_%num%.inp
echo ^) >> send_%num%.inp
echo COMPRESS Extended=^(CMPrlevel=4,WINdowsize=13,MEMlevel=4^) >> send_%num%.inp
:: this line is garbled, I know it's a conditional, but.. I rem'ed it out
echo IF %SENDIT% EQ 0 (
echo MOVETOSCS RUN TASK PNODE ^(PGM=WindowsNT/95^) >> send_%num%.inp
echo SYSOPTS ="cmd(copy '%srcpath%%%a' %scspath%)" >> send_%num%.inp
echo EIF >> send_%num%.inp
echo PEND; >> send_%num%.inp
echo quit; >> send_%num%.inp
::===== end snippet

Here is what came out the "back" end:
SUBMIT MAXDELAY=01:00:00
SEND PROCESS
SNODE=CDNODE
SENDIT COPY
FROM (
FILE="C:\temp\a20130322.txt"
SYSOPTS="DATATYPE(BINARY) STRIP.BLANKS(NO) XLATE(NO)"
) TO (
FILE="C:\jpg\temp\a20130322.txt"
DISP=RPL
)
COMPRESS Extended=(CMPrlevel=4,WINdowsize=13,MEMlevel=4)
MOVETOSCS RUN TASK PNODE (PGM=WindowsNT/95)
SYSOPTS ="cmd(copy 'C:\temp\a20130322.txt' C:\temp\success\)"
EIF
PEND;
quit;

note that the EIF will probably not work correctly since I had to alter the IF statement
to what i thought was intended, but probably wrong interp.


Report •

Ask Question