My batch file is creating a new folder instead of using the one already created

Posted on 2013-06-07
Last Modified: 2013-06-11
I have a batch file that takes a spreadsheet from one folder, and moves it to another. So, I have a folder called Estimates on the D drive, D:\Estimates. Then, I have a folder on the D drive called Finalize, D:\Finalize. The batch file is supposed to move the spreadsheet from the Estimates folder into the Finalize folder using the date as the folder name. Everything works, except when i run the batch file, it is created a totally new folder called Finalize, instead of using the one i already have created. Why isn't it using the folder "Finalize" I already created? I don't want it to create a new folder. I checked spelling, and its all correct. Here's my script:

@eco off
:: variables
set drive=D:\Finalize
set folder=%date:~10,4%_%date:~4,2%_%date:~7,2%
set backupcmd=xcopy /s /c /d /e /h /i /r/ k /y

echo ### Backing up directory...
%backupcmd% "D:\Estimate" "%drive%\%folder%"
Question by:brasiman
LVL 52

Expert Comment

by:Bill Prew
ID: 39229707
it is created a totally new folder called Finalize, instead of using the one i already have created
Well, you can't have two folders directly under D:\ with the same name "Finalize", so where is the new folder located?

LVL 52

Expert Comment

by:Bill Prew
ID: 39229717
By the way, /S and /E are contradictory, only use one, depending on if you want empty subfolders copied, or not.

LVL 68

Accepted Solution

Qlemo earned 350 total points
ID: 39229833

/S and /E can co-exist. /E includes /S, making /S redundant.


If the folder name looks the same, it still might not be the same. If you type D:\Finalize in Explorer, which folder do you get? The original or the newly created one? Whichever you get, the other is spelled incorrectly.
LVL 52

Expert Comment

by:Bill Prew
ID: 39229864

Yes, I understand that XCOPY doesn't error with both /E and /E, but based on their purpose:

/S           Copies directories and subdirectories except empty ones.
/E           Copies directories and subdirectories, including empty ones.

I typically would find it bad practice to include both on the same command.  The next person looking at the script is likely to ask themselves "so, did they want to INCLUDE or EXCLUDE empty folders".  That was the only point I was making.

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 75 total points
ID: 39230034
Yes where is this duplicate folder, in the root of D:\ along with the other one?

Can you show us the output of

DIR d:\F*
dir d:\finalize

LVL 43

Expert Comment

by:Steve Knight
ID: 39230037
You can make a directory called " Finalize" for instance, i.e. space before it, is the script above your actual script or have you amended it at all to post it?

Can you run it without the @echo off (I assume you have echo off not eco off) and post the results too .

LVL 37

Expert Comment

by:Gerwin Jansen
ID: 39232110
Can you show output of:

echo ### Backing up directory...
echo %backupcmd% "D:\Estimate" "%drive%\%folder%"
LVL 11

Assisted Solution

paultomasi earned 75 total points
ID: 39233141
Why complicate things?

1) "set drive=D:\Finalize" is non-descriptive. Is source or destination?

2) "set folder=%date:~10,4%_%date:~4,2%_%date:~7,2%". I presume you date format is "ddd mm/dd/yyyy" and not "ddd m/d/yyyy" or something else.

3) "set backupcmd=xcopy /s /c /d /e /h /i /r/ k /y". Totally unnecessary.

4) "%backupcmd% "D:\Estimate" "%drive%\%folder%"". Obscure and can be confusing. Also, inconsistent because you did not set "D:\Estimate" as a variable.

Try this simple approach and stick to good programming habits:

@echo off
set "source=D:\Estimate"
set "destination=D:\Finalize"

for /f "tokens=2-4 delims=/ " %%a in ('date /t') do set "backup_date=%%c_%%a_%%b"

echo Backing up directory %source%...
xcopy "%source%\*.*" "%destination%\%backup_date%\" /c /d /h /i /k /r /s /y

Open in new window

If your delimiter in %DATE% is not '/' then change "tokens=2-4 delims=/ " to whatever it is. Oh, and note there's a space included after 'delims='.

Author Closing Comment

ID: 39239552
Thanks everyone! I read it over and over and over again, assuming i mispelled something but never saw it. Sure enough, i had something mispelled. Thanks again!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Have "L" column display the task, dates and color on the second box. 22 50
Compile Error 7 41
Import filename in batch 2 24
Check folder every for files and print them 5 23
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

896 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now