|
Answer» I WANT TO RENAME THE FILE NAME THROUGH BATCH SCRIPT. FOR EXAMPLE.
I WANT RENAME THE FILE:- D:\ASIF\ABC.TXT TO D:\ASIF\ABCSYSTEM CURRENT DATE .TXT.
ABC.TXT FILE RENAME WITH ABC01-JAN-2011 (HOW CAN I DO IN BATCH FILE).
I HOPE UNDERSTAND MY QUESTION.Is there a CAPS LOCK key on your keyboard?
You need the month name in words? E.g "Jan", "Feb", etc? And must the words be in all capital LETTERS?
Also, what is your local date format? Open a command WINDOW and type ECHO %DATE% and press Enter. What do you see?
Code: [Select]import java.io.*; import java.util.Date; import java.text.SimpleDateFormat; import java.text.Format;
public class RenameFileWithDateTime { public static void main(String[] args){ if (args.length != 1){ System.out.printf("Usage: java RenameFileWithDateTime <file name>\n"); System.exit(1); } File file = new File(args[0]); if ( file.exists() && file.isFile() ){ String f = file.toString(); String extension = ""; int d=f.length(); if ( f.indexOf(".") != -1 ) { d = f.lastIndexOf(".") ; //get the last dot extension = f.substring(d); //get the extension } String fileName = f.substring(0,d); Date date = new Date(); Format dateFormatter = new SimpleDateFormat("dd-MMM-yyyy"); String newName = fileName + dateFormatter.format( new Date() ) + extension; file.renameTo( new File( newName ) ); } } }
use Java to run the class file (attached) Code: [Select]java RenameFileWithDateTime ABC.TXT
[recovering disk space - old attachment deleted by admin]thanks salmon i'm solve it self by that command:- first i change the date format:- dd-mmm-yy by windows then WRITE this command.
rename abc.txt abc%date:~-10%.txt
salmon how can i change the date format in dos prompt?Quote from: nathani on January 02, 2011, 04:33:58 AM salmon how can i change the date format in dos prompt?
To do this requires making changes to the registry then logging off then logging on again (or system restart) and is severely not recommended, and is probably not permitted in corporate environment.
It is also unnecessary. You can use VBScript date methods to get dd mm yyyy of current date without changing local settings. The batch can write a temporary VBScript .VBS file like here:
Code: [Select]@echo off >DateString.vbs ( echo MyD = day^(Date^) echo myM = month^(Date^) echo myY = year^(Date^) echo If MyD ^< 10 Then echo DD = "0" ^& CStr^(MyD^) echo Else echo DD = CStr^(MyD^) echo End If echo If MyM ^< 10 Then echo MM = "0" ^& CStr^(MyM^) echo Else echo MM = CStr^(MyM^) echo End If echo YYYY = CStr^(MyY^) echo wscript.echo DD ^& "-" ^& MM ^& "-" ^& YYYY )
REM Example of use for /f "delims=" %%D in ('cscript //nologo DateString.vbs') do set DString=%%D & del DateString.vbs echo Today's Date is %DString%
Output
Code: [Select]Today's Date is 02-01-2011
|