Page 1 of 1

Want to read a text file and do copy of the Files

Posted: 06 Feb 2018 04:56
by naraen87
I want to read a text file and its looks like
my text file backupfilenameandlocation.txt

Code: Select all

C:\Users\SVCT-WI-Jenkins\Desktop\Scripts_for_ORF2\5_cur_sys_filebackup.bat
C:\Users\SVCT-WI-Jenkins\Desktop\Scripts_for_ORF2\5_cur_sys_backup.bat
C:\Users\SVCT-WI-Jenkins\Desktop\Scripts_for_ORF2\24_Service_up.ba
C:\Users\SVCT-WI-Jenkins\Desktop\ORF2_Deployments\CAM8_Hotfix_Deployment_Instruction_CAM8_6.20.8.5.txtt
Read the above file line by line and copy the files in my dat format created folder.
Here I want handle the error while during the copy
Errorlevel is not working properly on my for loop
It's returing always 0 if the file is not exists also
I want to handle the error if anything happened wrong while the copy it needs to come out of the for loop and as well my script and send a error message.
Can anyone help me on this...
My Bat script

Code: Select all

REM *********************Take the Backup Of Current System******************************************************************
REM @echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%%MM%%DD%_%HH%%Min%%Sec%"
set "backup_location=C:\Users\SVCT-WI-Jenkins\Desktop\backup_location"
set "destination=%backup_location%\%fullstamp%"
mkdir %destination%

SetLocal EnableDelayedExpansion
::This line is just in case n has been defined before the batch file is run
Set n=
Set InputFile=C:\Users\SVCT-WI-Jenkins\Desktop\Scripts_for_ORF2\Input_File_For_Deployment\backupfilenameandlocation.txt
For /F "tokens=*" %%I IN (%InputFile%) DO (
XCOPY %%I %destination% /E /V /I /Y
echo %errorlevel%
if NOT %errorlevel%==0 ( exit /b 2 )
)
:: This line will display the variables just assigned
:: For testing only, delete when not needed

EndLocal

Re: Want to read a text file and do copy of the Files

Posted: 07 Feb 2018 04:52
by elzooilogico
Percent expansion is done before a code block is executed. you have set delayed expansion, but you are not using it.

change

Code: Select all

if NOT %errorlevel%==0 ( whatever )
to

Code: Select all

if !errorlevel! neq 0 ( whatever )

Re: Want to read a text file and do copy of the Files

Posted: 07 Feb 2018 05:38
by naraen87
Thank you @elzooilogico.

I've one more doubt on doing xcopy could you please clarify.
I've files on some UNC path drives, say for example
For the same batch file If I'm using the following text file as input backupfilenameandlocation.txt
\\vfs-or2-wi\bancs\BaNCSFS\BancsProduct\Extranet\Bancs.ear\APP-INF\lib\AllClasses.jar
\\vfs-or2-wi\bancs\BaNCSFS\BancsProduct\Extranet\Bancs.ear\APP-INF\lib\Dao.jar
\\vfs-or2-wi\bancs\BaNCSFS\BancsProduct\Extranet\Bancs.ear\APP-INF\lib\PS.jar
\\vfs-or2-wi\bancs\BaNCSFS\BancsProduct\Intranet\Bancs.ear\APP-INF\lib\AllClasses.jar
\\vfs-or2-wi\bancs\BaNCSFS\BancsProduct\Intranet\Bancs.ear\APP-INF\lib\Dao.jar
\\vfs-or2-wi\bancs\BaNCSFS\BancsProduct\Intranet\Bancs.ear\APP-INF\lib\Dao.jar
\\10.66.216.141\E$\BancsProduct\Springbatch\lib\AllClasses.jar
\\10.66.216.141\E$\BancsProduct\Springbatch\lib\Dao.jar
\\10.66.216.141\E$\BancsProduct\Springbatch\lib\PS.jar
In the Above file it do copy for the last three files except the above six files.
Can you please help me on this. How could I copy some UNC path
\\vfs-or2-wi\bancs\
As well I'm copying files from different folders but they have the same filename but they are different files in my case how do I take all the 6 files in known ways.

Re: Want to read a text file and do copy of the Files

Posted: 07 Feb 2018 09:23
by elzooilogico
be sure two write UNC paths within quotes when using xcopy. or better use robocopy

Re: Want to read a text file and do copy of the Files

Posted: 08 Feb 2018 07:31
by naraen87
Hi

If I use double quotes also I'm facing the same issue..
The command I Used

Code: Select all

xcopy "\\vfs-or2-wi\bancs\BaNCSFS\BancsProduct\Extranet\Bancs.ear\APP-INF\lib\PS.jar" C:\Users\SVCT-WI-Jenkins\Desktop\backup_location
Below is for your reference
C:\Users\SVCT-WI-Jenkins\Desktop\Scripts_for_ORF2>xcopy "\\vfs-or2-wi\bancs\BaNCSFS\BancsProduct\Extranet\Bancs.ear\APP-INF\lib\PS.jar" C:\Users\SVCT-WI-Jenkins\Desktop\backup_location
Invalid drive specification
0 File(s) copied

C:\Users\SVCT-WI-Jenkins\Desktop\Scripts_for_ORF2>

Re: Want to read a text file and do copy of the Files

Posted: 08 Feb 2018 12:45
by Squashman
naraen87 wrote:
08 Feb 2018 07:31
Hi

If I use double quotes also I'm facing the same issue..
The command I Used

Code: Select all

xcopy "\\vfs-or2-wi\bancs\BaNCSFS\BancsProduct\Extranet\Bancs.ear\APP-INF\lib\PS.jar" C:\Users\SVCT-WI-Jenkins\Desktop\backup_location
Below is for your reference
C:\Users\SVCT-WI-Jenkins\Desktop\Scripts_for_ORF2>xcopy "\\vfs-or2-wi\bancs\BaNCSFS\BancsProduct\Extranet\Bancs.ear\APP-INF\lib\PS.jar" C:\Users\SVCT-WI-Jenkins\Desktop\backup_location
Invalid drive specification
0 File(s) copied

C:\Users\SVCT-WI-Jenkins\Desktop\Scripts_for_ORF2>
One of the paths is invalid or you don't have permissions to. I would assume it is the source folder because it looks like you are running the script from the path of the destination folder.