Solved

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

Posted on 2013-06-07
9
475 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
[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
9 Comments
 
LVL 54

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 54

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 69

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 54

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
 
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 38

Expert Comment

by:Gerwin Jansen, EE MVE
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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 …

726 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