New to BAT files - Minor change to this BAT file

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

LVL 8
CamilliaAsked:
Who is Participating?
 
jsdrayCommented:
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
 
jsdrayCommented:
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
 
jsdrayCommented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
CamilliaAuthor Commented:
let me try, thanks. Will post back.
0
 
CamilliaAuthor Commented:
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
 
jsdrayCommented:
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
 
Neil RussellTechnical Development LeadCommented:
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
 
CamilliaAuthor Commented:
Yes, I will try the move.
0
 
Bill PrewCommented:
+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
 
CamilliaAuthor Commented:
I will test this as well..
0
 
jsdrayCommented:
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
 
CamilliaAuthor Commented:
let me get the file names. It's 4 files. I will post back when i get to work.
0
 
CamilliaAuthor Commented:
There are 6 files: PF.txt , PFDelta.txt, BN.txt and BNDelta.txt, WR.txt and WRDelta.txt

So, use bill's method?
0
 
Bill PrewCommented:
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
 
CamilliaAuthor Commented:
yes, that works, thanks so much for all your help.
0
 
Bill PrewCommented:
Welcome.

~bp
0
 
ReneGeCommented:
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
 
CamilliaAuthor Commented:
I will try this one as well. I have a related question and i will open one later. thanks again.
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.

All Courses

From novice to tech pro — start learning today.