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

x
?
Solved

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

Posted on 2013-06-07
9
Medium Priority
?
479 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 58

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 58

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 71

Accepted Solution

by:
Qlemo earned 1400 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 58

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

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…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

610 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