?
Solved

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

Posted on 2015-01-09
28
Medium Priority
?
225 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
[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
  • 13
  • 12
  • 3
28 Comments
 
LVL 25

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 70

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 70

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
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 

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 70

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 25

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 25

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 25

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 25

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 25

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 25

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 25

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 25

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 25

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 25

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 25

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

A small collection of useful tips and tricks for Windows 10 users that I decided to write as a result of recent questions that were asked and answered at Experts Exchange. Two short video tutorials included. Enjoy..
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

752 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