?
Solved

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

Posted on 2015-01-09
28
Medium Priority
?
228 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 26

Expert Comment

by:NVIT
ID: 40540421
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 71

Expert Comment

by:Qlemo
ID: 40540612
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 71

Assisted Solution

by:Qlemo
Qlemo earned 1000 total points
ID: 40540616
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:WO2015
ID: 40540647
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 71

Expert Comment

by:Qlemo
ID: 40540742
A move always removes the source file. A copy does not.
0
 

Author Comment

by:WO2015
ID: 40541029
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 26

Expert Comment

by:NVIT
ID: 40541051
Set All=%%A%%B%Year:~2,2%
0
 

Author Comment

by:WO2015
ID: 40541066
That did not work either, when I run it, it does not move the files at all.
0
 
LVL 26

Expert Comment

by:NVIT
ID: 40541068
Can you post what you have?
0
 

Author Comment

by:WO2015
ID: 40541073
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 26

Expert Comment

by:NVIT
ID: 40541084
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 26

Expert Comment

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

Author Comment

by:WO2015
ID: 40541093
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 26

Expert Comment

by:NVIT
ID: 40541099
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
 

Author Comment

by:WO2015
ID: 40541112
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 26

Expert Comment

by:NVIT
ID: 40541131
Oops. Hold on...
0
 

Author Comment

by:WO2015
ID: 40541136
Yes, I copied it and used it exact.
0
 
LVL 26

Accepted Solution

by:
NVIT earned 1000 total points
ID: 40541190
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
ID: 40541202
I copied and ran this and nothing moved.
0
 
LVL 26

Expert Comment

by:NVIT
ID: 40541207
Remove the ECHO in front of MOVE
0
 

Author Comment

by:WO2015
ID: 40541224
The same result, nothing moved.
0
 

Author Comment

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

Expert Comment

by:NVIT
ID: 40541242
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
ID: 40541245
I am on Widows Sever 2008 if that matters? I used a different CMD each time.
0
 
LVL 26

Expert Comment

by:NVIT
ID: 40541256
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
ID: 40541260
I copied exact, saved, and ran and my output is: WEBE!All!_001.note.txt
0
 

Author Comment

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

Expert Comment

by:NVIT
ID: 40541272
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

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!

Question has a verified solution.

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

Windows Server 2003 introduced persistent Volume Shadow Copies and made 2003 a must-do upgrade.  Since then, it's been a must-implement feature for all servers doing any kind of file sharing.
Unable to change the program that handles the scan event from a network attached Canon/Brother printer/scanner. This means you'll always have to choose which program handles this action, e.g. ControlCenter4 (in the case of a Brother).
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Progress

755 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