• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

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

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
WO2015
Asked:
WO2015
  • 13
  • 12
  • 3
2 Solutions
 
NVITCommented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
WO2015Author Commented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
A move always removes the source file. A copy does not.
0
 
WO2015Author Commented:
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
 
NVITCommented:
Set All=%%A%%B%Year:~2,2%
0
 
WO2015Author Commented:
That did not work either, when I run it, it does not move the files at all.
0
 
NVITCommented:
Can you post what you have?
0
 
WO2015Author Commented:
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
 
NVITCommented:
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
 
NVITCommented:
On another note, I don't know if MOVE will work. However, COPY will
0
 
WO2015Author Commented:
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
 
NVITCommented:
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
 
WO2015Author Commented:
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
 
NVITCommented:
Oops. Hold on...
0
 
WO2015Author Commented:
Yes, I copied it and used it exact.
0
 
NVITCommented:
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
 
WO2015Author Commented:
I copied and ran this and nothing moved.
0
 
NVITCommented:
Remove the ECHO in front of MOVE
0
 
WO2015Author Commented:
The same result, nothing moved.
0
 
WO2015Author Commented:
Sorry ignore the last thing I said, here is my result now:  WEBE!All!_001.note.txt
0
 
NVITCommented:
Strange. I just tested in XP and Windows 7. No problem.

Try opening a new CMD prompt window. Then run it there.
0
 
WO2015Author Commented:
I am on Widows Sever 2008 if that matters? I used a different CMD each time.
0
 
NVITCommented:
I just tried on 2008. It works. It shows:
move "N:\mhortick\TRAKNOTE.TAB" "G:\CLSINC\TRAK\WEBE010915_001.note.txt"
0
 
WO2015Author Commented:
I copied exact, saved, and ran and my output is: WEBE!All!_001.note.txt
0
 
WO2015Author Commented:
I noticed the top line was not there, I added it and now it works. Sorry about that. And thank you!!
0
 
NVITCommented:
OK. I was just going to say check that HKLM\Software\Microsoft\Command Processor\EnableExtensions is set.
Awesome! Glad it works. Aloha!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 13
  • 12
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now