[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

New to BAT files - Minor change to this BAT file

Posted on 2011-09-27
20
Medium Priority
?
395 Views
Last Modified: 2013-11-09
The BAT file in the attached related question works but I need to tweak it and I dont know how.

I copy a file called "PF.txt". It renames it to PF20112709.txt
I copy another file with the same name "PF.txt" but the renamed file gets renamed to "PF2011270920112709.txt"

1. How can I add "time" at the end of the file?
2. I need to rename the one that doesnt have date/time attached to it?

So, I can have the same filename and i need to rename the one that hasnt been changed by the BAT file...

I've posted the file again here
echo off
cls

set ManifestDir=c:\manifest\test

for /r %ManifestDir% %%i in (*.*) do (
call :doitnow %%i %%~ni %%~dpi %%~xi
)
goto eof

:doitnow
set ManifestFile=%1
set ManifestFileNameOnly=%2
set ManifestFilePath=%3
set ManifestFileExt=%4
RENAME %ManifestFile% %ManifestFileNameOnly%%date:~-4,4%%date:~-7,2%%date:~-10,2%%ManifestFileExt%
:eof

Open in new window

0
Comment
Question by:Camillia
  • 8
  • 5
  • 3
  • +2
18 Comments
 
LVL 9

Expert Comment

by:jsdray
ID: 36710881
Hello again...
Use this to add time to the end...

%time:~,2%%time:~3,2%%time:~6,2%

Change the rename line to

RENAME %ManifestFile% %ManifestFileNameOnly%%date:~-4,4%%date:~-7,2%%date:~-10,2%%time:~,2%%time:~3,2%%time:~6,2%%ManifestFileExt%
0
 
LVL 9

Expert Comment

by:jsdray
ID: 36710903
if I understand correctly, you're wanting to redo the ones already with the date and just add the time too...correct?

If so, change the rename line to this and run it...


RENAME %ManifestFile% %ManifestFileNameOnly%%time:~,2%%time:~3,2%%time:~6,2%%ManifestFileExt%
0
 
LVL 7

Author Comment

by:Camillia
ID: 36711239
let me try, thanks. Will post back.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
LVL 7

Author Comment

by:Camillia
ID: 36712301
They both work but the thing is ...it keeps adding to the same file.
For example...i have "PF.txt". I run it and it's PF134112.txt.
This is correct. I copy another file with the same name "PF.txt". Run it again and now I see:

PF134112134358.txt
and PF134358.txt

It keeps adding the time stamp to the first file as well. Files can be the same name when copied to the folder but timestamp should be attached to the one that doesnt have a timestamp aleady. The way it works now...i'll have HUGE file names :)
0
 
LVL 9

Accepted Solution

by:
jsdray earned 668 total points
ID: 36717267
echo on

cls

set ManifestDir=c:\manifest\test

for /r %ManifestDir% %%i in (*.*) do (
call :doitnow %%i %%~ni %%~dpi %%~xi
)
goto eof

:doitnow
set ManifestFile=%1
set ManifestFileNameOnly=%2
set ManifestFilePath=%3
set ManifestFileExt=%4

echo.%ManifestFileNameOnly:~-5%|findstr [0-9][0-9][0-9][0-9][0-9]
if %errorlevel%==1 (
RENAME %ManifestFile% %ManifestFileNameOnly%%time:~,2%%time:~3,2%%time:~6,2%%ManifestFileExt%
)
:eof
0
 
LVL 9

Expert Comment

by:jsdray
ID: 36717302
i only check for 5 digits since i'm not sure how your system handles earlier than 10am times... 09 or (space)9  so 5 didgits should be adequate to determin if you already ran this or not on that file...
0
 
LVL 37

Assisted Solution

by:Neil Russell
Neil Russell earned 664 total points
ID: 36717311
Your easiest solution would be have two directories. One for inbound files and one for completed.
Process the files in your Inbound directory with your script but add a MOVE command after the RENAME to move it to the new COMPLETED folder.
simples :D


set TimeStamp=%time:~,2%%time:~3,2%%time:~6,2%
RENAME %ManifestFile% %ManifestFileNameOnly%%TimeStamp%%ManifestFileExt%
MOVE %ManifestFileNameOnly%%TimeStamp%%ManifestFileExt%  c:\COMPLETED
0
 
LVL 7

Author Comment

by:Camillia
ID: 36717411
Yes, I will try the move.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36719885
+1 to @Neilsr comment, the best way to avoid issues is to move them.

But it looks like they could be nested in subfolders and that could cause a bit of a problem.

As an alternative consider changing the extension to something different like BAK, and then on the FOR line only look for TXT files, like this:

echo off
set ManifestDir=c:\manifest\test
for /R %ManifestDir% %%I in (*.txt) do (
  ren "%%~i" "%%~ni%DATE:~-4,4%%DATE:~-7,2%%DATE:~-10,2%%TIME:~,2%%TIME:~3,2%%TIME:~6,2%.bak"
)

Open in new window

~bp
0
 
LVL 7

Author Comment

by:Camillia
ID: 36719908
I will test this as well..
0
 
LVL 9

Expert Comment

by:jsdray
ID: 36812091
how many different file types do you plan to run this against? nested folders?
I understand what neilsr is doing, but as posted won't work unless script is in same dir.  MOVE will also allow rename.
 I like Billprew's idea better, but you'll end up running multiple times for desired results if you plan to run against multiple extensions.  Other wise, my last script posted will do exactly as you've asked with any extension and sub directories.  
Been fun scripting this for you...enjoy and hope you get what you need.
0
 
LVL 7

Author Comment

by:Camillia
ID: 36814191
let me get the file names. It's 4 files. I will post back when i get to work.
0
 
LVL 7

Author Comment

by:Camillia
ID: 36814467
There are 6 files: PF.txt , PFDelta.txt, BN.txt and BNDelta.txt, WR.txt and WRDelta.txt

So, use bill's method?
0
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 668 total points
ID: 36814509
Based on that, give this a try. It will rename only the files with those names adding the date time.

echo off
set ManifestDir=c:\manifest\test
for /R %ManifestDir% %%I in (pf.txt* pfdelta.txt* bn.txt* bndelta.txt* wr.txt* wrdelta.txt*) do (
  ren "%%~I" "%%~nI%DATE:~-4,4%%DATE:~-7,2%%DATE:~-10,2%%TIME:~,2%%TIME:~3,2%%TIME:~6,2%%%~xI"
)

Open in new window

~bp
0
 
LVL 7

Author Comment

by:Camillia
ID: 36814838
yes, that works, thanks so much for all your help.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36815252
Welcome.

~bp
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 36815473
According to your accepted solution, you may end up having problems if your FOR loop finds files with spaces, because of the CALL command. Here is what I suggest.

Cheers,
Rene
echo on

cls

set ManifestDir=c:\manifest\test

for /r %ManifestDir% %%i in (*.*) do (
call :doitnow "%%i" "%%~ni" "%%~dpi" "%%~xi"
)
goto eof

:doitnow
set ManifestFile=%~1
set ManifestFileNameOnly=%~2
set ManifestFilePath=%~3
set ManifestFileExt=%~4

echo.%ManifestFileNameOnly:~-5%|findstr [0-9][0-9][0-9][0-9][0-9]
if %errorlevel%==1 (
RENAME %ManifestFile% %ManifestFileNameOnly%%time:~,2%%time:~3,2%%time:~6,2%%ManifestFileExt%
)
:eof

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 36815506
I will try this one as well. I have a related question and i will open one later. thanks again.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 21 hours left to enroll

834 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