Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

dos batch if exists problem

Posted on 2011-09-14
11
Medium Priority
?
364 Views
Last Modified: 2012-05-12
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=%DestinationFileDir%%PromotionDate%\%CASENumberForPromotion%\
set OldFileExtenxtion=.%CurrentDateTime%

::------------------------------------------------------------
:: Define how to copy the files
::------------------------------------------------------------
:CopyFiles
set CopySource=%SrcFileDir%%SrcFile%
set DirCopyOldFilesDIR=%DestinationFileDir%%PromotionDate%\%CASENumberForPromotion%\
set OldFileExtenxtion=.%CurrentDateTime%
echo. CopySource=%SrcFileDir%%FILENAME% >> %dosDumpFileName%
For /F "tokens=*" %%n in ('dir %CopySource% /b') Do (
set TCopyFile=%%n
If Not Exist %DestinationFileDir%%TCopyFile% (Goto:CopyFile) Else (Goto:RenameCopyFile))
GOTO:EOF    

:CopyFile
echo. copy %SrcFileDir%%TCopyFile% %DestinationFileDir%
GOTO:EOF

:RenameCopyFile
echo. rename %SrcFileDir%%TCopyFile% %SrcFileDir%OLD_%TCopyFile%%OldFileExtenxtion%
echo. copy %SrcFileDir%%TCopyFile% %DestinationFileDir%
GOTO:EOF
0
Comment
Question by:jhacharya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 58

Expert Comment

by:Bill Prew
ID: 36538015
What problem or error message are you having?

~bp
0
 
LVL 58

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 36538050
I see a few potential problems, take a look at this.  You need EnableDelayedExpansion to be able to reference a variable that you set insode the FOR loop, as in TCopyFile. Also, I don't see the CurrentDaeTime variable set. In addition I think your logic in the FOR loop was jumping out of the FOR loop after one file, not sure you wanted that.

setlocal EnableDelayedExpansion

REM NEED TO SET CurrentDateTime

set SrcFileDir=C:\SRC\
set SrcFile=TEST.TXT
set DestinationFileDir=c:\DEST\
set CASENumberForPromotion=123
set PromotionDate=20110101
set dosDumpFileName=sample.log
set DirCopyOldFilesDIR=%DestinationFileDir%%PromotionDate%\%CASENumberForPromotion%\
set OldFileExtenxtion=.%CurrentDateTime%

::------------------------------------------------------------
:: Define how to copy the files
::------------------------------------------------------------
:CopyFiles
set CopySource=%SrcFileDir%%SrcFile%
set DirCopyOldFilesDIR=%DestinationFileDir%%PromotionDate%\%CASENumberForPromotion%\
set OldFileExtenxtion=.%CurrentDateTime%
echo. CopySource=%SrcFileDir%%FILENAME% >> %dosDumpFileName%
For /F "tokens=*" %%n in ('dir %CopySource% /b') Do (
  set TCopyFile=%%n
  If Not Exist %DestinationFileDir%!TCopyFile! (
    echo. copy %SrcFileDir%!TCopyFile! %DestinationFileDir%
  ) Else (
    echo. rename %SrcFileDir%!TCopyFile! %SrcFileDir%OLD_!TCopyFile!%OldFileExtenxtion%
    echo. copy %SrcFileDir%!TCopyFile! %DestinationFileDir%
  )
)

Open in new window

~bp
0
 
LVL 2

Author Comment

by:jhacharya
ID: 36538069
i get "unexpected at this time"

the error seems to be when it tried to check for the the file existance


0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 58

Assisted Solution

by:Bill Prew
Bill Prew earned 2000 total points
ID: 36538079
One other thing I notice:

    echo. rename %SrcFileDir%!TCopyFile! %SrcFileDir%OLD_!TCopyFile!%OldFileExtenxtion%
    echo. copy %SrcFileDir%!TCopyFile! %DestinationFileDir%

Open in new window

this seems like you will rename whatever file is represented by "%SrcFileDir%!TCopyFile!", and then after renaming it try to copy the old file name.  That won't exist at that point.  I suspect you might want:

    echo. rename %SrcFileDir%!TCopyFile! %SrcFileDir%OLD_!TCopyFile!%OldFileExtenxtion%
    echo. copy %SrcFileDir%OLD_!TCopyFile!%OldFileExtenxtion% %DestinationFileDir%

Open in new window

~bp
0
 
LVL 58

Assisted Solution

by:Bill Prew
Bill Prew earned 2000 total points
ID: 36538094
Also, I think the FOR loop may be wrong.  I think you are trying to read the contents of the TEST.TXT file, line by line, right?  If so, then this:

For /F "tokens=*" %%n in ('dir %CopySource% /b') Do (

Open in new window

should be:

For /F "tokens=*" %%n in (%CopySource%) Do (

Open in new window

~bp
0
 
LVL 2

Author Comment

by:jhacharya
ID: 36538390
ok this has got past the error, but for some reason the check
If Not Exist %DestinationFileDir%!TCopyFile!

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??
0
 
LVL 2

Author Comment

by:jhacharya
ID: 36538414
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
0
 
LVL 2

Author Comment

by:jhacharya
ID: 36538416
thasnks again you are areally a big help
0
 
LVL 2

Author Comment

by:jhacharya
ID: 36538466
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%%SrcFile%
set DirCopyOldFilesDIR=%DestinationFileDir%%PromotionDate%\%CASENumberForPromotion%\
set OldFileExtenxtion=.%CurrentDateTime%
echo. CopySource=%SrcFileDir%%SrcFile% >> %dosDumpFileName%
For /F "tokens=*" %%n in ('dir %CopySource% /b') Do (
  set TCopyFile=%%n
  If Not Exist %DestinationFileDir%!TCopyFile! (
    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%!TCopyFile! to %DirCopyOldFilesDIR%OLD_!TCopyFile!%OldFileExtenxtion%
    echo. copy %SrcFileDir%!TCopyFile! %DestinationFileDir%
  )
)
GOTO:EOF  

the other varaibles are set before in other parts of the script
0
 
LVL 2

Author Closing Comment

by:jhacharya
ID: 36538474
brillian easy to understand
0
 
LVL 58

Expert Comment

by:Bill Prew
ID: 36538984
Sorry, was a way, but glad that got you to a point you could work forward from.

~bp
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question