Solved

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

Posted on 2013-06-07
9
476 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 55

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 55

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 70

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 55

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

688 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