Computer Hope

Microsoft => Microsoft DOS => Topic started by: wtolentino on April 12, 2010, 09:40:52 AM

Title: Batch File That Accepts Two Input Parameter To Copy A File
Post by: wtolentino on April 12, 2010, 09:40:52 AM
we have a scheduler called TIDAL Enterprise Scheduler a software that we use to schedule a job. the scheduler runs a windows copy command to copy a file however the job failed when it does not find a file. to avoid this a batch file is a workaround to check if the file exist and if not return a friendly message that tells the file is not found.

i am attempting to write a batch file that will accepts two input parameters. the first parameter is to tell what files is to be copied, and the second parameter is to tell where the files are to be copied.

basically here is what i have so far:

Code: [Select]
@echo off
rem parameter settings
set pInputFile=%1%
set pOutputFile=%2%
set vDate=%date%

rem display system info
ver
echo Current sysdate date: %vDate%

rem display the source folder listing
echo Source folder listing
echo.
cd %~p1
dir %~p1

echo.
echo looking for the file %pInputFile%
echo.

for %%a in (%pInputFile%) do (
if EXIST %%a (echo %%a was found) ELSE (echo %~nx1 missing))

when i run the batch file with this command:
Code: [Select]
check_file.bat "e:\apps\ias\travelvouchertst\in\*FPCSFADZ_*.ENC" "e:\apps\ias\travelvouchertst\out"

i got this output:
Code: [Select]
Microsoft Windows [Version 5.2.3790]
Current sysdate date: Mon 04/12/2010
Source folder listing

 Volume in drive E is Data
 Volume Serial Number is 02EC-D431

 Directory of E:\APPS\IAS\travelvouchertst\in

04/02/2010  12:05 PM    <DIR>          .
04/02/2010  12:05 PM    <DIR>          ..
04/08/2010  11:50 AM           883,908 1FPCSFADZ_sample01.ENC
03/23/2010  12:00 PM           155,832 1FPCSFAEA_sample01.ENC
04/08/2010  11:50 AM         1,008,522 2FPCSFADZ_sample02.ENC
04/08/2010  11:50 AM           860,688 3FPCSFADZ_sample03.ENC
04/08/2010  11:50 AM           873,330 4FPCSFADZ_sample04.ENC
03/23/2010  03:21 PM                 0 ExpenseAnywhereTempFile.null
03/23/2010  04:15 PM         1,008,522 FPCSFADZ_20100323_051747.ENC
03/24/2010  05:20 AM           860,688 FPCSFADZ_20100324_051835.ENC
03/25/2010  04:18 AM           873,330 FPCSFADZ_20100325_041614.ENC
03/26/2010  05:25 AM           883,908 FPCSFADZ_20100326_052315.ENC
03/23/2010  12:00 PM           155,832 FPCSFAEA_20100323_091554.ENC
              11 File(s)      7,564,560 bytes
               2 Dir(s)   9,253,732,352 bytes free

looking for the file "e:\apps\ias\travelvouchertst\in\*FPCSFADZ_*.ENC"

1FPCSFADZ_sample01.ENC missing


Completed at 4/12/2010 11:36 AM

the files are existing and if i try to replace this line of code:
Code: [Select]
for %%a in (%pInputFile%) do (
with this actual file it works:
Code: [Select]
for %%a in (*FPCSFADZ_*.ENC) do (

Code: [Select]
Microsoft Windows [Version 5.2.3790]
Current sysdate date: Mon 04/12/2010
Source folder listing

 Volume in drive E is Data
 Volume Serial Number is 02EC-D431

 Directory of E:\APPS\IAS\travelvouchertst\in

04/02/2010  12:05 PM    <DIR>          .
04/02/2010  12:05 PM    <DIR>          ..
04/08/2010  11:50 AM           883,908 1FPCSFADZ_sample01.ENC
03/23/2010  12:00 PM           155,832 1FPCSFAEA_sample01.ENC
04/08/2010  11:50 AM         1,008,522 2FPCSFADZ_sample02.ENC
04/08/2010  11:50 AM           860,688 3FPCSFADZ_sample03.ENC
04/08/2010  11:50 AM           873,330 4FPCSFADZ_sample04.ENC
03/23/2010  03:21 PM                 0 ExpenseAnywhereTempFile.null
03/23/2010  04:15 PM         1,008,522 FPCSFADZ_20100323_051747.ENC
03/24/2010  05:20 AM           860,688 FPCSFADZ_20100324_051835.ENC
03/25/2010  04:18 AM           873,330 FPCSFADZ_20100325_041614.ENC
03/26/2010  05:25 AM           883,908 FPCSFADZ_20100326_052315.ENC
03/23/2010  12:00 PM           155,832 FPCSFAEA_20100323_091554.ENC
              11 File(s)      7,564,560 bytes
               2 Dir(s)   9,253,732,352 bytes free

looking for the file "e:\apps\ias\travelvouchertst\in\*FPCSFADZ_*.ENC"

1FPCSFADZ_sample01.ENC was found
2FPCSFADZ_sample02.ENC was found
3FPCSFADZ_sample03.ENC was found
4FPCSFADZ_sample04.ENC was found
FPCSFADZ_20100323_051747.ENC was found
FPCSFADZ_20100324_051835.ENC was found
FPCSFADZ_20100325_041614.ENC was found
FPCSFADZ_20100326_052315.ENC was found


Completed at 4/12/2010 11:42 AM

why can't i use a parameter to do a loop? please advise.

thanks,
warren
Title: Re: Batch File That Accepts Two Input Parameter To Copy A File
Post by: Sidewinder on April 12, 2010, 01:31:22 PM
From the looks of it, these two parameters are coming from the command line:

Quote
set pInputFile=%1%
set pOutputFile=%2%

If so, try using:

Code: [Select]
set pInputFile=%1
set pOutputFile=%2

 8)

Title: Re: Batch File That Accepts Two Input Parameter To Copy A File
Post by: Salmon Trout on April 12, 2010, 01:40:28 PM
Quote
set pInputFile=%1%
set pOutputFile=%2%

to clarify

passed parameters have only one % sign, before the number

right... %1 %2 %3 etc

wrong... %1% %2% %3% etc
Title: Re: Batch File That Accepts Two Input Parameter To Copy A File
Post by: ---saster--- on April 13, 2010, 09:00:08 AM
you should use ~ for possible quotes

Code: [Select]
set "pInputFile=%~1"
set "pOutputFile=%~2"

Title: Re: Batch File That Accepts Two Input Parameter To Copy A File
Post by: wtolentino on April 13, 2010, 09:28:12 AM

thank you all that works when i removed the extra % from the parameter and make it like this below:
Code: [Select]
set "pInputFile=%~1"
set "pOutputFile=%~2"

there is another thing that's going on with the batch file. when the file is not found it didn't go thru ELSE. please advise.
Title: Re: Batch File That Accepts Two Input Parameter To Copy A File
Post by: wbrost on April 13, 2010, 11:54:28 AM
Try the following:

Code: [Select]
if EXIST %%a (echo %%a was found) ELSE echo %~nx1 missing