bat file ren (rename) file error
Microsoft Windows [Version 6.0.6002]
1) A Bat file, "test.bat", contains;
ren E:\ORACLE\SQL\DMP\TESTR\PLX001.zip PLX001_%time%.zip
PAUSE
2) Execute "test.bat" and command window
shows this output below on screen;
ren E:\ORACLE\SQL\DMP\TESTR\PLX001.zip PLX001_16:05:07.30.zip
A duplicate file name exists, or the file
cannot be found.
------------------------------------
Clearly windows has found the file, "PLX001.zip" because it has renamed it on screen with the time "PLX001_16:05:07.30.zip".
But for some reason it still errors because it can't find it? or considers "PLX001.zip" the same name as "PLX001_16:05:07.30.zip" ?
How can this be?
Why won't it rename the actual file in the folder as it is doing on screen?
Thank you for your time,
Donald
March 28th, 2011 11:59pm
The command processor is echoing the command, with %time% expanded, before running it. It does not mean it has found the file. I get the same result when I run the batch file, and I don't have the file. However, I get a different error message if the drive
or the path are not found. I also get a different error if the path is OK but the file does not exist. However, I get your error if the file exists, but it is read-only. My guess is that the file is read-only, you lack permissions, or perhaps it is open
by another process.
Richard Mueller - MVP Directory Services
Free Windows Admin Tool Kit Click here and download it now
March 29th, 2011 4:29am
i think its about the ":" in the time. if you strip the ":" from the time like %time:~0,2%%time:~3,2%%time:~6,2% it works
March 29th, 2011 6:12am
Thank you FZB for repling. I have been working on that exact syntax but it seems to fail to read the leading digit in the 24 hour properly and errors.
The machines time format is 24hr, no AM or PM characters; HH:mm:ss
and displays as; 06:21:01
ren E:\ORACLE\SQL\DMP\TESTR\PLX001.zip PLX001_%time:~0,2%%time:~3,2%%time:~6,2%.zip
PAUSE
yields;
ren E:\ORACLE\SQL\DMP\TESTR\PLX001.zip PLX001_ 62101.zip
The syntax of the command is incorrect.
The file name does not get renamed. The coding can't deal with the leading zero in the 24 display, I've tried other 12 hr display w/AM PM but it fails too (we need it in 24 hr anyways)
How can I get it to work to show, PLX001_062101.zip, or when later in day when the hour is 2 digits, like 13 for 1pm?
Thank you for you time,
Donald
Free Windows Admin Tool Kit Click here and download it now
March 29th, 2011 6:31am
not sure about the leading zero for 1 digit hours. the syntax error comes from the space though, so that error could be avoided by using quotation marks around the second expression (like ren query.log "query%time:~0,2%%time:~3,2%%time:~6,2%.log")
March 29th, 2011 7:50am
Hi,
Adding the quotes yields:
"PLX001_ 75203.zip"
So that won't work for single digit hours with leading zero or double digits hours.
What is odd is that the string works for date stamp, ie the %date:~0,2% displays the
correct 03 for march but that logic won't work using %time:~0,2% to display 07 in hour of time.
Additionally odd is that this string using %time:~1,1% (with or without quotes)
ren E:\ORACLE.....\PLX001.zip "PLX001_%time:~1,1%%time:~3,2%%time:~6,2%.zip"
yields: PLX001_75934.zip
I wonder what will happen with a 2 digit time to evaluate? My guess is that it would return only
the 2nd digit, giving the wrong time stamp.
I think this is an undocumented bug in the 2008 server :(
Any other ideas? I'm completely stumped...
Free Windows Admin Tool Kit Click here and download it now
March 29th, 2011 8:09am
Thank you for the reply Richard, sorry for my slow response - I'm located in Dublin, Ireland and just saw your post...
I have local and domain admin permissions to the machine, folders, and file. Below renames the file in the folder as "PLX001_wishthisworked.zip".
REM ren E:\ORACLE\SQL\DMP\TESTR\PLX001.zip PLX001_%time%.zip
ren E:\ORACLE\SQL\DMP\TESTR\PLX001.zip PLX001_wishthisworked.zip
PAUSE
I'm stumped :( The attribute for read-only isn't checked, I've got the permissions, and I didn't find any other processess accessing / opened by... The file is static in its own test area. Only myself and network admin has access to the machine and he is
not logged on and wouldn't be touching that file if he was...
How can one add the machine's time appended to a file name? It doesn't matter to me what the timestamp format is, I just need it appended to the file name as a suffix.
I feel like such an idiot! as this should be child's play...
Thanks, Donald
March 29th, 2011 11:25am
Bueler ? Anyone?.... Bueler? " Hi, Adding the quotes yields: "PLX001_ 75203.zip" So that won't work for single digit hours with leading zero or double digits hours. What is odd is that the string works for date stamp, ie the %date:~0,2% displays the correct
03 for march but that logic won't work using %time:~0,2% to display 07 in hour of time. Additionally odd is that this string using %time:~1,1% (with or without quotes) ren E:\ORACLE.....\PLX001.zip "PLX001_%time:~1,1%%time:~3,2%%time:~6,2%.zip" yields: PLX001_75934.zip
I wonder what will happen with a 2 digit time to evaluate? My guess is that it would return only the 2nd digit, giving the wrong time stamp. I think this is an undocumented bug in the 2008 server :( Any other ideas? I'm completely stumped... "
Free Windows Admin Tool Kit Click here and download it now
May 3rd, 2011 9:46am
for /f "tokens=1 delims=: " %%a in ('echo %time%') do set timehrs=%%a
for /f "tokens=2 delims=: " %%a in ('echo %time%') do set timemin=%%a
for /f "tokens=3 delims=:. " %%a in ('echo %time%') do set timesec=%%a
ren file.bat file_%timehrs%.%timemin%.%timesec%.bat
Echoing time variable uses a 24 Hour format. This should work fairly well...
Steve Kline
Microsoft Certified IT Professional: Server Administrator
Microsoft Certified Technology Specialist: Active Directory, Network Infrastructure, Application Platform, Windows 7
Microsoft Certified Product Specialist & Network Product Specialist
Red Hat Certified System Administrator
This posting is "as is" without warranties and confers no rights.
May 3rd, 2011 9:57am
Additionally... you can add this after all of the "For" statements.
set ModTime=%timehrs%.%timemin%.%timesec%
ren file.bat file_%modtime%.bat
If you need to change the file name or do not want this to execute as a batch, you can make this a live environment variable each time you run the command prompt by saving it to your desktop as a CMD file instead of BAT. Remove the "REN" command from the
CMD file so it does not rename.
Then you have a %modtime% variable to throw around for any command for your environment. I have quite a few date/time mods on a command file.
Add a couple "CD" commands to change your default directory... and you'll make it a quite convenient commandline experience.
Steve Kline
Microsoft Certified IT Professional: Server Administrator
Microsoft Certified Technology Specialist: Active Directory, Network Infrastructure, Application Platform, Windows 7
Microsoft Certified Product Specialist & Network Product Specialist
Red Hat Certified System Administrator
This posting is "as is" without warranties and confers no rights.
Free Windows Admin Tool Kit Click here and download it now
May 3rd, 2011 10:24am
Hi Steve,
Thank you for the reply and code ! I had been working on similar but was not getting it right, I think I have only one '%' instead of 2... But I will give what you have provided a shot and report back... At the moment I've got a few meetings until
about 2pm east coast usa time and won't be able to test this until afterwards ;( ....
Thank you for your time and input,
Donald
May 3rd, 2011 10:29am
Here's one of my crazy "mad-hatter/white rabbit" time command files.
@ECHO OFF
TITLE Time for Tea!!!
MODE CON: COLS=80 LINES=20
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
for /f "tokens=1* delims= " %%a in ('date/t') do set dayabbr=%%a
for /f "tokens=1* delims= " %%a in ('date/t') do set mmddyyyy=%%a
for /f "tokens=1* delims=/" %%a in ('echo %mmddyyyy%') do set today=%%a
for /f "tokens=1 delims=: " %%a in ('echo %time%') do set timehrs=%%a
for /f "tokens=2 delims=: " %%a in ('echo %time%') do set timemin=%%a
for /f "tokens=3 delims=:. " %%a in ('echo %time%') do set timesec=%%a
set modtime=%timehrs%:%timemin%:%timesec%
set datenew=%yyyy%%mm%%dd%
CLS
IF "%mm%"=="01" SET mmname=January
IF "%mm%"=="02" SET mmname=February
IF "%mm%"=="03" SET mmname=March
IF "%mm%"=="04" SET mmname=April
IF "%mm%"=="05" SET mmname=May
IF "%mm%"=="06" SET mmname=June
IF "%mm%"=="07" SET mmname=July
IF "%mm%"=="08" SET mmname=August
IF "%mm%"=="09" SET mmname=September
IF "%mm%"=="10" SET mmname=October
IF "%mm%"=="11" SET mmname=November
IF "%mm%"=="12" SET mmname=December
CLS
IF "%today%"=="Mon" SET day=Monday
IF "%today%"=="Tue" SET day=Tuesday
IF "%today%"=="Wed" SET day=Wednesday
IF "%today%"=="Thu" SET day=Thursday
IF "%today%"=="Fri" SET day=Friday
IF "%today%"=="Sat" SET day=Saturday
IF "%today%"=="Sun" SET day=Sunday
CLS
echo. Hello %username% from the %userdnsdomain% world!
echo Day of the week: %day% - %dayabbr%
echo The month is: %mm%
echo The date is : %dd%
echo The year is : %yyyy%
echo The name of the month: %mmname%
echo The current Time is %timehrs%:%timemin%:%timesec%
cmd.exe /K
Steve Kline
Microsoft Certified IT Professional: Server Administrator
Microsoft Certified Technology Specialist: Active Directory, Network Infrastructure, Application Platform, Windows 7
Microsoft Certified Product Specialist & Network Product Specialist
Red Hat Certified System Administrator
This posting is "as is" without warranties and confers no rights.
Free Windows Admin Tool Kit Click here and download it now
May 3rd, 2011 10:31am
You can post your query on
Scripting Forum.
Thanks
May 3rd, 2011 10:56am
Hi Steve,
Thanks again for the script snippet and the others too... the time function works great now in the rename of the filex, thank you very much!
Best Regards,
Donald
Free Windows Admin Tool Kit Click here and download it now
May 4th, 2011 4:13pm