Jayesh Acharya
asked on
dos batch if exists problem
I am created a ms dos batch file that will check if a file exists in a drectory, but I keep on getting the unexpted at this time problem ...
here is what I am doing
set SrcFileDir=C:\SRC\
set SrcFile=TEST.TXT
set DestinationFileDir=c:\DEST \
set CASENumberForPromotion=123
set PromotionDate=20110101
set dosDumpFileName=sample.log
set DirCopyOldFilesDIR=%Destin ationFileD ir%%Promot ionDate%\% CASENumber ForPromoti on%\
set OldFileExtenxtion=.%Curren tDateTime%
::------------------------ ---------- ---------- ---------- ------
:: Define how to copy the files
::------------------------ ---------- ---------- ---------- ------
:CopyFiles
set CopySource=%SrcFileDir%%Sr cFile%
set DirCopyOldFilesDIR=%Destin ationFileD ir%%Promot ionDate%\% CASENumber ForPromoti on%\
set OldFileExtenxtion=.%Curren tDateTime%
echo. CopySource=%SrcFileDir%%FI LENAME% >> %dosDumpFileName%
For /F "tokens=*" %%n in ('dir %CopySource% /b') Do (
set TCopyFile=%%n
If Not Exist %DestinationFileDir%%TCopy File% (Goto:CopyFile) Else (Goto:RenameCopyFile))
GOTO:EOF
:CopyFile
echo. copy %SrcFileDir%%TCopyFile% %DestinationFileDir%
GOTO:EOF
:RenameCopyFile
echo. rename %SrcFileDir%%TCopyFile% %SrcFileDir%OLD_%TCopyFile %%OldFileE xtenxtion%
echo. copy %SrcFileDir%%TCopyFile% %DestinationFileDir%
GOTO:EOF
here is what I am doing
set SrcFileDir=C:\SRC\
set SrcFile=TEST.TXT
set DestinationFileDir=c:\DEST
set CASENumberForPromotion=123
set PromotionDate=20110101
set dosDumpFileName=sample.log
set DirCopyOldFilesDIR=%Destin
set OldFileExtenxtion=.%Curren
::------------------------
:: Define how to copy the files
::------------------------
:CopyFiles
set CopySource=%SrcFileDir%%Sr
set DirCopyOldFilesDIR=%Destin
set OldFileExtenxtion=.%Curren
echo. CopySource=%SrcFileDir%%FI
For /F "tokens=*" %%n in ('dir %CopySource% /b') Do (
set TCopyFile=%%n
If Not Exist %DestinationFileDir%%TCopy
GOTO:EOF
:CopyFile
echo. copy %SrcFileDir%%TCopyFile% %DestinationFileDir%
GOTO:EOF
:RenameCopyFile
echo. rename %SrcFileDir%%TCopyFile% %SrcFileDir%OLD_%TCopyFile
echo. copy %SrcFileDir%%TCopyFile% %DestinationFileDir%
GOTO:EOF
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i get "unexpected at this time"
the error seems to be when it tried to check for the the file existance
the error seems to be when it tried to check for the the file existance
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok this has got past the error, but for some reason the check
If Not Exist %DestinationFileDir%!TCopy File!
is not logically working
unless I am reading it wrong.
I read this if as :
If the file c:\DEST\TEST.TXT does not exist then the if is TRUE
But when running it it comes out false.
any help??
If Not Exist %DestinationFileDir%!TCopy
is not logically working
unless I am reading it wrong.
I read this if as :
If the file c:\DEST\TEST.TXT does not exist then the if is TRUE
But when running it it comes out false.
any help??
ASKER
no i am just trying to loop through all the files that are in the directory, If I dont give a srouce file name
then I wanted to program to loop through every file in the srouce directory
So I am trying to make this work with just one file
You are right about the copy / reanme but at this point I wanted to fisrt make sure the logic is right
then I wanted to program to loop through every file in the srouce directory
So I am trying to make this work with just one file
You are right about the copy / reanme but at this point I wanted to fisrt make sure the logic is right
ASKER
thasnks again you are areally a big help
ASKER
nope im wrong i didnt save the changes before i re ran my script duh !!!
sorry
thanks again ...
this is what I am using
::------------------------ ---------- ---------- ---------- ------
:: Define how to copy the files
::------------------------ ---------- ---------- ---------- ------
:CopyFiles
set CopySource=%SrcFileDir%%Sr cFile%
set DirCopyOldFilesDIR=%Destin ationFileD ir%%Promot ionDate%\% CASENumber ForPromoti on%\
set OldFileExtenxtion=.%Curren tDateTime%
echo. CopySource=%SrcFileDir%%Sr cFile% >> %dosDumpFileName%
For /F "tokens=*" %%n in ('dir %CopySource% /b') Do (
set TCopyFile=%%n
If Not Exist %DestinationFileDir%!TCopy File! (
echo. the file !TCopyFile! does not exists in %DestinationFileDir%
echo. copy %SrcFileDir%!TCopyFile! %DestinationFileDir%
) Else (
echo. the file !TCopyFile! exists in %DestinationFileDir% , destination file copying file to DirCopyOldFilesDIR% first
echo. copy %DestinationFileDir%!TCopy File! to %DirCopyOldFilesDIR%OLD_!T CopyFile!% OldFileExt enxtion%
echo. copy %SrcFileDir%!TCopyFile! %DestinationFileDir%
)
)
GOTO:EOF
the other varaibles are set before in other parts of the script
sorry
thanks again ...
this is what I am using
::------------------------
:: Define how to copy the files
::------------------------
:CopyFiles
set CopySource=%SrcFileDir%%Sr
set DirCopyOldFilesDIR=%Destin
set OldFileExtenxtion=.%Curren
echo. CopySource=%SrcFileDir%%Sr
For /F "tokens=*" %%n in ('dir %CopySource% /b') Do (
set TCopyFile=%%n
If Not Exist %DestinationFileDir%!TCopy
echo. the file !TCopyFile! does not exists in %DestinationFileDir%
echo. copy %SrcFileDir%!TCopyFile! %DestinationFileDir%
) Else (
echo. the file !TCopyFile! exists in %DestinationFileDir% , destination file copying file to DirCopyOldFilesDIR% first
echo. copy %DestinationFileDir%!TCopy
echo. copy %SrcFileDir%!TCopyFile! %DestinationFileDir%
)
)
GOTO:EOF
the other varaibles are set before in other parts of the script
ASKER
brillian easy to understand
Sorry, was a way, but glad that got you to a point you could work forward from.
~bp
~bp
~bp