Solved

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

Posted on 2013-06-07
9
470 Views
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%"
0
Comment
Question by:brasiman
9 Comments
 
LVL 51

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?

~bp
0
 
LVL 51

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.

~bp
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 350 total points
ID: 39229833
Bill,

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


brasiman,

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.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39229864
@Qlemo,

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.

~bp
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Steve
0
 
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 .

Steve
0
 
LVL 37

Expert Comment

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

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

Assisted Solution

by:paultomasi
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='.
0
 

Author Closing Comment

by:brasiman
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!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to count occurrences of each item in an array.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

760 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

18 Experts available now in Live!

Get 1:1 Help Now