Solved

Renaming and moving files within a .bat file in Windows Scheduler

Posted on 2015-01-09
28
218 Views
Last Modified: 2015-01-09
Hello,

I am trying to do two different tasks (not all in one .bat file). First, I have files that run and they are pre named with the correct date naming convention. How do  I move (or copy) a file from the folder the windows scheduler outputs to, to another folder so someone can get the file?  The file name changes daily because it has today's date in the name. Below is an example:
The file outputs here n:/mhortick/   named WEBE010915.ACT.TXT  tomorrow that file will be named WEBE011015.ACT.TXT. Regardless of the name I need to move (or copy) it to another folder.

Second, how do I rename a file that comes out as a generic name. I want to use today's date but I want the year to be 15 and not 2015.  Below is what I have for this already. It works, but the 2015 still shows and it is not what my client wants:

@echo off
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%%A%%B%%C
)
copy "N:\mhortick\TRAKNOTE.TAB" "G:\CLSINC\TRAK\WEBE"%All%"_001.note.txt"
0
Comment
Question by:WO2015
  • 13
  • 12
  • 3
28 Comments
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
Please clarify your 1st issue. Are'nt you already copying the file via your COPY command (I think)? Do you mean there are several files in there and you need JUST THAT FILE?

For your 2nd issue, try:
Set All=%%A%%B%%C:~2,2%
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
The move would just be
move "N:\mhortick\WEBE*.ACT.TXT" "C:\Somewhere\Else\"

Open in new window

As in your other batch, you can rename the file to e.g. WEBE.ACT.TXT in the same go.
0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
Comment Utility
On another note, you know that
Set Day=%%A
Set Month=%%B
Set Year=%%C

Open in new window

is superfluous, because those vars are not used anywhere?
0
 

Author Comment

by:WO2015
Comment Utility
Is there a way to remove the original file from the first folder once the "move" takes place?  Thank you, the second solution worked for moving files with a different date everyday. I am waiting for a script to complete before I can test the 2015 to 15 naming convention.

I did not know those Set's were not needed. Everything always worked good with them so I have never known otherwise.

Thanks again, this is a big help.
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
A move always removes the source file. A copy does not.
0
 

Author Comment

by:WO2015
Comment Utility
The solution for my second issue did not work:
For your 2nd issue, try:
Set All=%%A%%B%%C:~2,2%

Would anyone else happen to know how to make the year 15 instead of 2015?
0
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
Set All=%%A%%B%Year:~2,2%
0
 

Author Comment

by:WO2015
Comment Utility
That did not work either, when I run it, it does not move the files at all.
0
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
Can you post what you have?
0
 

Author Comment

by:WO2015
Comment Utility
All I am changing is the Set All=, it worked with what I had before (except with the 2015). When I change it nothing even moves:

@echo off
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%%A%%B%Year:~2,2%
)
move "N:\mhortick\TRAKNOTE.TAB" "G:\CLSINC\TRAK\WEBE"%All%"_001.note.txt"
0
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
I changed:
move "N:\mhortick\TRAKNOTE.TAB" "G:\CLSINC\TRAK\WEBE"%All%"_001.note.txt"

Open in new window

...to
move "N:\mhortick\TRAKNOTE.TAB" "G:\CLSINC\TRAK\WEBE%All%_001.note.txt"

Open in new window

When I run it, it shows:
move "N:\mhortick\TRAKNOTE.TAB" "G:\CLSINC\TRAK\WEBE010915_001.note.txt"

Open in new window

Would this work for you?
0
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
On another note, I don't know if MOVE will work. However, COPY will
0
 

Author Comment

by:WO2015
Comment Utility
That allowed it to run but now my file name is this:

WEBE0109~2,2_001.note.txt  I am trying to get it to be: WEBE010915_001.note.txt
0
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
Here's mine:
@echo off
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%%A%%B%Year:~2,2%
)
move "N:\mhortick\TRAKNOTE.TAB" "G:\CLSINC\TRAK\WEBE%All%_001.note.txt"

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:WO2015
Comment Utility
Whether I use copy or move it runs but the output is not correct:

WEBE0109~2,2_001.note.txt is what the output is,  I am trying to get it to be: WEBE010915_001.note.txt
0
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
Oops. Hold on...
0
 

Author Comment

by:WO2015
Comment Utility
Yes, I copied it and used it exact.
0
 
LVL 23

Accepted Solution

by:
NVIT earned 250 total points
Comment Utility
SETLOCAL ENABLEDELAYEDEXPANSION
@echo off

For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
  Set Day=%%A
  Set Month=%%B
  Set Year=%%C
  Set All=%%A%%B!Year:~-2!
)
echo move "N:\mhortick\TRAKNOTE.TAB" "G:\CLSINC\TRAK\WEBE!All!_001.note.txt"

Open in new window

0
 

Author Comment

by:WO2015
Comment Utility
I copied and ran this and nothing moved.
0
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
Remove the ECHO in front of MOVE
0
 

Author Comment

by:WO2015
Comment Utility
The same result, nothing moved.
0
 

Author Comment

by:WO2015
Comment Utility
Sorry ignore the last thing I said, here is my result now:  WEBE!All!_001.note.txt
0
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
Strange. I just tested in XP and Windows 7. No problem.

Try opening a new CMD prompt window. Then run it there.
0
 

Author Comment

by:WO2015
Comment Utility
I am on Widows Sever 2008 if that matters? I used a different CMD each time.
0
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
I just tried on 2008. It works. It shows:
move "N:\mhortick\TRAKNOTE.TAB" "G:\CLSINC\TRAK\WEBE010915_001.note.txt"
0
 

Author Comment

by:WO2015
Comment Utility
I copied exact, saved, and ran and my output is: WEBE!All!_001.note.txt
0
 

Author Comment

by:WO2015
Comment Utility
I noticed the top line was not there, I added it and now it works. Sorry about that. And thank you!!
0
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
OK. I was just going to say check that HKLM\Software\Microsoft\Command Processor\EnableExtensions is set.
Awesome! Glad it works. Aloha!
0

Featured Post

Want to promote your upcoming event?

Attending an event? Speaking at a conference? Or exhibiting at a tradeshow? Easily inform your contacts by using a promotional banner in your email signature. This will ensure your organization’s most important contacts are in the know.

Join & Write a Comment

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

728 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

15 Experts available now in Live!

Get 1:1 Help Now