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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
naraen87
Posts: 17
Joined: 21 Dec 2017 06:41

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

#1 Post by naraen87 » 06 Feb 2018 04:56

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

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

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

#2 Post by elzooilogico » 07 Feb 2018 04:52

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 )

naraen87
Posts: 17
Joined: 21 Dec 2017 06:41

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

#3 Post by naraen87 » 07 Feb 2018 05:38

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.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

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

#4 Post by elzooilogico » 07 Feb 2018 09:23

be sure two write UNC paths within quotes when using xcopy. or better use robocopy

naraen87
Posts: 17
Joined: 21 Dec 2017 06:41

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

#5 Post by naraen87 » 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>

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

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

#6 Post by Squashman » 08 Feb 2018 12:45

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.

Post Reply